【opencv】基于tesseract-OCR的文字识别检测
import cv2
import pytesseract
import numpy as np
from PIL import ImageGrab
import time
'''
Tesseract
一款由HP实验室开发由Google维护的开源OCR(Optical Character Recognition , 光学字符识别)引擎,与Microsoft Office Document Imaging(MODI)相比,我们可以不断的训练的库,使图像转换文本的能力不断增强;如果团队深度需要,还可以以它为模板,开发出符合自身需求的OCR引擎。
源码地址为:https://github.com/tesseract-ocr/tesseract;
tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/
Tesseract已经有多个语言的版本:
C#版本:https://github.com/charlesw/tesseract
Java版本:https://github.com/bytedeco/javacpp-presets/tree/master/tesseract
Python版本:https://github.com/sirfz/tesserocr
PHP版本:https://github.com/thiagoalessio/tesseract-ocr-for-php
Tesseract的其他语言版本见:https://github.com/tesseract-ocr/tesseract/wiki/AddOns#tesseract-wrappers
安装时注意添加环境变量,可选择等多种语言
Tesseract的使用
方式一:直接在命令行调用:
tesseract d:\6.png d:\result
方式二:在Python中调用
'''
pytesseract.pytesseract.tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract.exe'
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
def captureScreen(bbox=(300,300,1500,1000)):
capScr = np.array(ImageGrab.grab(bbox))
capScr = cv2.cvtColor(capScr, cv2.COLOR_RGB2BGR)
return capScr
while True:
timer = cv2.getTickCount()
_,img = cap.read()
hImg, wImg,_ = img.shape
boxes = pytesseract.image_to_boxes(img)
for b in boxes.splitlines():
b = b.split(' ')
x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 2)
cv2.putText(img,b[0],(x,hImg- y+25),cv2.FONT_HERSHEY_SIMPLEX,1,(50,50,255),2)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
cv2.putText(img, str(int(fps)), (75, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (20,230,20), 2);
cv2.imshow("Result",img)
cv2.waitKey(1)
cv2.imshow('img', img)
cv2.waitKey(0)