全国免费电话:400-888-8888

竹百叶窗

本文摘要:1 说明:=====1.1 汽车车牌识别技术已经相当成熟了,也广泛应用到我们实际的生活中,好比:高速ETC,小区智能汽车车牌识别,停车场汽车车牌识别等等。1.2 今天来看看纯python实现中文车牌识别,是一套完整的汽车车牌识别分析系统,包罗汽车车牌定位,车牌字符的支解,车牌识别并显示识别后的车牌号码。1.3 注意情况:python3.8,深度操作系统deepin-linux,微软编辑器vscode,opencv版本4.2.0。

金沙体育官方平台

1 说明:=====1.1 汽车车牌识别技术已经相当成熟了,也广泛应用到我们实际的生活中,好比:高速ETC,小区智能汽车车牌识别,停车场汽车车牌识别等等。1.2 今天来看看纯python实现中文车牌识别,是一套完整的汽车车牌识别分析系统,包罗汽车车牌定位,车牌字符的支解,车牌识别并显示识别后的车牌号码。1.3 注意情况:python3.8,深度操作系统deepin-linux,微软编辑器vscode,opencv版本4.2.0。

2 代码泉源:========2.1 代码和模板泉源:http://zengqiang.club/blog/342.2 革新:增加中文车牌识别后的显示设置,对代码举行修改、注释和其中的bug举行分析和革新。3 第一个例子:==========3.1 图片,泉源地址:https://image.so.com/view?q=%E6%B1%BD%E8%BD%A6%E8%BD%A6%E7%89%8C&src=tab_www&correct=%E6%B1%BD%E8%BD%A6%E8%BD%A6%E7%89%8C&ancestor=list&cmsid=20929f08b4c5bf819e28029ca53bc106&cmras=6&cn=0&gn=0&kn=23&crn=0&bxn=20&fsn=103&cuben=0&adstar=0&clw=275#id=8803a63342b4f9c08d404257d00ade68&currsn=0&ps=78&pc=78图片来自网络,仅供学习3.2 操作示意图:3.3 效果图:3.4 文件结构:predata就是中文车牌识别模板,原来叫:refer1,需要自己去下载3.5 完整代码:#---第1步:导入所需模块---import cv2from matplotlib import pyplot as pltimport osimport numpy as npfrom PIL import Image, ImageDraw, ImageFont #add#---第2步:界说须要函数---# 函数1:显示图片def cv_show(name,img): cv2.imshow(name,img) cv2.waitKey() cv2.destroyAllWindows()# 函数2:plt显示彩色图片def plt_show0(img): b,g,r = cv2.split(img) #易报错地方,后面会提到 img = cv2.merge([r, g, b]) plt.imshow(img) plt.show()# 函数3:plt显示灰度图片def plt_show(img): plt.imshow(img,cmap='gray') plt.show()# 函数4:plt显示中文(add)def plt_show_ch(img): plt.imshow(img) plt.show()# 函数4:图像去噪灰度处置惩罚def gray_guss(image): image = cv2.GaussianBlur(image, (3, 3), 0) gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) return gray_image#---第3步:读取待检测图片---origin_image = cv2.imread('/home/xgj/Desktop/car-all-good/car1.jpg') #新能源汽车,支解有误plt_show0(origin_image) #第1张图:显示原始图片#---第4步:提取车牌部门图片---def get_carLicense_img(image): gray_image = gray_guss(image) Sobel_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0) absX = cv2.convertScaleAbs(Sobel_x) image = absX ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU) kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 5)) image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX,iterations = 3) kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1)) kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19)) image = cv2.dilate(image, kernelX) image = cv2.erode(image, kernelX) image = cv2.erode(image, kernelY) image = cv2.dilate(image, kernelY) image = cv2.medianBlur(image, 15) contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for item in contours: rect = cv2.boundingRect(item) x = rect[0] y = rect[1] weight = rect[2] height = rect[3] if (weight > (height * 3)) and (weight < (height * 4)): image = origin_image[y:y + height, x:x + weight] return imageimage = origin_image.copy()carLicense_image = get_carLicense_img(image)plt_show0(carLicense_image) #第2张图:#---第5步:车牌字符支解---def carLicense_spilte(image): gray_image = gray_guss(image) ret, image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU) plt_show(image) #第3张图: # 盘算二值图像黑白点的个数,处置惩罚绿牌照问题,让车牌号码始终为白色 area_white = 0 area_black = 0 height, width = image.shape for i in range(height): for j in range(width): if image[i, j] == 255: area_white += 1 else: area_black += 1 if area_white>area_black: ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) plt_show(image) #第3张图: kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) image = cv2.dilate(image, kernel) #轮廓识别,此处易报错 contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) words = [] word_images = [] for item in contours: word = [] rect = cv2.boundingRect(item) x = rect[0] y = rect[1] weight = rect[2] height = rect[3] word.append(x) word.append(y) word.append(weight) word.append(height) words.append(word) words = sorted(words,key=lambda s:s[0],reverse=False) i = 0 for word in words: #if (word[3] > (word[2] * 1.8)) and (word[3] < (word[2] * 3.5)): 原来 if (word[3] > (word[2] * 1)) and (word[3] < (word[2] * 3)): #修改成 i = i+1 splite_image = image[word[1]:word[1] + word[3], word[0]:word[0] + word[2]] word_images.append(splite_image) return word_imagesimage = carLicense_image.copy()word_images = carLicense_spilte(image) # 蓝牌为7,注意新能源汽车车牌可能报错for i,j in enumerate(word_images): plt.subplot(1,7,i+1) #普通汽车蓝色牌照 plt.imshow(word_images[i],cmap='gray')plt.show() #显示支解后的车牌#---第6步:模板准备和读取---# 准备模板template = ['0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z', '藏','川','鄂','甘','赣','贵','桂','黑','沪','吉','冀','津','晋','京','辽','鲁','蒙','闽','宁', '青','琼','陕','苏','皖','湘','新','渝','豫','粤','云','浙']#模板需要文件夹refer1,更名predata需要下载复制过来# 读取一个文件夹下的所有图片,输入参数是文件名,返回文件地址列表def read_directory(directory_name): referImg_list = [] for filename in os.listdir(directory_name): referImg_list.append(directory_name + "/" + filename) return referImg_list#指定路径,可自界说read_directory("/home/xgj/Desktop/car-all-good/predata")# 中文模板列表(只匹配车牌的第一个字符)def get_chinese_words_list(): chinese_words_list = [] for i in range(34,64): #中文识别 #路径,可自界说 c_word = read_directory('/home/xgj/Desktop/car-all-good/predata/' + template[i]) chinese_words_list.append(c_word) return chinese_words_listchinese_words_list = get_chinese_words_list()# 英文模板列表(只匹配车牌的第二个字符)def get_eng_words_list(): eng_words_list = [] for i in range(10,34): #可自界说 e_word = read_directory('/home/xgj/Desktop/car-all-good/predata/' + template[i]) eng_words_list.append(e_word) return eng_words_listeng_words_list = get_eng_words_list()# 英文数字模板列表(匹配车牌后面的字符)def get_eng_num_words_list(): eng_num_words_list = [] for i in range(0,34): #可自界说 word = read_directory('/home/xgj/Desktop/car-all-good/predata/' + template[i]) eng_num_words_list.append(word) return eng_num_words_listeng_num_words_list = get_eng_num_words_list()# 读取一个模板地址与图片举行匹配,返回得分def template_score(template,image): template_img=cv2.imdecode(np.fromfile(template,dtype=np.uint8),1) template_img = cv2.cvtColor(template_img, cv2.COLOR_RGB2GRAY) ret, template_img = cv2.threshold(template_img, 0, 255, cv2.THRESH_OTSU) image_ = image.copy() height, width = image_.shape template_img = cv2.resize(template_img, (width, height)) result = cv2.matchTemplate(image_, template_img, cv2.TM_CCOEFF) return result[0][0]#模板匹配def template_matching(word_images): results = [] for index,word_image in enumerate(word_images): if index==0: best_score = [] for chinese_words in chinese_words_list: score = [] for chinese_word in chinese_words: result = template_score(chinese_word,word_image) score.append(result) best_score.append(max(score)) i = best_score.index(max(best_score)) # print(template[34+i]) r = template[34+i] #汉字识别 results.append(r) continue if index==1: best_score = [] for eng_word_list in eng_words_list: score = [] for eng_word in eng_word_list: result = template_score(eng_word,word_image) score.append(result) best_score.append(max(score)) i = best_score.index(max(best_score)) # print(template[10+i]) r = template[10+i] #车牌汉字后面的大写英文字母的识别 results.append(r) continue else: best_score = [] for eng_num_word_list in eng_num_words_list: score = [] for eng_num_word in eng_num_word_list: result = template_score(eng_num_word,word_image) score.append(result) best_score.append(max(score)) i = best_score.index(max(best_score)) # print(template[i]) r = template[i] #车牌数字识别 results.append(r) continue return resultsword_images_ = word_images.copy()result = template_matching(word_images_)height,weight = origin_image.shape[0:2]#---第7步:在图片上标示绿色框和识别后的车牌显示image = origin_image.copy()#坐标点上移,图像#参数:pt1,对角坐标1, pt2:对角坐标2#注意这里凭据两个点pt1,pt2,确定了对角线的位置,进而确定了矩形的位置#0.9代表宽cv2.rectangle(image, (int(0.1*weight), int(0.3*height)), (int(weight*0.9), int(height*0.8)), (0, 255, 0), 5)chepaitext="".join(result)print(chepaitext) #终端打印识别出来的车牌#原来显示,中文不能显示,注释掉#cv2.putText(image, chepaitext, (int(0.1*weight)+10, int(0.5*height)+80), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 255, 0), 4)#增加:中文设置,放到前面去#from PIL import Image, ImageDraw, ImageFont# cv2和PIL中颜色的hex码的储存顺序差别cv2img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) pilimg = Image.fromarray(cv2img)# PIL图片上打印汉字draw = ImageDraw.Draw(pilimg) # 图片上打印#提前下载自己的中文字,好比我的体华文仿宋hwfs.ttf,巨细和编码font = ImageFont.truetype("hwfs.ttf", 70, encoding="utf-8") #显示中文汽车车牌号码draw.text((int(0.1*weight)+10,int(0.5*height)-50), chepaitext, (0,255, 0), font=font) cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)#plt_show0(image) #原来这样的,不显示#自界说中文显示函数plt_show_ch(pilimg) #显示中文4 第二个例子:==========4.1 图片:https://image.so.com/view?q=%E6%B1%BD%E8%BD%A6%E8%BD%A6%E7%89%8C&src=srp&correct=%E6%B1%BD%E8%BD%A6%E8%BD%A6%E7%89%8C&ancestor=list&cmsid=12f000f351b14744da9f2aae59d10df5&cmras=6&cn=0&gn=0&kn=23&crn=0&bxn=20&fsn=103&cuben=0&adstar=0&clw=275#id=b274aa2a1a162d7d4f156c888c121e5e&currsn=0&ps=78&pc=78图片来自网络,仅供学习4.2 识别错误:有bug,希望下次继续革新===欲知详情,请看下回剖析===关注我,不迷路,一起学习,不掉坑。

金沙体育官方平台

金沙体育app官网下载


本文关键词:金沙体育官网,强,全套,完整,中文,车牌,识别,纯,python,来

本文来源:金沙体育app官网下载-www.nbfert.cn

Copyright © 2002-2021 www.nbfert.cn. 金沙体育app官网下载科技 版权所有  ICP备81931252号-6  XML地图