当前位置:   article > 正文

使用Python,几行代码实现OCR图片识别,附测试图片和识别效果_python实现ocr

python实现ocr

目录

1.具体如何做呢?

1.1安装Tesseract-OCR

1.2配置环境

1.3安装Tesseract中文包

1.4.安装pytesseract和pillow

2.代码和测试效果

2.1识别英文图片

2.2 识别中文图片


1.具体如何做呢?

1.1安装Tesseract-OCR

windows安装包下载地址:https://github.com/UB-Mannheim/tesseract/wiki

下载的文件名为tesseract-ocr-w64-setup-v5.2.0.20220712.exe

右击管理员运行tesseract-ocr-w64-setup-v5.2.0.20220712.exe

1.2配置环境

1.3安装Tesseract中文包

下载地址和如何安装,见另外一篇分享文章

Tesseract最新中文语言包chi_sim.traineddata(4.0.0)三种获取方式_秋9的博客-CSDN博客

1.4.安装pytesseractpillow

pip install pytesseract pip install pillow

具体操作如下:

  1. D:\1\ocr>pip install pytesseract
  2. Collecting pytesseract
  3. Downloading pytesseract-0.3.10-py3-none-any.whl (14 kB)
  4. Requirement already satisfied: Pillow>=8.0.0 in d:\python\python381\lib\site-packages (from pytesseract) (9.2.0)
  5. Collecting packaging>=21.3
  6. Downloading packaging-21.3-py3-none-any.whl (40 kB)
  7. |████████████████████████████████| 40 kB 123 kB/s
  8. Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in d:\python\python381\lib\site-packages (from packaging>=21.3->pytesseract) (2.4.7)
  9. Installing collected packages: packaging, pytesseract
  10. Attempting uninstall: packaging
  11. Found existing installation: packaging 21.0
  12. Uninstalling packaging-21.0:
  13. Successfully uninstalled packaging-21.0
  14. Successfully installed packaging-21.3 pytesseract-0.3.10
  15. WARNING: You are using pip version 21.0.1; however, version 22.3 is available.
  16. You should consider upgrading via the 'd:\python\python381\python.exe -m pip install --upgrade pip' command.
  17. D:\1\ocr>pip install pillow
  18. Requirement already satisfied: pillow in d:\python\python381\lib\site-packages (9.2.0)
  19. WARNING: You are using pip version 21.0.1; however, version 22.3 is available.
  20. You should consider upgrading via the 'd:\python\python381\python.exe -m pip install --upgrade pip' command.
  21. D:\1\ocr>

2.代码和测试效果

2.1识别英文图片

代码如下:

  1. from PIL import Image
  2. import pytesseract
  3. path="test2.png"
  4. im=Image.open(path)
  5. text=pytesseract.image_to_string(im)
  6. print(text)

test2.png图片如下:

识别效果:

结论:只有第1个字母识别错误

2.2 识别中文图片

代码如下:

  1. from PIL import Image
  2. import pytesseract
  3. path="test4.png"
  4. im=Image.open(path)
  5. text=pytesseract.image_to_string(im,lang='chi_sim')
  6. print(text)

test4.png图片如下:

识别效果:

中文识别效果比较差。

欢迎小伙伴们,留言讨论

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/650449
推荐阅读
相关标签
  

闽ICP备14008679号