def display(self): if self.img_src_path == None: # 还没选择图片就进行预测 self.can_pred1.create_text(32, 15, text='请选择图片', anchor='nw', font=('黑体', 28)) else: img_src = cv2.imdecode(np.fromfile(self.img_src_path, dtype=np.uint8), -1) # 从中文路径读取时用 h, w = img_src.shape[0], img_src.shape[1] if h * w <= 240 * 80 and 2 <= w / h <= 5: # 满足该条件说明可能整个图片就是一张车牌,无需定位,直接识别即可 lic = cv2.resize(img_src, dsize=(240, 80), interpolation=cv2.INTER_AREA)[:, :, :3] # 直接resize为(240,80) img_src_copy, Lic_img = img_src, [lic] else: # 否则就需通过unet对img_src原图预测,得到img_mask,实现车牌定位,然后进行识别 img_src, img_mask = unet_predict(self.unet, self.img_src_path) img_src_copy, Lic_img = locate_and_correct(img_src, img_mask) # 利用core.py中的locate_and_correct函数进行车牌定位和矫正 Lic_pred = cnn_predict(self.cnn, Lic_img) # 利用cnn进行车牌的识别预测,Lic_pred中存的是元祖(车牌图片,识别结果) if Lic_pred: img = Image.fromarray(img_src_copy[:, :, ::-1]) # img_src_copy[:, :, ::-1]将BGR转为RGB self.img_Tk = ImageTk.PhotoImage(img) self.can_src.delete('all') # 显示前,先清空画板 self.can_src.create_image(258, 258, image=self.img_Tk, anchor='center') # img_src_copy上绘制出了定位的车牌轮廓,将其显示在画板上 for i, lic_pred in enumerate(Lic_pred): if i == 0: self.lic_Tk1 = ImageTk.PhotoImage(Image.fromarray(lic_pred[0][:, :, ::-1])) self.can_lic1.create_image(5, 5, image=self.lic_Tk1, anchor='nw') self.can_pred1.create_text(35, 15, text=lic_pred[1], anchor='nw', font=('黑体', 28)) elif i == 1: self.lic_Tk2 = ImageTk.PhotoImage(Image.fromarray(lic_pred[0][:, :, ::-1])) self.can_lic2.create_image(5, 5, image=self.lic_Tk2, anchor='nw') self.can_pred2.create_text(40, 15, text=lic_pred[1], anchor='nw', font=('黑体', 28)) elif i == 2: self.lic_Tk3 = ImageTk.PhotoImage(Image.fromarray(lic_pred[0][:, :, ::-1])) self.can_lic3.create_image(5, 5, image=self.lic_Tk3, anchor='nw') self.can_pred3.create_text(40, 15, text=lic_pred[1], anchor='nw', font=('黑体', 28)) else: # Lic_pred为空说明未能识别 self.can_pred1.create_text(47, 15, text='未能识别', anchor='nw', font=('黑体', 27))
lpn = provinces[gt[0]] + alphabets[gt[1]] + ads[gt[2]] + ads[ gt[3]] + ads[gt[4]] + ads[gt[5]] + ads[gt[6]] # img = cv2.imread(img_src_path) # img = cv2.resize(img,(512,512)) # cv2.imwrite(img_src_path,img) img_src = cv2.imdecode(np.fromfile(img_src_path, dtype=np.uint8), -1) h, w = img_src.shape[0], img_src.shape[1] if h * w <= 240 * 80 and 2 <= w / h <= 5: # 满足该条件说明可能整个图片就是一张车牌,无需定位,直接识别即可 lic = cv2.resize( img_src, dsize=(240, 80), interpolation=cv2.INTER_AREA)[:, :, : 3] # 直接resize为(240,80) img_src_copy, Lic_img = img_src, [lic] else: # 否则就需通过unet对img_src原图预测,得到img_mask,实现车牌定位,然后进行识别 img_src, img_mask = unet_predict(unet, img_src_path) img_src_copy, Lic_img, prebox = locate_and_correct( img_src, img_mask) # 利用core.py中的locate_and_correct函数进行车牌定位和矫正 prebox = np.array(prebox) if get_iou(prebox, gtbox) > 0.7: iou_right += 1 Lic_pred = cnn_predict( cnn, Lic_img) # 利用cnn进行车牌的识别预测,Lic_pred中存的是元祖(车牌图片,识别结果) #print('print the result') #print(Lic_pred) if Lic_pred: for i, lic_pred in enumerate(Lic_pred): if i == 0: text = lic_pred[1] text = text[0:2] + text[3:]
import cv2 import numpy as np from tensorflow import keras from Unet import unet_predict from core import locate, erode, dilate import tensorflow as tf print(tf.__version__) UNet = tf.keras.models.load_model('unet.h5') img_src, img_mask = unet_predict(UNet, '/home/sjq/81.jpg') #测试图片 # cv2.imshow('mask',img_mask) img2gray = erode(img_mask) # cv2.imshow('img2gray',img2gray) ret, mask = cv2.threshold(img2gray, 200, 255, cv2.THRESH_BINARY) # cv2.imshow('mask',mask) cv2.imshow('img_mask', cv2.bitwise_and(img_src, img_src, mask=cv2.bitwise_not(mask))) cv2.waitKey(0) gray2img = cv2.cvtColor(img2gray, cv2.COLOR_GRAY2BGR) img_src_copy = locate(img_src, gray2img) # cv2.imshow('show', img_mask) cv2.imshow('plate', img_src_copy) cv2.waitKey(0) cv2.destroyAllWindows()