2021SC@SDUSC
概述
本篇是对该识别系统主要使用到的函数SimpleRecognizePlateByE2E(image)、SimpleRecognizePlateByE2E(self,image)、SimpleRecognizePlate()进行分析总结
SimpleRecognizePlateByE2E(image)函数
def SimpleRecognizePlateByE2E(image):
t0 = time.time()
images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
res_set = []
for j,plate in enumerate(images):
plate, rect, origin_plate =plate
plate =cv2.resize(plate,(136,36*2))
res,confidence = e2e.recognizeOne(origin_plate)
print "res",res
t1 = time.time()
ptype = td.SimplePredict(plate)
if ptype>0 and ptype<5:
plate = cv2.bitwise_not(plate)
image_rgb = fm.findContoursAndDrawBoundingBox(plate)
image_rgb = fv.finemappingVertical(image_rgb)
image_rgb = fv.finemappingVertical(image_rgb)
cache.verticalMappingToFolder(image_rgb)
res,confidence = e2e.recognizeOne(image_rgb)
print res,confidence
res_set.append([[],res,confidence])
if confidence>0.7:
image = drawRectBox(image, rect, res+" "+str(round(confidence,3)))
return image,res_set
该函数用于粗略地初始化图片,并传给images对象,其中上下填充率默认为0.1
首先是打印出来一个传入image的shape值也就是图片的大小,针对上下填充率进行判断,高于0.2时报错。再将图片的高度,内边距,宽高比进行赋值。
再用resize对图片进行裁剪操作。用cvtColor来把图片转变为一个黑白的图片。最后使用了级联分类器进行模式识别,用于提取图片车牌特征。detectMultiScale是cv2中进行模式识别的函数,它能够对输入的图片进行识别,识别出所有待识别模式(在这里是车牌)的位置坐标和大小,将其用矩vector表示并返回,存储在watches。初步识别出车牌后,对车牌进行裁剪,方便下一步操作。
SimpleRecognizePlateByE2E(self,image)函数
def SimpleRecognizePlateByE2E(self,image):
images = self.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
res_set = []
for j,plate in enumerate(images):
plate, rect =plate
image_rgb,rect_refine = self.finemappingVertical(plate,rect)
res,confidence = self.recognizeOne(image_rgb)
res_set.append([res,confidence,rect_refine])
return res_set
对车牌号进行识别的核心函数,其算法思路如下:对导入的车牌文件(也就是图片)进行车牌粗定位,目的是找出图中所有的车牌;找出所有车牌后,对图中所有车牌进行精定位,也就是沿着这些车牌的轮廓,将车牌裁剪下来,方便接下来对车牌内容进行识别
SimpleRecognizePlate()函数
def SimpleRecognizePlate(image):
t0 = time.time()
images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
res_set = []
for j,plate in enumerate(images):
plate, rect, origin_plate =plate
# plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
plate =cv2.resize(plate,(136,36*2))
t1 = time.time()
ptype = td.SimplePredict(plate)
if ptype>0 and ptype<5:
plate = cv2.bitwise_not(plate)
image_rgb = fm.findContoursAndDrawBoundingBox(plate)
image_rgb = fv.finemappingVertical(image_rgb)
cache.verticalMappingToFolder(image_rgb)
print("e2e:", e2e.recognizeOne(image_rgb))
image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_RGB2GRAY)
# image_gray = horizontalSegmentation(image_gray)
cv2.imshow("image_gray",image_gray)
# cv2.waitKey()
cv2.imwrite("./"+str(j)+".jpg",image_gray)
# cv2.imshow("image",image_gray)
# cv2.waitKey(0)
print("校正",time.time() - t1,"s")
# cv2.imshow("image,",image_gray)
# cv2.waitKey(0)
t2 = time.time()
val = segmentation.slidingWindowsEval(image_gray)
# print val
print("分割和识别",time.time() - t2,"s")
if len(val)==3:
blocks, res, confidence = val
if confidence/7>0.7:
image = drawRectBox(image,rect,res)
res_set.append(res)
for i,block in enumerate(blocks):
block_ = cv2.resize(block,(25,25))
block_ = cv2.cvtColor(block_,cv2.COLOR_GRAY2BGR)
image[j * 25:(j * 25) + 25, i * 25:(i * 25) + 25] = block_
if image[j*25:(j*25)+25,i*25:(i*25)+25].shape == block_.shape:
pass
if confidence>0:
print("车牌:",res,"置信度:",confidence/7)
else:
pass
# print "不确定的车牌:", res, "置信度:", confidence
print(time.time() - t0,"s")
return image,res_set
输入为一个Mat类型的图片
输出为识别的车牌字符串,以及confidence可信度