import cv2标签:字符识别,img,int,cv2,yimg,pytesseract,tesseract From: https://www.cnblogs.com/lld76/p/15995190.html
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r'E:\tesseract\tesseract.exe'
#字符识别(英文,数字)
# img = cv2.imread('img_1.png')
# img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# yimg,ximg,_ = img.shape #(y,x)
# # print(pytesseract.image_to_string(img)) #内容
# # print(pytesseract.image_to_boxes(img)) #字符位置
# boxes = pytesseract.image_to_boxes(img)
# for i in boxes.splitlines():
# i = i.split(' ')
# print(i)
# x,y,w,h = int(i[1]),int(i[2]),int(i[3]),int(i[4])
# cv2.rectangle(img,(x,yimg-y),(w,yimg-h),(0,0,255),1)
# cv2.putText(img,i[0],(x,yimg-y),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
# cv2.imshow('result',img)
# cv2.waitKey(0)
#字符识别(单词)
# pytesseract.pytesseract.tesseract_cmd=r'E:\tesseract\tesseract.exe'
# img = cv2.imread('img_1.png')
# img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# yimg,ximg,_ = img.shape #(y,x)
# boexs = pytesseract.image_to_data(img)
# print(boexs)
#第一种方法
# for x,i in enumerate(boexs.splitlines()):
# if x != 0:
# i = i.split()
# print(i)
#第二种方法
# for i in boexs.splitlines()[1::]:
# i = i.split()
# print(i)
# if len(i) == 12:
# x,y,w,h = int(i[6]),int(i[7]),int(i[8]),int(i[9])
# cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
# cv2.putText(img,i[11],(x,y),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
# cv2.imshow('result',img)
# cv2.waitKey(0)
#字符识别(数字)
# pytesseract.pytesseract.tesseract_cmd=r'E:\tesseract\tesseract.exe'
# img = cv2.imread('img_1.png')
# img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# yimg,ximg,_ = img.shape #(y,x)
# cong = r'--oem 3 --psm 6 outputbase digits'
# boxes = pytesseract.image_to_boxes(img,config=cong)
# for i in boxes.splitlines():
# i = i.split(' ')
# x,y,w,h = int(i[1]),int(i[2]),int(i[3]),int(i[4])
# cv2.rectangle(img,(x,yimg-y),(w,yimg-h),(0,0,255),1)
# cv2.putText(img,i[0],(x,yimg-y),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
# cv2.imshow('result',img)
# cv2.waitKey(0)
#中文识别
from PIL import Image
img = Image.open('../img_5.png')
text = pytesseract.image_to_string(img,lang='chi_sim')#config='psm - 6'
print(text)