Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def tesseract_image(image, languages):
"""Extract text from a binary string of data."""
languages = get_languages(languages)
if not hasattr(thread, 'api'):
thread.api = PyTessBaseAPI(lang=languages,
path=TESSDATA_PREFIX)
else:
thread.api.Init(path=TESSDATA_PREFIX, lang=languages)
try:
# TODO: play with contrast and sharpening the images.
thread.api.SetPageSegMode(PSM.AUTO_OSD)
thread.api.SetImage(image)
return thread.api.GetUTF8Text()
except RuntimeError as re:
log.warning(re)
return None
finally:
thread.api.Clear()
def configure_engine(self, languages):
from tesserocr import PyTessBaseAPI, PSM, OEM
if not hasattr(self.tl, 'api') or self.tl.api is None:
log.info("Configuring OCR engine (%s)", languages)
self.tl.api = PyTessBaseAPI(lang=languages,
oem=OEM.LSTM_ONLY,
psm=PSM.AUTO_OSD)
if languages != self.tl.api.GetInitLanguagesAsString():
log.info("Re-initialising OCR engine (%s)", languages)
self.tl.api.Init(lang=languages, oem=OEM.LSTM_ONLY)
return self.tl.api
# -*-coding:utf8-*-#
__author__ = 'play4fun'
"""
create time:16/10/21 11:47
"""
# Orientation and script detection (OSD)
from PIL import Image
from tesserocr import PyTessBaseAPI, PSM
with PyTessBaseAPI(psm=PSM.AUTO_OSD) as api:
# image = Image.open("/usr/src/tesseract/testing/eurotext.tif")#No such file
# image = Image.open("eurotext.tif")
image = Image.open('phototest.tif')
api.SetImage(image)
api.Recognize()
it = api.AnalyseLayout()
orientation, direction, order, deskew_angle = it.Orientation()
print("Orientation: {:d}".format(orientation))
print("WritingDirection: {:d}".format(direction))
print("TextlineOrder: {:d}".format(order))
print("Deskew angle: {:.4f}".format(deskew_angle))
#
ocrResult = api.GetUTF8Text()
print('result:\n',ocrResult)
import time
import logging
import threading
from PIL import Image
from io import BytesIO
from languagecodes import list_to_alpha3 as alpha3
from tesserocr import PyTessBaseAPI, get_languages, PSM, OEM # noqa
log = logging.getLogger(__name__)
class OCR(object):
MAX_MODELS = 5
DEFAULT_MODE = PSM.AUTO_OSD
# DEFAULT_MODE = PSM.AUTO
def __init__(self):
# Tesseract language types:
_, self.supported = get_languages()
self.tl = threading.local()
def language_list(self, languages):
models = [c for c in alpha3(languages) if c in self.supported]
if len(models) > self.MAX_MODELS:
log.warning("Too many models, limit: %s", self.MAX_MODELS)
models = models[:self.MAX_MODELS]
models.append('eng')
return '+'.join(sorted(set(models)))
def configure_engine(self, languages, mode):
def get_api(self, languages):
if not hasattr(self.thread, 'api'):
from tesserocr import PyTessBaseAPI, PSM
api = PyTessBaseAPI(lang=languages)
api.SetPageSegMode(PSM.AUTO_OSD)
self.thread.api = api
return self.thread.api
accordingly, and annotate the angle, readingDirection and textlineOrder.
Create a corresponding image file, and reference it as AlternativeImage
in the element. Add the new image file to the workspace with the fileGrp USE
given in the second position of the output fileGrp, or ``OCR-D-IMG-DESKEW``,
and an ID based on input file and input element.
Produce a new output file by serialising the resulting hierarchy.
"""
oplevel = self.parameter['operation_level']
with PyTessBaseAPI(
path=TESSDATA_PREFIX,
lang="osd", # osd required for legacy init!
oem=OEM.TESSERACT_LSTM_COMBINED, # legacy required for OSD!
psm=PSM.AUTO_OSD
) as tessapi:
for n, input_file in enumerate(self.input_files):
file_id = input_file.ID.replace(self.input_file_grp, self.image_grp)
page_id = input_file.pageId or input_file.ID
LOG.info("INPUT FILE %i / %s", n, page_id)
pcgts = page_from_file(self.workspace.download_file(input_file))
page = pcgts.get_Page()
# add metadata about this operation and its runtime parameters:
metadata = pcgts.get_Metadata() # ensured by from_file()
metadata.add_MetadataItem(
MetadataItemType(type_="processingStep",
name=self.ocrd_tool['steps'][0],
value=TOOL,
Labels=[LabelsType(
externalModel="ocrd-tool",
def get_api(self, languages):
if not hasattr(self.thread, 'api'):
from tesserocr import PyTessBaseAPI, PSM
api = PyTessBaseAPI(lang=languages)
api.SetPageSegMode(PSM.AUTO_OSD)
self.thread.api = api
return self.thread.api
def get_ocr():
"""Check if OCR service is available; else throw an error"""
if not hasattr(settings, '_ocr'):
try:
from tesserocr import PyTessBaseAPI, PSM, OEM
log.info("Configuring OCR engine...")
settings._ocr = PyTessBaseAPI(lang=LANGUAGES,
oem=OEM.LSTM_ONLY,
psm=PSM.AUTO_OSD)
except ImportError:
log.warning("OCR engine is not available")
settings._ocr = None
return settings._ocr