程序员开发实例大全宝库

网站首页 > 编程文章 正文

图片转文字--四种OCR工具的安装和使用

zazugpt 2024-08-22 04:39:10 编程文章 15 ℃ 0 评论

本文仅测试简单的安装和使用,

下一步应该是测试不同数据集下的检测准确率和检测效率,敬请期待。


作者的系统环境是:

笔记本:ThindPad P520

OS:win11

显卡:Quadro P520


一、EasyOCR

源码地址:https://github.com/JaidedAI/EasyOCR

1、安装anaconda

为什么要安装anaconda?

因为我日常开发用python3.11的版本,使用YOLOv8需要python3.8的环境,这次使用EasyOCR需要python3.10的环境,需要切换python环境。

anaconda的安装和配置,请参考《YOLOv8入门篇--YOLOv8的安装和使用》。


2、创建虚拟环境(指定python3.10)

查看已有的conda虚拟环境

切换到创建好的虚拟环境

conda create -n testOCR python=3.10
conda env list
conda activate testOCR

或者在pycharm开发环境下,选择conda+python3.10创建虚拟环境,并命名为testOCR,效果一样。


3、安装EasyOCR

 pip install easyocr


4、写代码测试

import easyocr
reader = easyocr.Reader(['ch_sim','en']) # this needs to run only once to load the model into memory
result = reader.readtext('img.png')
print(result)


二、CnOCR

源码地址:https://github.com/breezedeus/CnOCR

1、安装anaconda

同上,略过。


2、创建虚拟环境(指定python3.10)

查看已有的conda虚拟环境

切换到创建好的虚拟环境

conda create -n testCNOCR python=3.10
conda env list
conda activate testCNOCR

或者在pycharm开发环境下,选择conda+python3.10创建虚拟环境,并命名为testCNOCR,效果一样。


3、安装CnOCR

pip install cnocr

碰到如下错误:

Failed to build Polygon3
ERROR: Could not build wheels for Polygon3, which is required to install pyproject.toml-based projects


3.1、试试源码安装

git clone https://github.com/breezedeus/CnOCR
cd CnOCR
pip install -r requirement.txt


出现同样的错误,参考这篇文章:

https://wenku.csdn.net/answer/1pak5c8m9v

?和

https://github.com/PaddlePaddle/PaddleOCR/issues/9668

?

需要安装一下:

https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/

?这次通过了,

Successfully built polygon3


4、写代码测试

from cnocr import CnOcr

img_fp = './images/img.png'
ocr = CnOcr() # 所有参数都使用默认值
out = ocr.ocr(img_fp)

print(out)


三、Tesseract

Tesseract官方仓库:https://github.com/tesseract-ocr/tesseract

Tesseract是用C++进行开发的,因此如果要在python中进行使用,需要借助第三方依赖pytesseract


首先需要在本机上安装Tesseract

安装包下载地址:https://digi.bib.uni-mannheim.de/tesseract/

安装过程可参考:https://blog.csdn.net/weixin_51571728/article/details/120384909

?安装过程记得在“Additional languange data(download)”中,选上中文相关的4种语言,如下面两图:


配置完成后,在命令行输入tesseract -v打印出版本信息则表示安装成功。


1、安装anaconda

同上,略过。


2、创建虚拟环境(指定python3.10)

查看已有的conda虚拟环境

切换到创建好的虚拟环境

conda create -n testTEOCR python=3.10
conda env list
conda activate testTEOCR

或者在pycharm开发环境下,选择conda+python3.10创建虚拟环境,并命名为testTEOCR,效果一样。


3、安装pytesseract

pip install pytesseract


4、写代码测试

import pytesseract
from PIL import Image


def demo():
    # 重要!!!
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    # 打开要识别的图片
    image = Image.open('./images/img.png')
    # 使用pytesseract调用image_to_string方法进行识别,传入要识别的图片,lang='chi_sim'是设置为中文识别,
    text = pytesseract.image_to_string(image, lang='chi_sim')

    # 输入所识别的文字
    print(text)


if __name__ == '__main__':
   demo()


四、PaddleOCR

PaddleOCR是百度旗下的产品,目前已经迭代到第四版。

PaddleOCR官方仓库:https://github.com/PaddlePaddle/PaddleOCR


1、安装anaconda

同上,略过。


2、创建虚拟环境(指定python3.10)

查看已有的conda虚拟环境

切换到创建好的虚拟环境

conda create -n testPAOCR python=3.10
conda env list
conda activate testPAOCR

或者在pycharm开发环境下,选择conda+python3.10创建虚拟环境,并命名为testPAOCR,效果一样。


3、安装PaddleOCR

pip install PaddleOCR


4、写代码测试

import cv2
from paddleocr import PaddleOCR

if __name__ == '__main__':
    ocr = PaddleOCR(use_angle_cls=True, lang="ch", ocr_version='PP-OCRv4')
    image_input_fullname = 'images/img.png'
    img = cv2.imread(image_input_fullname)
    result = ocr.ocr(img, cls=True)
    print(result)


运行代码,提示缺少paddle

安装paddle,又提示缺少xx。

罗列如下:


pip install paddle
pip install common
pip install dual
pip install tight
pip install data
pip install prox


再次运行,碰到如下错误:

ModuleNotFoundError: No module named 'paddle.distributed'

pip install paddlepaddle

再次运行,下载模型,输出检测结果。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表