OCR模型调研及详细安装

OCR模型调研及详细安装

1 搭建 Tesseract-OCR 环境。

1.1 注意需先手动安装Tesseract-OCR, 下载地址:https://digi.bib.uni-mannheim.de/tesseract/?C=M;O=D

复制代码
注意:安装的时候选中中文包(安装时把所有选项都勾上)。
安装磁盘选择与运行的代码在同一磁盘。
安装 Tesseract-OCR 后,需将 Tesseract-OCR 对应的安装路径添加到系统环境变量中。

安装完成后,使用命令,查看版本号和支持语言:
cd C:\Program Files\Tesseract-OCR  
tesseract -v tesseract --list-langs -v tesseract --list-langs 

若有语言方面的Error,需将中文包 chi_sim.traineddata 下载到本地C:\Program Files\Tesseract-OCR 路径下。(见1.3下载语言包)

1.2 再安装python库pytesseract

复制代码
pip install pytesseract

1.3 下载语言包,并放到Tesseract的目录下

复制代码
下载地址:https://github.com/tesseract-ocr/tesseract/wiki/Data-Files
        https://tesseract-ocr.github.io/tessdoc/Data-Files

1.4 代码块

复制代码
def tesseract_to_str(image_path):
    """Tesseract-OCR: 提取图片中的文字,返回 text字符串"""
    from PIL import Image
    import pytesseract
    import os

    if not os.path.isfile(image_path):
        logging.info('          路径存在问题,请检查image_path: '.format(image_path))
        return ''
    image = Image.open(image_path)
    # 如果没有将tesseract的安装目录添加到系统环境变量中,则需要指定安装路径,
    pytesseract.pytesseract.tesseract_cmd = r"D:\Program_Files\Tesseract-OCR\tesseract.exe"
    testdata_dir_config = '--tessdata-dir D:/Program_Files/Tesseract-OCR/tessdata'
    # 调用pytesseract库提取文字,识别中文需指定语言lang='chi_sim'
    print('-'*20,'获取图中的文字','-'*20)
    try:
        text_from_image = pytesseract.image_to_string(image,  config=testdata_dir_config, lang='chi_sim')
    except Exception as e:
        logging.info('          识别文字失败:{} '.format(e))
        return ''
    # print('-' * 20, '获取图中的文字完成', '-' * 20)
    # print('text_from_tesseract: \n', text_from_image)
    return text_from_image

2 EasyOCR: 是一个基于 PyTorch 的 OCR 库。

复制代码
pip install easyocr

源码

复制代码
https://github.com/JaidedAI/EasyOCR
API详解见:https://blog.csdn.net/yohnyang/article/details/130300923

模型储存路径:

复制代码
windows: C:\Users\username\.EasyOCR\
linux:/root/.EasyOCR/

代码

复制代码
def easyocr_to_str(image_path):
    import easyocr
    # import os
    # os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

    # reader = easyocr.Reader(['ch_sim','en'], gpu = False)
    print('result:1 \n', )
    reader = easyocr.Reader(['ch_sim',], gpu = False)
    print('result:2 \n', )
    result = reader.readtext(image_path)
    print('result: \n', result)
    for detection in result:
        print(detection[1])

问题

复制代码
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

修改:

网友说的方法:

添加如下代码

复制代码
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

然而没用,然后修改了自己环境中的如下内容之后,不再报错了,即使将上述os内容注释,也不报错。

复制代码
D:\ProgramFiles\miniconda3\envs\env_myenv\Library\bin路径下的libiomp5md.dll改为libiomp5md.dll.bk

识别文本示例:

复制代码

3 Keras-OCR

源码

复制代码
https://gitcode.com/gh_mirrors/ke/keras-ocr/overview?utm_source=artical_gitcode&index=top&type=card&webUrl

安装

复制代码
安装 :keras-ocr支持Python >= 3.6和TensorFlow >= 2.0.0。
方法1: 从主分支安装
pip install git+https://github.com/faustomorales/keras-ocr.git#egg=keras-ocr
方法2: 从PyPi安装
pip install keras-ocr

4 Doctr 识别文档中的文本区域、图像和表格

项目地址

复制代码
https://gitcode.com/gh_mirrors/do/doctr/overview?utm_source=artical_gitcode&index=top&type=card&webUrl&isLogin=1

安装

复制代码
pip install "python-doctr[torch]"

首次运行会下载模型,存储在

复制代码
C:\Users\hlj\.cache\doctr\models\db_resnet50-79bd7d70.pt
C:\Users\hlj\.cache\doctr\models\crnn_vgg16_bn-9762b0b0.pt

缺点

复制代码
不支持中文模型
相关推荐
sali-tec1 天前
C# 基于halcon的视觉工作流-章70 深度学习-Deep OCR
开发语言·人工智能·深度学习·算法·计算机视觉·c#·ocr
云老大TG:@yunlaoda3602 天前
腾讯云国际站代理商的OCR有什么优势呢?
人工智能·ocr·腾讯云
翔云 OCR API3 天前
API让文档信息“活”起来:通用文档识别接口-开发者文字识别API
前端·数据库·人工智能·mysql·ocr
Chunyyyen4 天前
【第二十六周】OCR学习01
学习·计算机视觉·ocr
恶猫5 天前
STranslate 翻译 工具 v2.0.0 绿色便携版 翻译、OCR工具
ocr·文字识别·自动翻译·翻译·划词翻译·截图翻译
垦***耪5 天前
台达 DVP ES2 与 3 台英威腾 GD 通讯程序(TDES - 12)开发实战
ocr
MonkeyKing_sunyuhua6 天前
使用ARQ做PDF OCR和 图片OCR的任务的方案
pdf·ocr
kevin 16 天前
合同盖章前,如何比对差异,确保纸质版与电子版100%一致?
人工智能·自动化·ocr
AI人工智能+6 天前
授权委托书识别技术:利用深度学习和NLP实现纸质文档的智能解析
ocr·文档抽取·授权委托书识别
大强同学6 天前
ShareX - 错误:英语 language is not available in this system for OCR.
ocr