#识别单张图片+显示+保存
#!-*-coding:utf-8-*-
import easyocr
import cv2
import time
reader=easyocr.Reader(['ch_sim','en'])
img=cv2.imread('test (1).jpg')
result=reader.readtext(img)
color=(0,0,255)
thick=3
for res in result:
print(res)
pos = res[0]
text = res[1]
for p in [(0,1),(1,2),(2,3),(3,0)]:
cv2.line(img,tuple(pos[p[0]]),tuple(pos[p[1]]),color,thick)
cv2.putText(img, text,(pos[0][0]+10,pos[0][1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
cv2.imshow('result',img)
time.sleep(10)
cv2.imwrite('bx-road-poetry.jpg',img)
#识别文件夹中的所有图片并保存
import easyocr
import cv2
import time
import os
reader=easyocr.Reader(['ch_sim','en'])
parent_path="./images/license_plate_test/"
for filename in os.listdir(parent_path):
print(filename)
path=os.path.join(parent_path,filename)
img=cv2.imread(path)
#img=cv2.imread('test (1).jpg')
result=reader.readtext(img)
color=(0,0,255)
thick=3
for res in result:
print(res)
pos = res[0]
text = res[1]
for p in [(0,1),(1,2),(2,3),(3,0)]:
cv2.line(img,tuple(pos[p[0]]),tuple(pos[p[1]]),color,thick)
cv2.putText(img, text,(pos[0][0]+10,pos[0][1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
#cv2.imshow('result',img)
#time.sleep(10)
cv2.imwrite("./reultimg/"+filename,img)
conda create easyocr
conda activate easyocr
pip install easyocr
pip install torch torchvision torchaudio
版本:
torch==1.5.1
torchvision==0.4.1
easyocr==1.4.2
注意版本问题,多次修改版本安装,以上版本可正常运行代码。
show的时候中文字符显示有问题,是OpenCV不支持中文字符的展示,需要对其转码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)