OCR support means the ability to connect an external module (library) for the recognition of printed text (optical character recognition, OCR) on images, either separate or embedded in documents.
To connect OCR, you need to implement the IOcrConnector interface in the client code and assign the connector to the ocr_connector property of the OcrIndexingOptions object of the indexing options. The recognize method receives an OcrContext object, which provides the image stream (context.image_stream.read() returns the bytes of the image), the location of the image (ImageLocation: a separate image file, an image embedded in a document or an image that is an item of a container) and the image file extension. The method returns the recognized text, or None if there is none. In Python, the connector is a plain class with a recognize(self, context) method; do not derive it from the IOcrConnector class. Since the interface has one method, a function that takes the context can be assigned as the connector too. The OcrIndexingOptions class also specifies for which images text recognition is enabled (see Indexing options).
Python has no built-in OCR, so the connector calls an OCR package. The following example demonstrates how to implement the OCR connector using the EasyOCR package for text recognition in images (pip install easyocr). Without the package, the connector prints the images it receives and recognizes no text, so the example runs without it.
fromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportImageLocation,IndexingOptionstry:importeasyocr# pip install easyocrexceptImportError:easyocr=None# Implementing the OCR connector that uses the EasyOCR packageclassEasyOcrConnector(object):def__init__(self):self.reader=easyocr.Reader(["en"])ifeasyocrisnotNoneelseNonedefrecognize(self,context):extension=context.image_file_extension.lower()ifcontext.image_location==ImageLocation.SEPARATE:ifextensionin(".gif",".png",".jpg",".jpeg",".bmp",".tif",".tiff",""):returnself.recognize_private(context)returnNoneelifcontext.image_locationin(ImageLocation.EMBEDDED,ImageLocation.CONTAINER_ITEM):returnself.recognize_private(context)else:raiseNotImplementedError("The image type is not supported: "+str(context.image_location))defrecognize_private(self,context):image=context.image_stream.read()# Reading the whole image streamifself.readerisNone:# Without EasyOCR, the connector only reports the image it receivesprint("Image: %s, extension '%s', %d bytes"%(context.image_location.name,context.image_file_extension,len(image)))returnNonereturn" ".join(self.reader.readtext(image,detail=0))# Creating an indexindex=Index("./index")# Setting the OCR indexing optionsoptions=IndexingOptions()options.ocr_indexing_options.enabled_for_separate_images=Trueoptions.ocr_indexing_options.enabled_for_embedded_images=Trueoptions.ocr_indexing_options.ocr_connector=EasyOcrConnector()# Indexing documents in a document folderindex.add("./documents",options)# Searching in the indexresult=index.search("Einstein")print("Documents found:",result.document_count)
The next example demonstrates a connector written as a function. Such a function can send the image to a cloud OCR service, for example Aspose.OCR Cloud, and return the recognized text; the example only prints what it receives.
fromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportIndexingOptions# Implementing the OCR connector as a functiondefrecognize(context):image=context.image_stream.read()# Send the image to an OCR service here, for example, Aspose.OCR Cloud,# and return the text it recognizesprint("Image to recognize: %s, extension '%s', %d bytes"%(context.image_location.name,context.image_file_extension,len(image)))returnNone# Creating an indexindex=Index("./index")# Setting the OCR indexing optionsoptions=IndexingOptions()options.ocr_indexing_options.enabled_for_separate_images=Trueoptions.ocr_indexing_options.ocr_connector=recognize# Indexing documents in a document folderindex.add("./documents",options)
And the following example demonstrates how to implement the OCR connector using Tesseract through the pytesseract package (pip install pytesseract pillow), which also needs the Tesseract OCR engine with the English language data.
importiofromgroupdocs.searchimportIndexfromgroupdocs.search.optionsimportIndexingOptionstry:# pip install pytesseract pillow# The Tesseract OCR engine with the English language data must be installed tooimportpytesseractfromPILimportImageexceptImportError:pytesseract=None# Implementing the OCR connector that uses TesseractclassTesseractOcrConnector(object):defrecognize(self,context):buffer=context.image_stream.read()# Reading the whole image streamifpytesseractisNone:print("pytesseract is not installed: an image of %d bytes is not recognized"%len(buffer))returnNonewithImage.open(io.BytesIO(buffer))asimage:recognized_text=pytesseract.image_to_string(image,lang="eng")returnrecognized_text# Creating an indexindex=Index("./index")# Setting the OCR indexing optionsoptions=IndexingOptions()options.ocr_indexing_options.enabled_for_separate_images=Trueoptions.ocr_indexing_options.ocr_connector=TesseractOcrConnector()# Indexing documents in a document folderindex.add("./documents",options)
Along with the full-featured library, we provide a free online document search app. You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our Free Online Document Search App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.