【opencv】基于tesseract-OCR的文字识别检测

【opencv】基于tesseract-OCR的文字识别检测

import cv2
import pytesseract #tesseract接口
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中调用

'''
#Detecting 
pytesseract.pytesseract.tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract.exe'#调用已安装Tesseract
#img = cv2.imread('1.PNG')#检测图片路径
#img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)#修改图像色彩空间,opencv默认的彩色图像的颜色空间是BGR
# print(pytesseract.image_to_string(img))


#Detecting Words
# boxes = pytesseract.image_to_data(img)
# for a,b in enumerate(boxes.splitlines()):
        # print(b)
        # if a!=0:
            # b = b.split()
            # if len(b)==12:
                # x,y,w,h = int(b[6]),int(b[7]),int(b[8]),int(b[9])
                # cv2.putText(img,b[11],(x+120,y+20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(50,50,255),1)
                # cv2.rectangle(img, (x,y), (x+w, y+h), (50, 50, 255), 2)


#Detecting ONLY Digits
# hImg, wImg,_ = img.shape
# conf = r'--oem 3 --psm 6 outputbase digits'
# boxes = pytesseract.image_to_boxes(img,config=conf)
# for b in boxes.splitlines():
    # print(b)
    # b = b.split(' ')#转换成列表
    # print(b)
    # 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+120,y+20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(50,50,255),1)#标字




#Webcam and Screen Capture Example
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()
    #img = captureScreen()
    #DETECTING CHARACTERES
    hImg, wImg,_ = img.shape
    boxes = pytesseract.image_to_boxes(img)
    for b in boxes.splitlines():
        #print(b)
        b = b.split(' ')
        #print(b)
        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)

【opencv】基于tesseract-OCR的文字识别检测_第1张图片

【opencv】基于tesseract-OCR的文字识别检测_第2张图片

你可能感兴趣的:(opencv,opencv,计算机视觉,python)