├── README.md ├── README └── imgs │ ├── 1.png │ ├── 2.png │ └── aa ├── version ├── InputPicture │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ └── 8.jpg ├── OutputPicture │ ├── 1_0.jpg │ ├── 2_0.jpg │ ├── 3_1.jpg │ ├── 4_0.jpg │ ├── 5_0.jpg │ ├── 6_0.jpg │ ├── 7_0.jpg │ └── 如果不判断颜色定位出来的车牌有一些杂物 │ │ ├── 1_0.jpg │ │ ├── 2_0.jpg │ │ ├── 2_1.jpg │ │ ├── 2_2.jpg │ │ ├── 3_0.jpg │ │ ├── 3_1.jpg │ │ ├── 4_0.jpg │ │ ├── 5_0.jpg │ │ ├── 6_0.jpg │ │ ├── 6_1.jpg │ │ ├── 6_2.jpg │ │ ├── 6_3.jpg │ │ ├── 6_4.jpg │ │ ├── 7_0.jpg │ │ ├── 7_1.jpg │ │ ├── 7_2.jpg │ │ └── 7_3.jpg └── main.py └── version2 ├── icon.jpg ├── icon.png ├── outputPicture ├── 1.jpg ├── 10.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg ├── 6.jpg ├── 7.jpg ├── 8.jpg ├── 9.jpg ├── cAA662F.jpg ├── car4.jpg ├── lLD9016.jpg ├── wA87271.jpg ├── wATH859.jpg └── wAUB816.jpg ├── surface.py ├── tempPicture ├── 1.jpg ├── 10.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg ├── 6.jpg ├── 7.jpg ├── 8.jpg ├── 9.jpg ├── cAA662F.jpg ├── car4.jpg ├── lLD9016.jpg ├── wA87271.jpg ├── wATH859.jpg └── wAUB816.jpg ├── testPicture ├── 1.jpg ├── 10.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg ├── 6.jpg ├── 7.jpg ├── wATH859.jpg └── wAUB816.jpg └── tool.py /README.md: -------------------------------------------------------------------------------- 1 | # LicensePlateRecognition 2 | > ### 简介 3 | ##### 车牌识别系统python+opencv+百度ai 4 | 5 | > ### 运行环境配置如下: 6 | #### python3.6.1 7 | #### numpy (1.14.4) 8 | #### opencv-python (3.4.1.15) 9 | #### Pillow (5.1.0) 10 | #### requests (2.19.1) 11 | 12 | > ### version2运行结果 13 | ![选择图片](https://github.com/chsobin/LicensePlateRecognition/blob/master/README/imgs/1.png) 14 | 15 | ![车牌定位与车牌识别](https://github.com/chsobin/LicensePlateRecognition/blob/master/README/imgs/2.png) 16 | 17 | -------------------------------------------------------------------------------- /README/imgs/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/README/imgs/1.png -------------------------------------------------------------------------------- /README/imgs/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/README/imgs/2.png -------------------------------------------------------------------------------- /README/imgs/aa: -------------------------------------------------------------------------------- 1 | aaa 2 | -------------------------------------------------------------------------------- /version/InputPicture/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/1.jpg -------------------------------------------------------------------------------- /version/InputPicture/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/2.jpg -------------------------------------------------------------------------------- /version/InputPicture/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/3.jpg -------------------------------------------------------------------------------- /version/InputPicture/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/4.jpg -------------------------------------------------------------------------------- /version/InputPicture/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/5.jpg -------------------------------------------------------------------------------- /version/InputPicture/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/6.jpg -------------------------------------------------------------------------------- /version/InputPicture/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/7.jpg -------------------------------------------------------------------------------- /version/InputPicture/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/InputPicture/8.jpg -------------------------------------------------------------------------------- /version/OutputPicture/1_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/1_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/2_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/2_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/3_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/3_1.jpg -------------------------------------------------------------------------------- /version/OutputPicture/4_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/4_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/5_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/5_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/6_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/6_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/7_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/7_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/1_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/1_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/2_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/2_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/2_1.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/2_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/2_2.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/3_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/3_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/3_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/3_1.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/4_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/4_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/5_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/5_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_1.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_2.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_3.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/6_4.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_0.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_1.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_2.jpg -------------------------------------------------------------------------------- /version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version/OutputPicture/如果不判断颜色定位出来的车牌有一些杂物/7_3.jpg -------------------------------------------------------------------------------- /version/main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import base64 4 | import requests 5 | import json 6 | 7 | #### 图像预处理函数 8 | def preProcess(src): 9 | # # 调试用代码块 10 | # # 功能:显示处理过后的图像 11 | # cv2.namedWindow("src", cv2.WINDOW_NORMAL) 12 | # cv2.imshow("src", src) 13 | # # 调试用代码块 14 | 15 | # 灰度化 16 | gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) 17 | # # 调试用代码块 18 | # # 功能:显示处理过后的图像 19 | # cv2.namedWindow("gray", cv2.WINDOW_NORMAL) 20 | # cv2.imshow("gray", gray) 21 | # # 调试用代码块 22 | 23 | # 高斯平滑,中值滤波处理 24 | gaussian = cv2.GaussianBlur(gray, (3, 3), 0, 0, cv2.BORDER_DEFAULT) 25 | median = cv2.medianBlur(gaussian, 5) 26 | # # 调试用代码块 27 | # # 功能:显示处理过后的图像 28 | # cv2.namedWindow("median", cv2.WINDOW_NORMAL) 29 | # cv2.imshow("median", median) 30 | # # 调试用代码块 31 | 32 | # Sobel边缘检测 33 | sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3) 34 | # # 调试用代码块 35 | # # 功能:显示处理过后的图像 36 | # cv2.namedWindow("sobel", cv2.WINDOW_NORMAL) 37 | # cv2.imshow("sobel", sobel) 38 | # # 调试用代码块 39 | 40 | # 二值化 41 | ret, binary = cv2.threshold(sobel, 170, 255, cv2.THRESH_BINARY) 42 | 43 | # 以下膨胀和腐蚀的参数需要自己测试 44 | element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1)) 45 | element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 6)) 46 | # 膨胀一次,让轮廓突出 47 | dilation = cv2.dilate(binary, element2, iterations=10) 48 | # # 调试用代码块 49 | # # 功能:显示处理过后的图像 50 | # cv2.namedWindow("peng", cv2.WINDOW_NORMAL) 51 | # cv2.imshow("peng", dilation) 52 | # # 调试用代码块 53 | # 腐蚀一次,去掉细节 54 | erosion = cv2.erode(dilation, element1, iterations=12) 55 | # # 调试用代码块 56 | # # 功能:显示处理过后的图像 57 | # cv2.namedWindow("fu", cv2.WINDOW_NORMAL) 58 | # cv2.imshow("fu", erosion) 59 | # # 调试用代码块 60 | # 再次膨胀,让轮廓明显一些 61 | dilation2 = cv2.dilate(erosion, element2, iterations=14) 62 | # # 调试用代码块 63 | # # 功能:显示处理过后的图像 64 | # cv2.namedWindow("dilation2", cv2.WINDOW_NORMAL) 65 | # cv2.imshow('dilation2', dilation2) 66 | # # 调试用代码块 67 | return dilation2 68 | 69 | 70 | #### 轮廓筛选函数 需要输入预处理并且二值化的图像 71 | def filterContours(src, afterPreProcess): 72 | targets = []; 73 | pic, contours, hierarchy = cv2.findContours(afterPreProcess, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 74 | 75 | # 遍历每一个轮廓 过滤掉面积过大或者过小的轮廓此处取值[130000, 260000] 76 | for i in range(len(contours)): 77 | cnt = contours[i] 78 | area = cv2.contourArea(cnt) 79 | if area>=130000 and area<=260000: 80 | # 将轮廓近似为矩形 81 | epsilon = 0.15 * cv2.arcLength(cnt, True) 82 | # approx 存储了矩形的对角点 83 | approx = cv2.approxPolyDP(cnt, epsilon, True) 84 | for j in range(0, len(approx), 2): 85 | point_m = (approx[j][0][0], approx[j][0][1]) 86 | point_n = (approx[j + 1][0][0], approx[j + 1][0][1]) 87 | # # 调试用代码 begin 88 | # # 输出矩形的点坐标 89 | # print(point_m, point_n) 90 | # # 调试用代码 end 91 | # # 调试用代码 begin 92 | # # 可以在原图画出approx所代表的矩形 93 | # cv2.rectangle(src, point_m, point_n, (0, 0, 255), 3) 94 | # # 调试用代码 end 95 | min_x = min(point_n[0], point_m[0]) 96 | max_x = max(point_n[0], point_m[0]) 97 | min_y = min(point_n[1], point_m[1]) 98 | max_y = max(point_n[1], point_m[1]) 99 | # 这一步是为了y方向上切多一些 100 | if min_y - 50 < 0: 101 | min_y = 0 102 | else: 103 | min_y = min_y - 50 104 | temp = src[min_y:max_y+20, min_x:max_x] 105 | targets.append(temp) 106 | return targets 107 | 108 | # 根据车牌的颜色直方图过滤 109 | def checkByColor(img): 110 | # # 调试用代码块 begin 111 | # # 功能:显示图像灰度分布直方图 112 | # plt.hist(img.ravel(),256,[0,256]); 113 | # plt.show() 114 | # # 调试用代码块 end 115 | 116 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 117 | maxColor = np.argmax(np.bincount(gray.ravel())) 118 | if maxColor>= 64 and maxColor<=129: 119 | return True 120 | else: 121 | return False 122 | 123 | #### 通过百度ai接口进行图片识别 输入参数access_token有效期为一个月,一个月之后需要重新获取 124 | def getCarNumberByBaiDuAI(access_token, picturePath): 125 | url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=" 126 | url += access_token 127 | headers = {} 128 | headers["Content-Type"] = "application/x-www-form-urlencoded" 129 | data = {} 130 | data["multi_detect"] = False # 图像中是否有多个车牌需要识别 131 | with open(picturePath, "rb") as f: 132 | # b64encode是编码,b64decode是解码 133 | base64_data = base64.b64encode(f.read()) 134 | data["image"] = base64_data 135 | response = requests.post(url, data=data, headers=headers) 136 | html = response.content 137 | html_doc = str(html, 'utf-8') 138 | result = json.loads(html_doc) 139 | return result 140 | 141 | 142 | if __name__ == '__main__': 143 | baiduAIAccessToken = '24.8f04d71f6aab2e42083a20c78e3b7b4e.2592000.1532249479.282335-11432255' 144 | prefixPath = './InputPicture/' 145 | pictureNum = 9 146 | suffixPath = '.jpg' 147 | outputDir = './OutputPicture/' 148 | 149 | for picIndex in range(1, pictureNum): 150 | src = cv2.imread(prefixPath + str(picIndex) + suffixPath) 151 | afterPreProcess = preProcess(src) 152 | targets = filterContours(src, afterPreProcess) 153 | if len(targets)==1: 154 | picName = outputDir+str(picIndex)+"_0"+".jpg" 155 | cv2.imwrite(picName, targets[0]) 156 | result = getCarNumberByBaiDuAI(baiduAIAccessToken, picName) 157 | if 'error_code' in result: 158 | print("-------- " + picName) 159 | print("!!!!错误:百度ai识别失败,错误信息为", result) 160 | else: 161 | print("-------- " + picName) 162 | print("颜色:" + result['words_result']['color']) 163 | print("号码:" + result['words_result']['number']) 164 | else: 165 | for i in range(len(targets)): 166 | picName = outputDir+str(picIndex)+"_"+str(i)+".jpg" 167 | if checkByColor(targets[i]): 168 | cv2.imwrite(picName, targets[i]) 169 | result = getCarNumberByBaiDuAI(baiduAIAccessToken, picName) 170 | if 'error_code' in result: 171 | print("-------- " + picName) 172 | print("!!!!错误:百度ai识别失败,错误信息为", result) 173 | else: 174 | print("-------- " + picName) 175 | print("颜色:" + result['words_result']['color']) 176 | print("号码:" + result['words_result']['number']) 177 | 178 | -------------------------------------------------------------------------------- /version2/icon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/icon.jpg -------------------------------------------------------------------------------- /version2/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/icon.png -------------------------------------------------------------------------------- /version2/outputPicture/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/1.jpg -------------------------------------------------------------------------------- /version2/outputPicture/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/10.jpg -------------------------------------------------------------------------------- /version2/outputPicture/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/2.jpg -------------------------------------------------------------------------------- /version2/outputPicture/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/3.jpg -------------------------------------------------------------------------------- /version2/outputPicture/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/4.jpg -------------------------------------------------------------------------------- /version2/outputPicture/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/5.jpg -------------------------------------------------------------------------------- /version2/outputPicture/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/6.jpg -------------------------------------------------------------------------------- /version2/outputPicture/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/7.jpg -------------------------------------------------------------------------------- /version2/outputPicture/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/8.jpg -------------------------------------------------------------------------------- /version2/outputPicture/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/9.jpg -------------------------------------------------------------------------------- /version2/outputPicture/cAA662F.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/cAA662F.jpg -------------------------------------------------------------------------------- /version2/outputPicture/car4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/car4.jpg -------------------------------------------------------------------------------- /version2/outputPicture/lLD9016.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/lLD9016.jpg -------------------------------------------------------------------------------- /version2/outputPicture/wA87271.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/wA87271.jpg -------------------------------------------------------------------------------- /version2/outputPicture/wATH859.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/wATH859.jpg -------------------------------------------------------------------------------- /version2/outputPicture/wAUB816.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/outputPicture/wAUB816.jpg -------------------------------------------------------------------------------- /version2/surface.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import filedialog 3 | from PIL import Image, ImageTk 4 | import tool 5 | import cv2 6 | 7 | # 全局变量 8 | srcPath = "" 9 | pictureName = "" 10 | tempPicturePath = "./tempPicture/" 11 | outputPicturePath = "./outputPicture/" 12 | baiduAIAccessToken = '24.5d22f3f43aaeee80c6bcaafdb72b093c.2592000.1533119124.282335-11475678' 13 | 14 | # 改变图片大小 15 | def resize( w_box, h_box, pil_image): #参数是:要适应的窗口宽、高、Image.open后的图片 16 | w, h = pil_image.size #获取图像的原始大小 17 | f1 = 1.0*w_box/w 18 | f2 = 1.0*h_box/h 19 | factor = min([f1, f2]) 20 | width = int(w*factor) 21 | height = int(h*factor) 22 | return pil_image.resize((width, height), Image.ANTIALIAS) 23 | 24 | # 窗口设置 25 | window = tk.Tk() 26 | window.title('车牌自动识别系统(chsobin)') 27 | window.geometry('1000x700') 28 | 29 | 30 | # chsobin's logo 31 | canvas = tk.Canvas(window, width=150, height=150) 32 | image_file = tk.PhotoImage(file='icon.png') 33 | image = canvas.create_image(0,0, anchor='nw', image=image_file) 34 | canvas.place(x=20, y=20) 35 | 36 | # 显示原始图像的canvas 37 | canvas_pic = tk.Canvas(window, width=700, height=600) 38 | canvas_pic.pack(side='right') 39 | # 图片选择函数 40 | def printcoords(): 41 | File = filedialog.askopenfilename(parent=window, title='请选择一张图片') 42 | global srcPath 43 | srcPath=File 44 | src = Image.open(File) 45 | resizePic = resize(700, 600, src) 46 | filename = ImageTk.PhotoImage(resizePic) 47 | canvas_pic.image = filename # <--- keep reference of your image 48 | canvas_pic.create_image(350, 300, image=filename) 49 | # 选择图片按钮 50 | btn_choosePic = tk.Button(window, text='选择图片', command=printcoords) 51 | btn_choosePic.place(x=40, y=190) 52 | 53 | 54 | # 显示定位的车牌的canvas 55 | canvas_plate = tk.Canvas(window, width=200, height=50) 56 | canvas_plate.place(x=10, y=320) 57 | # 定位调用函数 58 | def location(): 59 | global tempPicturePath 60 | global outputPicturePath 61 | global pictureName 62 | pictureName = srcPath.split('/')[-1] 63 | src = cv2.imread(srcPath) 64 | afterPreProcess = tool.preProcess(src) 65 | pic, contours, hierarchy = cv2.findContours(afterPreProcess, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 66 | rect = tool.getRect(contours) 67 | if len(rect) != 1: 68 | print('最小包围矩形有:', len(rect), '系统无法识别') 69 | else: 70 | Rotation_M = tool.getRotation_M(rect[0]) 71 | rotatePic = tool.rotateSrc(src, Rotation_M) 72 | cv2.imwrite(tempPicturePath + pictureName, rotatePic) 73 | src = cv2.imread(tempPicturePath + pictureName) 74 | afterPreProcess = tool.preProcess(src) 75 | pic, contours, hierarchy = cv2.findContours(afterPreProcess, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 76 | rect = tool.getRect(contours) 77 | if len(rect) != 1: 78 | print('最小包围矩形有:',len(rect) ,'系统无法识别') 79 | else: 80 | result = tool.cutSrcByRect(src, rect[0], -5) 81 | cv2.imwrite(outputPicturePath + pictureName, result) 82 | print(pictureName, "成功定位到车牌") 83 | src = Image.open(outputPicturePath + pictureName) 84 | resizePic = resize(200, 50, src) 85 | filename = ImageTk.PhotoImage(resizePic) 86 | canvas_plate.image = filename 87 | canvas_plate.create_image(100, 25, image=filename) 88 | # 开始定位按钮 89 | btn_location = tk.Button(window, text='开始定位', command=location) 90 | btn_location.place(x=40, y=250) 91 | tk.Label(window, text='车牌定位结果: ', font='18', width=18).place(x=20, y= 300) 92 | 93 | 94 | 95 | # 识别结果显示框 96 | var_returnMsg = tk.StringVar() 97 | var_returnMsg.set("(* _ *)") 98 | l = tk.Label(window, 99 | textvariable=var_returnMsg, 100 | font=('Arial', 12), # 字体和字体大小 101 | width=18, height=4 # 标签长宽 102 | ) 103 | l.place(x=20, y=470) 104 | # 识别调用函数 105 | def recognition(): 106 | global baiduAIAccessToken 107 | global outputPicturePath 108 | global pictureName 109 | result = tool.getCarNumberByBaiDuAI(baiduAIAccessToken, outputPicturePath+pictureName) 110 | if 'error_code' in result: 111 | print("-------- " + outputPicturePath + pictureName) 112 | print("!!!!错误:百度ai识别失败,错误信息为", result) 113 | else: 114 | print("-------- " + outputPicturePath + pictureName) 115 | print("颜色:" + result['words_result']['color']) 116 | print("号码:" + result['words_result']['number']) 117 | var_returnMsg.set(result['words_result']['color'] + " " + result['words_result']['number']) 118 | 119 | # 开始识别按钮 120 | btn_recognition = tk.Button(window, text='开始识别', command=recognition) 121 | btn_recognition.place(x=40, y=400) 122 | tk.Label(window, text='车牌识别结果: ', font='18').place(x=20, y= 450) 123 | 124 | window.mainloop() 125 | 126 | -------------------------------------------------------------------------------- /version2/tempPicture/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/1.jpg -------------------------------------------------------------------------------- /version2/tempPicture/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/10.jpg -------------------------------------------------------------------------------- /version2/tempPicture/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/2.jpg -------------------------------------------------------------------------------- /version2/tempPicture/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/3.jpg -------------------------------------------------------------------------------- /version2/tempPicture/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/4.jpg -------------------------------------------------------------------------------- /version2/tempPicture/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/5.jpg -------------------------------------------------------------------------------- /version2/tempPicture/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/6.jpg -------------------------------------------------------------------------------- /version2/tempPicture/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/7.jpg -------------------------------------------------------------------------------- /version2/tempPicture/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/8.jpg -------------------------------------------------------------------------------- /version2/tempPicture/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/9.jpg -------------------------------------------------------------------------------- /version2/tempPicture/cAA662F.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/cAA662F.jpg -------------------------------------------------------------------------------- /version2/tempPicture/car4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/car4.jpg -------------------------------------------------------------------------------- /version2/tempPicture/lLD9016.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/lLD9016.jpg -------------------------------------------------------------------------------- /version2/tempPicture/wA87271.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/wA87271.jpg -------------------------------------------------------------------------------- /version2/tempPicture/wATH859.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/wATH859.jpg -------------------------------------------------------------------------------- /version2/tempPicture/wAUB816.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/tempPicture/wAUB816.jpg -------------------------------------------------------------------------------- /version2/testPicture/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/1.jpg -------------------------------------------------------------------------------- /version2/testPicture/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/10.jpg -------------------------------------------------------------------------------- /version2/testPicture/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/2.jpg -------------------------------------------------------------------------------- /version2/testPicture/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/3.jpg -------------------------------------------------------------------------------- /version2/testPicture/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/4.jpg -------------------------------------------------------------------------------- /version2/testPicture/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/5.jpg -------------------------------------------------------------------------------- /version2/testPicture/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/6.jpg -------------------------------------------------------------------------------- /version2/testPicture/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/7.jpg -------------------------------------------------------------------------------- /version2/testPicture/wATH859.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/wATH859.jpg -------------------------------------------------------------------------------- /version2/testPicture/wAUB816.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chensongbin/LicensePlateRecognition/464c5acf64b9d57080df622f45b618ff15963dc6/version2/testPicture/wAUB816.jpg -------------------------------------------------------------------------------- /version2/tool.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import json 4 | import base64 5 | import requests 6 | 7 | # 参数设定 8 | # 车牌面积范围 9 | MIN_AREA = 7000 10 | MAX_AREA = 200000 11 | # 车牌宽高比值 12 | MIN_RATE = 2.6 13 | MAX_RATE = 3.6 14 | # 蓝色范围 15 | lower_blue = np.array([100, 110, 110]) 16 | upper_blue = np.array([130, 255, 255]) 17 | # 黄色范围 (暂时没有用到) 18 | lower_yellow = np.array([15, 55, 55]) 19 | upper_yellow = np.array([50, 255, 255]) 20 | 21 | # 函数返回灰度化的图像,经过处理,车牌与背景分割出来 22 | def preProcess(src): 23 | # 高斯平滑,中值滤波 24 | gaussian = cv2.GaussianBlur(src, (3, 3), 0, 0, cv2.BORDER_DEFAULT) 25 | median = cv2.medianBlur(gaussian, 5) 26 | # 将rgb模型转化为hsv模型,方便颜色定位 27 | # 根据阈值找到对应颜色 28 | hsv = cv2.cvtColor(median, cv2.COLOR_BGR2HSV) 29 | mask_blue = cv2.inRange(hsv, lower_blue, upper_blue) 30 | blue = cv2.bitwise_and(hsv, hsv, mask=mask_blue) 31 | # 灰度化 32 | gray = cv2.cvtColor(blue, cv2.COLOR_BGR2GRAY) 33 | # 形态学操作:膨胀 与 腐蚀 34 | element = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 20)) 35 | dilation = cv2.dilate(gray, element, iterations=1) 36 | erosion = cv2.erode(dilation, element, iterations=1) 37 | return erosion 38 | 39 | # 过滤掉一些不符合要求地轮廓,返回轮廓的最小包围矩形 40 | def getRect(contours): 41 | result = [] 42 | for contour in contours: 43 | # 计算轮廓的面积 44 | area = cv2.contourArea(contour) 45 | if area < MIN_AREA or area > MAX_AREA: 46 | continue 47 | # 求出轮廓的最小包围矩形 48 | rect = cv2.minAreaRect(contour) 49 | # 长宽 50 | width, height = rect[1] 51 | if height == 0 or width==0: 52 | continue 53 | rate = max(width, height) / min(width, height) 54 | # 过滤掉长宽比例不符合要求的轮廓 55 | if rate < MIN_RATE or rate > MAX_RATE: 56 | continue 57 | # 符合要求则添加进result 58 | result.append(rect) 59 | return result 60 | 61 | # 通过minAreaRect得到旋转角度 62 | def getRotation_M(rect): 63 | # 角度:[-90,0) 64 | angle = rect[2] 65 | # 倾斜校正 66 | if abs(angle) > 45 and abs(angle) < 90: 67 | angle = 90 - abs(angle) 68 | scale = 1 69 | # 得到旋转矩阵 70 | return cv2.getRotationMatrix2D(rect[0], angle, scale) 71 | 72 | # 将原图进行旋转 73 | def rotateSrc(src, rotation_M): 74 | desImg = src.copy() 75 | rows, cols, depth = src.shape 76 | desImg = cv2.warpAffine(src, rotation_M, (rows+200, cols+200)) 77 | return desImg 78 | 79 | 80 | # 根据矩形在原图裁剪图片 81 | # src 原图 rect 最小包围矩形 padding 缩小矩形范围 82 | def cutSrcByRect(src, rect, padding=0): 83 | box = cv2.boxPoints(rect) 84 | min_x = int(min(box[:, 0])) + padding 85 | max_x = int(max(box[:, 0])) - padding 86 | min_y = int(min(box[:, 1])) + padding 87 | max_y = int(max(box[:, 1])) - padding 88 | target = src[min_y:max_y, min_x:max_x] 89 | return target 90 | 91 | 92 | #### 通过百度ai接口进行图片识别 输入参数access_token有效期为一个月,一个月之后需要重新获取 93 | def getCarNumberByBaiDuAI(access_token, picturePath): 94 | url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=" 95 | #url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" 96 | url += access_token 97 | headers = {} 98 | headers["Content-Type"] = "application/x-www-form-urlencoded" 99 | data = {} 100 | data["multi_detect"] = False # 图像中是否有多个车牌需要识别 101 | with open(picturePath, "rb") as f: 102 | # b64encode是编码,b64decode是解码 103 | base64_data = base64.b64encode(f.read()) 104 | data["image"] = base64_data 105 | response = requests.post(url, data=data, headers=headers) 106 | html = response.content 107 | html_doc = str(html, 'utf-8') 108 | result = json.loads(html_doc) 109 | return result 110 | 111 | 112 | if __name__ == '__main__': 113 | baiduAIAccessToken = '24.8f04d71f6aab2e42083a20c78e3b7b4e.2592000.1532249479.282335-11432255' 114 | path = './testPicture/' 115 | tempPath = './tempPicture/' 116 | outputPath = './outputPicture/' 117 | for i in range(1,8): 118 | name = str(i) + '.jpg' 119 | src = cv2.imread(path+name) 120 | afterPreProcess = preProcess(src) 121 | pic, contours, hierarchy = cv2.findContours(afterPreProcess, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 122 | rect = getRect(contours) 123 | if len(rect) != 1: 124 | print('最小包围矩形有多个,系统无法识别') 125 | else: 126 | Rotation_M = getRotation_M(rect[0]) 127 | rotatePic = rotateSrc(src, Rotation_M) 128 | cv2.imwrite(tempPath+name, rotatePic) 129 | src = cv2.imread(tempPath+name) 130 | afterPreProcess = preProcess(src) 131 | pic, contours, hierarchy = cv2.findContours(afterPreProcess, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 132 | rect = getRect(contours) 133 | if len(rect)!=1: 134 | print('最小包围矩形有多个,系统无法识别') 135 | else: 136 | result = cutSrcByRect(src, rect[0], -5) 137 | cv2.imwrite(outputPath+name, result) 138 | print(name,"成功截图") 139 | print(outputPath+name) 140 | print(getCarNumberByBaiDuAI(baiduAIAccessToken, outputPath+name)) 141 | 142 | --------------------------------------------------------------------------------