使用cython的tesseract ocr api的一个简单的、枕头友好的python包装器
ocrd-fork-tesserocr的Python项目详细描述
一个简单的,^{tt1}$友好的, 用于光学字符识别的tesseract-ocrapi包装器 (光学字符识别)。
^ STR 1 } $TestSoRCR 使用Cython直接与Tesseract的C++ API集成 它允许一个简单的pythonic和易于阅读的源代码。它 与python的^{tt3}一起使用时启用真正的并发执行$ 在tesseract中处理图像时释放gil。
tesserocr设计为^{tt1}$友好,但也可以使用 用图像文件代替。
要求
需要libtesseract(>;=3.04)和libleptonica(>;=1.71)。
在debian/ubuntu上:
$ apt-get install tesseract-ocr libtesseract-dev libleptonica-dev pkg-config
您可能需要manually compile tesseract才能获得更新的版本。注意你可能需要 更新LD_LIBRARY_PATH环境变量以指向 如果你有多个Tesseract/Leptonica装置。
安装
linux和bsd/macos
$ pip install tesserocr
安装脚本尝试检测include/library目录(如果可用,通过pkg-config),但是 可以用自己的参数覆盖它们,例如:
$ CPPFLAGS=-I/usr/local/include pip install tesserocr
或
$ python setup.py build_ext -I/usr/local/include
在Linux和BSD/MacOS上测试
窗口
建议的下载包含独立的包,其中包含执行所需的所有windows库。这意味着您的系统不需要额外安装Tesseract。
条件
您可以使用频道simonflueckiger从conda安装:
> conda install -c simonflueckiger tesserocr
或者用tesseract 4.0.0编译tesseracr:
> conda install -c simonflueckiger/label/tesseract-4.0.0-master tesserocr
管道
从simonflueckiger/tesserocr-windows_build/releases下载与windows平台和python安装相对应的wheel文件,并通过:
> pip install <package_name>.whl
用法
初始化并重新使用tesseract api实例来获得多个 图像:
fromtesserocrimportPyTessBaseAPIimages=['sample.jpg','sample2.jpg','sample3.jpg']withPyTessBaseAPI()asapi:forimginimages:api.SetImageFile(img)print(api.GetUTF8Text())print(api.AllWordConfidences())# api is automatically finalized when used in a with-statement (context manager).# otherwise api.End() should be explicitly called when it's no longer needed.
PyTessBaseAPI公开几个tesseract api方法。确保你 阅读他们的文档以获取更多信息。
使用可用助手函数的基本示例:
importtesserocrfromPILimportImageprint(tesserocr.tesseract_version())# print tesseract-ocr versionprint(tesserocr.get_languages())# prints tessdata path and list of available languagesimage=Image.open('sample.jpg')print(tesserocr.image_to_text(image))# print ocr text from image# orprint(tesserocr.file_to_text('sample.jpg'))
image_to_text和file_to_text可以与threading一起使用 同时处理多幅图像,效率高。
高级api示例
获取组件图像示例:
fromPILimportImagefromtesserocrimportPyTessBaseAPI,RILimage=Image.open('/usr/src/tesseract/testing/phototest.tif')withPyTessBaseAPI()asapi:api.SetImage(image)boxes=api.GetComponentImages(RIL.TEXTLINE,True)print('Found {} textline image components.'.format(len(boxes)))fori,(im,box,_,_)inenumerate(boxes):# im is a PIL image object# box is a dict with x, y, w and h keysapi.SetRectangle(box['x'],box['y'],box['w'],box['h'])ocrResult=api.GetUTF8Text()conf=api.MeanTextConf()print(u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, ""confidence: {1}, text: {2}".format(i,conf,ocrResult,**box))
方向和脚本检测(OSD):
fromPILimportImagefromtesserocrimportPyTessBaseAPI,PSMwithPyTessBaseAPI(psm=PSM.AUTO_OSD)asapi:image=Image.open("/usr/src/tesseract/testing/eurotext.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))
或者更简单地使用OSD_ONLY页面分段模式:
fromtesserocrimportPyTessBaseAPI,PSMwithPyTessBaseAPI(psm=PSM.OSD_ONLY)asapi:api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif")os=api.DetectOS()print("Orientation: {orientation}\nOrientation confidence: {oconfidence}\n""Script: {script}\nScript confidence: {sconfidence}".format(**os))
使用Tesseract 4+提供更多人类可读信息(演示LSTM引擎用法):
fromtesserocrimportPyTessBaseAPI,PSM,OEMwithPyTessBaseAPI(psm=PSM.OSD_ONLY,oem=OEM.LSTM_ONLY)asapi:api.SetImageFile("/usr/src/tesseract/testing/eurotext.tif")os=api.DetectOrientationScript()print("Orientation: {orient_deg}\nOrientation confidence: {orient_conf}\n""Script: {script_name}\nScript confidence: {script_conf}".format(**os))
迭代单个符号的分类器选择:
from__future__importprint_functionfromtesserocrimportPyTessBaseAPI,RIL,iterate_levelwithPyTessBaseAPI()asapi:api.SetImageFile('/usr/src/tesseract/testing/phototest.tif')api.SetVariable("save_blob_choices","T")api.SetRectangle(37,228,548,31)api.Recognize()ri=api.GetIterator()level=RIL.SYMBOLforriniterate_level(ri,level):symbol=r.GetUTF8Text(level)# r == riconf=r.Confidence(level)ifsymbol:print(u'symbol {}, conf: {}'.format(symbol,conf),end='')indent=Falseci=r.GetChoiceIterator()forcinci:ifindent:print('\t\t ',end='')print('\t- ',end='')choice=c.GetUTF8Text()# c == ciprint(u'{} conf: {}'.format(choice,c.Confidence()))indent=Trueprint('---------------------------------------------')