[Python] Efficient OCR finetuning

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project that involves OCRing an extremely large number of PDFs. Each PDF is an image of a normal, office-type document. Each PDF page is stored a separate file. Is there any way to do this faster (i.e. with lower compute time) than my current approach?

    With a project this size, any small performance improvement would yield a large reduction in computing cost/time. Currently, the call to Tesseract takes about 2-3 orders of magnitude more time than all other processing steps in the project.

    For each file (page.pdf), I first convert it to Tiff (image.tiff), like so. I stripped the Python off of this call for clarity.

    ghostscript -o image.tiff -q -r300x300 -sCompression=none -sDEVICE=tiffgray page.pdf


    Then I run essentially the following procedure.

    def ocr( image ):
    import tesseract, cv2.cv as cv

    # Load Tesseract
    api = tesseract.TessBaseAPI()
    api.Init(".","eng",tesseract.OEM_DEFAULT)
    api.SetPageSegMode(tesseract.PSM_AUTO)

    # Load and OCR the image file
    cv_image = cv.LoadImage(image, cv.CV_LOAD_IMAGE_GRAYSCALE)
    tesseract.SetCvImage(cv_image, api)

    # Return the text
    return api.GetUTF8Text()


    I've simplified a bit for clarity. I don't actually separately load Tesseract for each file, for instance. I handle exceptions, etc. However, the differences don't really matter, since 99.9% of the time used is in the api.GetUTF8Text() call.

    Any ideas to speed it up?

    Continue reading...

Compartilhe esta Página