步骤一:下载hyperlpr包
pip install hyperlpr -i https://pypi.tuna.tsinghua.edu.cn/simple/
步骤二:创建sourceai/model/ocr/custom/car_ocr.py文件,代码如下:
# 导入包
from hyperlpr import *
# 导入OpenCV库
import cv2
def start(img):
# 读入图片
image = cv2.imread(img)
# 识别结果
res = HyperLPR_plate_recognition(image)
print(res)
if __name__ == '__main__':
start("car01.jpg")
步骤三:将car01.jpg图片放到car_ocr.py同级目录
步骤四:运行car_ocr.py查看结果
注:如果报错如下
mat_ = cv2.estimateRigidTransform(org_pts, target_pts, True)
AttributeError: module 'cv2' has no attribute 'estimateRigidTransform'
解决方案:
修改hyperlpr.py文件,大概在231行将mat_ = cv2.estimateRigidTransform(org_pts, target_pts, True)
修改为
mat_,inlier = cv2.estimateAffine2D(org_pts, target_pts)
其中寻找hyperlpr源代码方式如下,按ctrl键同时点击下图hyperlpr
按ctrl键同时点击下图.hyperlpr
注释231行,新增232行代码
步骤五:修改支持多个车牌检测,找到hyperlpr.py源码文件,定位到65行,将return cropped_images代码与for对齐,修改前:
修改后:
步骤六:将含有多个车牌的car01.jpg图片放置到car_ocr.py同级目录
步骤七:car_ocr.py主函数中添加代码如下:
start("carmul.jpg")
全部代码参考:
# 导入包
from hyperlpr import *
# 导入OpenCV库
import cv2
def start(img):
# 读入图片
image = cv2.imread(img)
# 识别结果
res = HyperLPR_plate_recognition(image)
print(res)
return res
def distinguish(img):
datas = []
# 读入图片
image = cv2.imread(img)
# 识别结果
res = HyperLPR_plate_recognition(image)
for item in res:
datas.append(item[0])
print(datas)
return datas
def distinguish_folder(folder):
dic_datas = {}
filenames = os.listdir(folder)
files = [os.path.join(folder, file) for file in filenames]
for img in files:
filename = os.path.basename(img)
datas = []
# 读入图片
image = cv2.imread(img)
# 识别结果
res = HyperLPR_plate_recognition(image)
for item in res:
if float(item[1]) > 0.95:
datas.append(item[0])
if datas:
dic_datas[filename] = datas
print(filename,'识别成功', datas)
else:
print(filename,'未识别到车牌')
print(dic_datas)
return dic_datas
def distinguish_list(lists):
dic_datas = {}
for img in lists:
filename = os.path.basename(img)
datas = []
# 读入图片
image = cv2.imread(img)
# 识别结果
res = HyperLPR_plate_recognition(image)
for item in res:
if float(item[0][1]) > 0.95:
datas.append(item[0])
if datas:
dic_datas[filename] = datas
print(dic_datas)
return dic_datas
if __name__ == '__main__':
# start("car01.jpg")
# start("video_1.png")
# distinguish("carmul.jpg")
start("carmul.jpg")
步骤八:运行car_ocr.py代码如下: