Python版本
需要安装:cv2、hyperlpr
hyperlpr的安装
pip install hyperlpr -i https://pypi.tuna.tsinghua.edu.cn/simple
如果运行代码出错,跟着报错的路径,修改系列代码
该代码
mat_ = cv2.estimateRigidTransform(org_pts, target_pts, True)
用下列代码替换
mat_,inlie = cv2.estimateAffine2D(org_pts, target_pts, True)
借鉴其他博主代码进行修改的,如有侵权,请告知我会删除文章。
主要代码:
from hyperlpr import HyperLPR_plate_recognition
import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np
def car_look(image):
result=HyperLPR_plate_recognition(image)#识别车牌
#time.sleep()
if(result): #如果没识别到车牌,则不进行处理,只显示图片
car_number = result[0][0] # 车牌号码
print(car_number)
#conf = result[0][1] # 置信度
pt1=(result[0][2][0],result[0][2][1])#车牌框左上角坐标
pt2=(result[0][2][2],result[0][2][3])#车牌框右上角坐标
#绘制车牌框
cv2.rectangle(image, pt1=pt1, pt2=pt2, color=(0, 255, 0), thickness=3)
#设置需要显示的字体
fontpath = "font/simsun.ttc"
font = ImageFont.truetype(fontpath, size=30)#字体大小
image = Image.fromarray(image)
draw = ImageDraw.Draw(image)
#绘制文字信息
draw.text((pt1[0]+30,pt1[1]-30),car_number,font=font,fill=(0,0,255))
image = np.array(image)
#显示图象
cv2.imshow('image',image)
capture = cv2.VideoCapture(0)
# 打开自带的摄像头
if capture.isOpened():
# 以下两步设置显示屏的宽高
capture .set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture .set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# 持续读取摄像头数据
while True:
read_code, frame = capture.read()
if not read_code:
break
# 车牌识别函数
car_look(frame)
cv2.waitKey(1)
# 释放资源
capture.release()
cv2.destroyWindow("screen_title")