Python - opencv (八) OCR

(Optical Character Recognition,光学字符识别)技术,将图片、照片上的文字内容,直接转换为文本。opencv不自带ocr,即使从cv4.4以后的external中包含cv::text识别文字,也需要用户先预装tesseract。

Tesseract是最主流的OCR开源库,安装:

1. Windows:

有Binary安装文件可以即装即用,但是只是个可执行文件不能用来开发,开发的话可以源码安装,路径https://github.com/tesseract-ocr/tesseract/,cmake以后用vs打开,但是这玩意依赖太多,没成功。

推荐方法:安装sw和vcpkg,然后命令:

.\vcpkg install tesseract:x64-windows

2. Mac:

brew install tesseract

3. Ubuntu

sudo add-apt-repository ppa:alex-p/tesseract-ocr
sudo apt-get update
sudo apt-get install tesseract-ocr 

 

安装好后可装Python库:

pip install pytesseract

 

训练集需要另外下载,地址:https://github.com/tesseract-ocr/tessdata

下载好后,Windows默认放在编译路径下的tessdata文件夹里,Mac默认路径:

 

 

测试:

图片

 

代码:

复制代码
 1 import cv2
 2 import pytesseract
 3 
 4 if __name__ == '__main__':
 5 
 6     # Read image path from command line
 7     imPath = "../pics/2.jpg"
 8 
 9     # Uncomment the line below to provide path to tesseract manually
10     # pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
11 
12     # Define config parameters.
13     # '-l eng'  for using the English language
14     # '--oem 1' for using LSTM OCR Engine
15     config = ('-l eng --oem 1 --psm 3')
16 
17     # Read image from disk
18     im = cv2.imread(imPath, cv2.IMREAD_COLOR)
19 
20     # Run tesseract OCR on image
21     text = pytesseract.image_to_string(im, config=config, output_type=pytesseract.Output.DICT)
22 
23     # Print recognized text
24     print(text)
复制代码

输出:

{'text': 'WORLD FAMOUS BRAND\nPOP FASHION\nINSTLTUTIONS OF\nOUR DREAM AND HOPE\n\x0c'}

 

posted @   Asp1rant  阅读(592)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示