使用cython的tesseract ocr api的一个简单的、枕头友好的python包装器

ocrd-fork-tesserocr的Python项目详细描述


一个简单的,^{tt1}$友好的, 用于光学字符识别的tesseract-ocrapi包装器 (光学字符识别)。

TravisCI build statusLatest version on PyPiSupported python versions

^ 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装置。

^{tt6}$(>;=0.23)是构建所必需的,并且可以选择^{tt1}$来支持PIL.Image对象。

安装

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_textfile_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('---------------------------------------------')

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在EclipseIndigo上安装新的Glassfish服务器   java打印组织的最短方法是什么。w3c。多姆。文件发送到stdout?   安卓 java。lang.NullPointerException:尝试在oncreate方法中的null对象引用上调用virtual方法   linux java,我的线程无一例外地终止了,为什么?   JSON和Lombok构造函数的java问题Jackson反序列化   Spring引导升级后,java无法实例化自定义库的数据源   输入Kotlin中Java Scanner的等价物是什么?   列表vs数组作为java中递归的参数   创建名为“FilterService”的bean时发生java错误:通过字段“filterDAO”表示的未满足的依赖关系   如何在Java(基本上是Android)中将指纹图像的字节数组转换为iso 19794_2?   java如何使用基本适配器单击每个位置   java如何更新数据库显示消息“您的数据库已更新,没有任何错误,但实际上我的数据库未更新”   不同比例的安卓屏幕设备的java程序   java Android For循环,全局静态列表与本地列表