2022年车牌识别Python程序 .pdf
《2022年车牌识别Python程序 .pdf》由会员分享,可在线阅读,更多相关《2022年车牌识别Python程序 .pdf(22页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、# -*- coding: utf-8 -*- _author_ = d1bysj import pymysql db = pymysql.connect(host = , # 远程主机的ip 地址,user = , # MySQL用户名db = , # database 名passwd = , # 数据库密码port = 3306, #数据库监听端口,默认3306 charset = utf8) #指定 utf8 编码的连接cur= db.cursor() sql=select * from sex try: cur.execute(sql) re=cur.fetchall() for it
2、 in re: name = it0 num = it1 print(name,num) except Exception as e: raise e finally: db.close() import cv2 import numpy as np from numpy.linalg import norm import sys import os import json SZ = 20 # 训练图片长宽MAX_WIDTH = 1000 # 原始图片最大宽度Min_Area = 2000 # 车牌区域允许最大面积PROVINCE_START = 1000 # 读取图片文件def imread
3、ex(filename): return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_COLOR) def point_limit(point): 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 22 页 - - - - - - - - - if point0 0: point0 = 0 if point1 threshold: up_point = 0 is_peak = True wav
4、e_peaks = for i, x in enumerate(histogram): if is_peak and x 2: is_peak = False wave_peaks.append(up_point, i) elif not is_peak and x = threshold: is_peak = True up_point = i if is_peak and up_point != -1 and i - up_point 4: wave_peaks.append(up_point, i) return wave_peaks # 根据找出的波峰,分隔图片,从而得到逐个字符图片d
5、ef seperate_card(img, waves): part_cards = for wave in waves: part_cards.append(img:, wave0:wave1) return part_cards # 来自 opencv 的 sample,用于 svm 训练def deskew(img): m = cv2.moments(img) if abs(mmu02) 1: continue root_int = ord(os.path.basename(root) for filename in files: filepath = os.path.join(root
6、, filename) digit_img = cv2.imread(filepath) digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY) chars_train.append(digit_img) # chars_label.append(1) chars_label.append(root_int) chars_train = list(map(deskew, chars_train) chars_train = preprocess_hog(chars_train) # chars_train = chars_train.re
7、shape(-1, 20, 20).astype(np.float32) chars_label = np.array(chars_label) print(chars_train.shape) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 22 页 - - - - - - - - - self.model.train(chars_train, chars_label) if os.path.exists(svmchinese.dat): self.modelchine
8、se.load(svmchinese.dat) else: chars_train = chars_label = for root, dirs, files in os.walk(traincharsChinese): if not os.path.basename(root).startswith(zh_): continue pinyin = os.path.basename(root) index = provinces.index(pinyin) + PROVINCE_START + 1 # 1 是拼音对应的汉字for filename in files: filepath = os
9、.path.join(root, filename) digit_img = cv2.imread(filepath) digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY) chars_train.append(digit_img) # chars_label.append(1) chars_label.append(index) chars_train = list(map(deskew, chars_train) chars_train = preprocess_hog(chars_train) # chars_train = ch
10、ars_train.reshape(-1, 20, 20).astype(np.float32) chars_label = np.array(chars_label) print(chars_train.shape) self.modelchinese.train(chars_train, chars_label) def save_traindata(self): if not os.path.exists(svm.dat): self.model.save(svm.dat) if not os.path.exists(svmchinese.dat): self.modelchinese.
11、save(svmchinese.dat) def accurate_place(self, card_img_hsv, limit1, limit2, color): row_num, col_num = card_img_hsv.shape:2 xl = col_num xr = 0 yh = 0 yl = row_num # col_num_limit = self.cfgcol_num_limit row_num_limit = self.cfgrow_num_limit col_num_limit = col_num * 0.8 if color != green else col_n
12、um * 0.5 # 绿色有渐变for i in range(row_num): count = 0 for j in range(col_num): 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 22 页 - - - - - - - - - H = card_img_hsv.item(i, j, 0) S = card_img_hsv.item(i, j, 1) V = card_img_hsv.item(i, j, 2) if limit1 H = limit2 a
13、nd 34 S and 46 col_num_limit: if yl i: yl = i if yh i: yh = i for j in range(col_num): count = 0 for i in range(row_num): H = card_img_hsv.item(i, j, 0) S = card_img_hsv.item(i, j, 1) V = card_img_hsv.item(i, j, 2) if limit1 H = limit2 and 34 S and 46 row_num - row_num_limit: if xl j: xl = j if xr M
14、AX_WIDTH: resize_rate = MAX_WIDTH / pic_width img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * resize_rate), interpolation=cv2.INTER_AREA) blur = self.cfgblur # 高斯去噪if blur 0: img = cv2.GaussianBlur(img, (blur, blur), 0) # 图片分辨率调整oldimg = img img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # equ = cv2.
15、equalizeHist(img) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 22 页 - - - - - - - - - # img = np.hstack(img, equ) # 去掉图像中不会是车牌的区域kernel = np.ones(20, 20), np.uint8) img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) img_opening = cv2.addWeighted(img,
16、 1, img_opening, -1, 0); # 找到图像边缘ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) img_edge = cv2.Canny(img_thresh, 100, 200) # 使用开运算和闭运算让图像边缘成为一个整体kernel = np.ones(self.cfgmorphologyr, self.cfgmorphologyc), np.uint8) img_edge1 = cv2.morphologyEx(img_edge, cv2
17、.MORPH_CLOSE, kernel) img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, kernel) # 查找图像边缘整体形成的矩形区域,可能有很多, 车牌就在其中一个矩形区域中image, contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours = cnt for cnt in contours if cv2.contourArea(cnt) Min_Area print(len(c
18、ontours), len(contours) # 一一排除不是车牌的矩形区域car_contours = for cnt in contours: rect = cv2.minAreaRect(cnt) area_width, area_height = rect1 if area_width 2 and wh_ratio -1 and rect2 point0: left_point = point if low_point1 point1: low_point = point if heigth_point1 point1: heigth_point = point if right_p
19、oint0 point0: right_point = point if left_point1 right_point1: # 负角度new_left_point = left_point0, heigth_point1 pts2 = np.float32(new_left_point, heigth_point, right_point) # 字符只是高度需要改变pts1 = np.float32(left_point, heigth_point, right_point) M = cv2.getAffineTransform(pts1, pts2) dst = cv2.warpAffin
20、e(oldimg, M, (pic_width, pic_hight) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 22 页 - - - - - - - - - point_limit(right_point) point_limit(heigth_point) point_limit(new_left_point) card_img = dstint(right_point1):int(heigth_point1), int(new_left_point0):int
21、(right_point0) card_imgs.append(card_img) # cv2.imshow(card, card_img) # cv2.waitKey(0) # 开始使用颜色定位,排除不是车牌的矩形,目前只识别蓝、绿、黄车牌colors = for card_index, card_img in enumerate(card_imgs): green = yello = blue = black = white = 0 card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV) # 有转换失败的可能,原因来自于上面矫正矩形
22、出错if card_img_hsv is None: continue row_num, col_num = card_img_hsv.shape:2 card_img_count = row_num * col_num for i in range(row_num): for j in range(col_num): H = card_img_hsv.item(i, j, 0) S = card_img_hsv.item(i, j, 1) V = card_img_hsv.item(i, j, 2) if 11 H 34: # 图片分辨率调整yello += 1 elif 35 H 34:
23、# 图片分辨率调整green += 1 elif 99 H 34: # 图片分辨率调整blue += 1 if 0 H 180 and 0 S 255 and 0 V 46: black += 1 elif 0 H 180 and 0 S 43 and 221 V = card_img_count: color = yello limit1 = 11 limit2 = 34 # 有的图片有色偏偏绿elif green * 2 = card_img_count: color = green 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - -
24、- - 名师精心整理 - - - - - - - 第 10 页,共 22 页 - - - - - - - - - limit1 = 35 limit2 = 99 elif blue * 2 = card_img_count: color = blue limit1 = 100 limit2 = 124 # 有的图片有色偏偏紫elif black + white = card_img_count * 0.7: # TODO color = bw print(color) colors.append(color) print(blue, green, yello, black, white, ca
25、rd_img_count) # cv2.imshow(color, card_img) # cv2.waitKey(0) if limit1 = 0: continue # 以上为确定车牌颜色# 以下为根据车牌颜色再定位,缩小边缘非车牌边界xl, xr, yh, yl = self.accurate_place(card_img_hsv, limit1, limit2, color) if yl = yh and xl = xr: continue need_accurate = False if yl = yh: yl = 0 yh = row_num need_accurate = Tru
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年车牌识别Python程序 2022 车牌 识别 Python 程序
限制150内