用于tesseract ocr和google vision ocr的python包装器
gpyocr的Python项目详细描述
python包装器,用于Tesseract OCR和Google Vision OCR对图像执行ocr并获取结果的置信值。
这两个ocr引擎都是谷歌的产品。Tesseract是一个开源软件,需要进行一些调整才能获得良好的效果,尤其是在文本定义不明确的图像上。google vision ocr引擎是一个性能更好的商业产品,允许您跳过图像的预处理工作。
仅支持python>;=3.6。
用法
gpyocr模块有两个主要功能:
- tesseract_ocr(image, lang='', psm=None, config=''):它返回一个元组 通过tesseract获得(text,confidence)。参数相同 第command-line Tesseract tool 除了输出文件。
- google_vision_ocr(image, langs=None):它返回一个元组 (text,confidence)通过google vision api获得。参数 是在OCR过程中要查找的语言的列表。更多信息 关于支持的语言,请参见 this page
参数image可以是:
- 包含图像文件路径的字符串
- numpy对象(opencv)
- 图像对象(枕头/枕头)
有可能获得一些关于Tesseract和Google Vision的信息 系统中找到的版本具有get_tesseract_version()和 ^分别是{tt6}$。
软件包的安装还提供了一个命令行工具,请运行
$ gpyocr --help
更多信息。
示例
阅读图像中文本的示例tests/resources/european-test.png 在这个仓库里。
>>>importgpyocr>>>gpyocr.tesseract_ocr('tests/resources/european-test.png')('The (quick) [brown] {fox} ... ',87.13636363636364)>>>gpyocr.google_vision_ocr('tests/resources/european-test.png')('The (quick) [brown] {fox} ... ',98.00000190734863)>>>importcv2# support for OpenCV library>>>image=cv2.imread('tests/resources/european-test.png')>>>gpyocr.tesseract_ocr(image)('The (quick) [brown] {fox} ... ',87.13636363636364)>>>fromPILimportImage# support for Pillow library>>>image=Image.open('tests/resources/european-test.png')>>>gpyocr.tesseract_ocr(image)('The (quick) [brown] {fox} ... ',87.13636363636364)>>>gpyocr.tesseract_ocr('tests/resources/european-test.png'),lang='ita',psm=7,config='tessedit_char_whitelist=abc',)('bc aa cb b c a ... ',18.5)>>>gpyocr.google_vision_ocr('tests/resources/european-test.png',langs=['en','it'])('The (quick) [brown] {fox} ... ',87.13636363636364)
请参阅单元测试以获取更多示例。
安装
gpyocr是python包索引中提供的pip包。 要在python环境中安装它,请运行:
$ pip install gpyocr
如果要使用gpyocr运行tesseract,必须在 系统。为了得到置信值,gpyocr需要tesseract>;=3.05。 您可以使用bash脚本安装tesseract 您可以在存储库中找到。如果您想要Tesseract 3.05(建议的版本) 然后运行:
$ sudo ./tesseract_installer.sh 3.05
如果要尝试Tesseract 4.1.0,请运行:
$ sudo ./tesseract_installer.sh 4.1.0
note:这个bash脚本在ubuntu 18.04和centos 7中测试;如果您 如果没有这些系统,则可能在安装某些依赖项时出现问题。 您可以自行安装所有依赖项,然后运行:
$ sudo ./tesseract_installer.sh 3.05 --no-dependencies
要使用google cloud vision api,您必须使用
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
请参考 their documentation用于 有关安装和使用谷歌云视觉服务的更多信息。
许可证
阿帕奇2.0
测试
首先,安装pytest、pytest-cov和pytest-mock。你可以 用pip install -r requirements.txt安装它们。
转到此存储库的根目录。
要在不使用ocr库的情况下运行单元测试,请运行:
$ pytest
要使用ocr库运行单元测试,请运行:
$ pytest --nomock
要获取覆盖率报告,请运行
$ pytest --cov --cov-report term-missing
要只测试tesseract函数,请运行:
$ pytest -m tesseract
要只测试google cloud vision功能,请运行:
$ pytest -m googlevision
如何贡献
此项目是为特定应用程序开发的,因此它不是 非常多才多艺。如果你想有新的功能或者 问题,请随时通过电子邮件与我联系或在此处打开问题 github。