• 关于OCR文字识别的坑(64位Python专属)


    PaddleOCR文字识别

    这里主要用到PaddleOCR,PaddleOCR是一套丰富、领先、且实用的OCR工具库,其主要特性如下:

    1. 支持中英文数字组合识别、竖排文本识别、长文本识别
    2. 支持多语言识别:韩语、日语、德语、法语
    3. 支持用户自定义训练,提供丰富的预测推理部署方案
    4. 支持PIP快速安装使用
    5. 可运行于Linux、Windows、MacOS等多种系统

    注意事项:PaddleOCR不支持32位的Python,因此安装过程会出现各种错误。

    1.安装paddlepaddle

    paddlepaddle是运行PaddleOCR库的依赖库,如果缺少这个库,使用PaddleOCR时会报错。

    需要确认 Python 和 pip 是 64bit,才可以安装paddlepaddle。很遗憾,32位的Python无法使用这个库。

    因此,放弃使用PaddleOCR。

    pip install paddlepaddle
    
    • 1

    下载速度超慢,需要耐心等待。

    也可以使用国内的镜像源进行安装,速度要快很多。

    https://pypi.tuna.tsinghua.edu.cn/simple   #清华
    http://mirrors.aliyun.com/pypi/simple/       #阿里云
    https://pypi.mirrors.ustc.edu.cn/simple/    #中国科技大学
    http://pypi.hustunique.com/                   #华中理工大学
    http://pypi.sdutlinux.org/                     #山东理工大学
    http://pypi.douban.com/simple/            #豆瓣       
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在使用pip命令的时候,可使用 -i 参数加上更新源地址,以完成安装和更新。例如(表示更改为清华镜像更新源,不在使用官方更新源):

    pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    如果pip命令出错或者版本太旧,可以尝试更新pip。

    更新后的pip,命令行里的文件大小、下载速度和时间,都是用彩色字体显示,更好看了。

    python -m pip install --upgrade pip
    
    • 1

    2.安装shapely

    windows环境下不要直接使用pip install shapely安装这个库,通过这种方式安装好后在实际使用中会出现:[winRrror 126] 找不到指定模块的问题

    最好直接这里下载whl文件,在本地安装。

    在下载好的文件,所在的文件夹的地址栏里,输入cmd,回车,进入到命令窗口。

    然后使用 pip install XXX 命令即可本地安装。例如:

    pip install Shapely-1.8.2-cp39-cp39-win32.whl
    
    • 1

    安装过程,可能会出现错误:

    ERROR: Shapely-1.8.2-cp39-cp39-win32.whl is not a supported wheel on this platform.
    
    • 1

    可以使用 pip debug --verbose 命令来查看自己电脑支持的安装标签,然后修改whl文件的名字后,再重复上面的安装步骤即可。

    # 原本的名字安装出现错误
    pip install Shapely-1.8.2-cp38-cp38-win32.whl
    # 修改文件名字后成功安装
    pip install Shapely-1.8.2-cp39-cp39-win_amd64.whl
    # 原因:我的电脑不支持win32标签文件
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3. 安装PaddleOCR

    安装好前面两个库以后,就可以安装PaddleOCR了。

    这里需要指定安装的版本,否则会出错。

    pip install paddleocr==2.6.0.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1
    不指定PaddleOCR版本,安装时遇到的错误
    pip install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    报错如下:

    Failed to build lanms-neo Polygon3
    ERROR: Could not build wheels for lanms-neo, which is required to install pyproject.toml-based projects
    
    • 1
    • 2
    Building wheels for collected packages: lanms-neo, Polygon3
      Building wheel for lanms-neo (PEP 517) ... error
      ERROR: Command errored out with exit status 1:
       command: 'A:\PythonProgram\Anaconda\python.exe' 'A:\PythonProgram\Anaconda\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\caogu\AppData\Local\Temp\tmpigq57uv9'
           cwd: C:\Users\caogu\AppData\Local\Temp\pip-install-sjq2dc4v\lanms-neo_79e74858cc95414ba834bbff35d2bac9
      Complete output (10 lines):
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-cpython-38
      creating build\lib.win-amd64-cpython-38\lanms
      copying lanms\__init__.py -> build\lib.win-amd64-cpython-38\lanms
      running build_ext
      building 'lanms._C' extension
      error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
      ----------------------------------------
      ERROR: Failed building wheel for lanms-neo
      Building wheel for Polygon3 (setup.py) ... error
      ERROR: Command errored out with exit status 1:
       command: 'A:\PythonProgram\Anaconda\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\caogu\\AppData\\Local\\Temp\\pip-install-sjq2dc4v\\polygon3_ec6a30f73d3a49fc936f2bc3e12f5bcb\\setup.py'"'"'; __file__='"'"'C:\\Users\\caogu\\AppData\\Local\\Temp\\pip-install-sjq2dc4v\\polygon3_ec6a30f73d3a49fc936f2bc3e12f5bcb\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\caogu\AppData\Local\Temp\pip-wheel-uf9nqjw3'
           cwd: C:\Users\caogu\AppData\Local\Temp\pip-install-sjq2dc4v\polygon3_ec6a30f73d3a49fc936f2bc3e12f5bcb\
      Complete output (14 lines):
      Using NumPy extension!
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-3.8
      creating build\lib.win-amd64-3.8\Polygon
      copying Polygon\IO.py -> build\lib.win-amd64-3.8\Polygon
      copying Polygon\Shapes.py -> build\lib.win-amd64-3.8\Polygon
      copying Polygon\Utils.py -> build\lib.win-amd64-3.8\Polygon
      copying Polygon\__init__.py -> build\lib.win-amd64-3.8\Polygon
      running build_ext
      building 'Polygon.cPolygon' extension
      error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
      ----------------------------------------
      ERROR: Failed building wheel for Polygon3
      Running setup.py clean for Polygon3
    Failed to build lanms-neo Polygon3
    ERROR: Could not build wheels for lanms-neo which use PEP 517 and cannot be installed directly
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    找了很多资料,尝试了各种方法:

    • 更新vc++2022的运行库文件
    • 单独安装lanms-neo
    • 更新pip

    都不能解决,依旧出错。

    最后,下载solved_packageMissInInstalling_mu_visual_cpp_build_tools_2015_update_3_x64_dvd_dfd9a39c.iso文件,并安装。

    还是出错:

      ……
      note: This error originates from a subprocess, and is likely not a problem with pip.
      ERROR: Failed building wheel for lanms-neo
    Failed to build lanms-neo
    ERROR: Could not build wheels for lanms-neo, which is required to install pyproject.toml-based projects
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后进到doc/doc_ch/installation.md · develop · mirrors / paddlepaddle / paddleocr · GitCode,发现可能需要安装以来的库。

    进入https://gitee.com/paddlepaddle/PaddleOCR下载PaddleOCR压缩文件。

    下载完成后解压缩,放到 \Anaconda\Lib\site-packages\ 文件夹内。然后在解压的 PaddleOCR-release-2.6 文件内,打开cmd,安装以来的库。

    # 安装依赖的库
    python3 -m pip install -r requirements.txt
    
    • 1
    • 2

    requirements.txt内容如下:

    shapely
    scikit-image
    imgaug
    pyclipper
    lmdb
    tqdm
    numpy
    visualdl
    rapidfuzz
    opencv-python
    opencv-contrib-python
    cython
    lxml
    premailer
    openpyxl
    attrdict
    Polygon3
    lanms-neo==1.0.2
    PyMuPDF==1.19.0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    建议使用国内的镜像源逐个安装,外国的源实在是太慢了。

    安装到lanms-neo==1.0.2这个包时,出现了上述的错误。

    最后解决的方式:指定安装2.6.0.2版本,成功安装。
    pip install paddleocr==2.6.0.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    PaddleOCR代码示例:

    from paddleocr import PaddleOCR, draw_ocr
    # Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换
    # 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。
    ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
    img_path = 'PaddleOCR/doc/imgs/11.jpg'
    result = ocr.ocr(img_path, cls=True)
    for line in result:
        print(line)
    
    # 显示结果,对结果可视化
    from PIL import Image
    image = Image.open(img_path).convert('RGB')
    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    im_show = draw_ocr(image, boxes, txts, scores, font_path='/path/to/PaddleOCR/doc/simfang.ttf')
    im_show = Image.fromarray(im_show)
    im_show.save('result.jpg')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    结果是一个list,每个item包含了文本框,文字和识别置信度。

    [[[24.0, 36.0], [304.0, 34.0], [304.0, 72.0], [24.0, 74.0]], ['纯臻营养护发素', 0.964739]]
    [[[24.0, 80.0], [172.0, 80.0], [172.0, 104.0], [24.0, 104.0]], ['产品信息/参数', 0.98069626]]
    [[[24.0, 109.0], [333.0, 109.0], [333.0, 136.0], [24.0, 136.0]], ['(45元/每公斤,100公斤起订)', 0.9676722]]
    ......
    
    • 1
    • 2
    • 3
    • 4

    EasyOCR文字识别

    Python中有一个不错的OCR库:EasyOCR。

    它可以在python中调用,用来识别图像中的文字,并输出为文本。

    安装命令

    pip install easyocr
    
    • 1

    注意事项:PaddleOCR不支持32位的Python,因此安装过程会出现各种错误。

    直接安装easyocr,发现会报错。那么先安装它的依赖库:Scipy。64位的Python安装并没有出现错误。

    安装Scipy库

    32位才需要单独安装,64位在pip install easyocr命令里就安装了。

    Scipy库是一个用于数学、科学、工程领域的常用软件包,可以处理插值、积分、优化、图像处理、常微分方程数值解的求解、信号处理等问题。它用于有效计算Numpy矩阵,使Numpy和Scipy协同工作,高效解决问题。

    直接安装会报错,于是下载SciPy-1.8.1-cp38-cp38-win32.whl文件,本地安装。

    下载地址:Archived: Python Extension Packages for Windows - Christoph Gohlke (uci.edu)

    使用方法

    EasyOCR的用法非常简单,分为三步:

    • 1.创建识别对象;
    • 2.读取并识别图像;
    • 3.导出文本。

    安装好后,试着运行了一下,发现对中文的识别不是太准确。准确率比不上PaddleOCR。

    # 导入easyocr
    import easyocr
    # 没有找到CUDA是因为在安装Pytorch时没有选择安装CUDA。
    
    # 路径不能有中文,文件名称不能有中文
    # 创建reader对象
    # ch_sim(简体中文)、en(英文)
    reader = easyocr.Reader(['ch_sim','en'])  
    # 读取图像
    # img = r'L:\test\test.png'
    img = r'L:\test\test.bmp'
    # img = r'L:\Python调用大漠插件\test.png'
    result = reader.readtext(img)
    for i in result:
        print(i)
        print('——'*50)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    识别的结果包含在[元组]里,元组由三部分组成:边框坐标、文本、识别概率。

    runfile('L:/Python调用大漠插件/easyOCR   测试.py', wdir='L:/Python调用大漠插件')
    CUDA not available - defaulting to CPU. Note: This module is much faster with a GPU.
    ([[29, 5], [145, 5], [145, 21], [29, 21]], 'Spyder (Pthon 3.8]', 0.71470388862037)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[11, 29], [500, 29], [500, 49], [11, 49]], '文件(月  编辑(]  查找(5)  源代码(C)  运行(R)  调试[D)  控制台(0]  项目(P)  工具C 查看0', 0.03665007207665919)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[82, 60], [180, 60], [180, 90], [82, 90]], '罔  召 v', 0.005002305188916934)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[290, 60], [428, 60], [428, 86], [290, 86]], '9 哑 C M', 0.01261425011100126)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[436, 60], [462, 60], [462, 84], [436, 84]], 'R', 0.045295749291515364)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[11, 93], [207, 93], [207, 111], [11, 111]], '工:  yt1or调周用太漠插伴 :未命名2.r', 0.0019529638911147207)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[31, 115], [85, 115], [85, 133], [31, 133]], '习则试 r', 0.07507313989111337)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[110, 114], [211, 114], [211, 134], [110, 134]], '解挪窗口  天刀 e', 0.03494765837455324)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[235, 113], [307, 113], [307, 133], [235, 133]], '自动押镖: I', 0.2134713275573948)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[333, 115], [451, 115], [451, 133], [333, 133]], '天刀寻径脚本界面: n', 0.33952645549954036)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    ([[475, 113], [500, 113], [500, 131], [475, 131]], '押镖', 0.9046226769301282)
    ————————————————————————————————————————————————————————————————————————————————————————————————————
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    多目标优化算法:多目标霸王龙优化算法(MOTROA)MATLAB
    【Netty】1-3. Netty急速入门 - 编码实现(java)
    基于JAVA的图书借阅管理平台【数据库设计、源码、开题报告】
    【腾讯云 HAI域探秘】浅尝一番AI绘画
    后台管理---删除功能
    冷门黑科技软件推荐
    【电路笔记】-快速了解无源器件
    ch0_OSI 七层网络协议介绍
    Ai版式设计类型 优漫动游
    契约锁电子签助力拍卖业务网上签约,保全证据、不可抵赖,成交快
  • 原文地址:https://blog.csdn.net/qq_37944726/article/details/127942521