# -*- coding: utf-8 -*-
__author__ = '樱花落舞'
import os
import cv2
import numpy as np
import debug
import img_math
import img_recognition
import config
SZ = 20 # 训练图片长宽
MAX_WIDTH = 1000 # 原始图片最大宽度
Min_Area = 2000 # 车牌区域允许最大面积
PROVINCE_START = 1000
class StatModel(object):
def load(self, fn):
self.model = self.model.load(fn)
def save(self, fn):
self.model.save(fn)
class SVM(StatModel):
def __init__(self, C=1, gamma=0.5):
self.model = cv2.ml.SVM_create()
self.model.setGamma(gamma)
self.model.setC(C)
self.model.setKernel(cv2.ml.SVM_RBF)
self.model.setType(cv2.ml.SVM_C_SVC)
# 训练svm
def train(self, samples, responses):
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
# 字符识别
def predict(self, samples):
r = self.model.predict(samples)
return r[1].ravel()
class CardPredictor:
def __init__(self):
pass
def __del__(self):
self.save_traindata()
def train_svm(self):
# 识别英文字母和数字
self.model = SVM(C=1, gamma=0.5)
# 识别中文
self.modelchinese = SVM(C=1, gamma=0.5)
if os.path.exists("svm.dat"):
self.model.load("svm.dat")
else:
chars_train = []
chars_label = []
for root, dirs, files in os.walk("train\\chars2"):
if len(os.path.basename(root)) > 1:
continue
root_int = ord(os.path.basename(root))
for filename in files:
filepath = os.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(root_int)
chars_train = list(map(img_recognition.deskew, chars_train))
chars_train = img_recognition.preprocess_hog(chars_train)
# chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
chars_label = np.array(chars_label)
print(chars_train.shape)
self.model.train(chars_train, chars_label)
if os.path.exists("svmchinese.dat"):
self.modelchinese.load("svmchinese.dat")
else:
chars_train = []
chars_label = []
for root, dirs, files in os.walk("train\\charsChinese"):
if not os.path.basename(root).startswith("zh_"):
continue
pinyin = os.path.basename(root)
index = img_recognition.provinces.index(pinyin) + PROVINCE_START + 1 # 1是拼音对应的汉字
for filename in files:
filepath = os.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(img_recognition.deskew, chars_train))
chars_train = img_recognition.preprocess_hog(chars_train)
# chars_train = chars_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.save("svmchinese.dat")
def img_first_pre(self, car_pic_file):
"""
:param car_pic_file: 图像文件
:return:已经处理好的图像文件 原图像文件
"""
if type(car_pic_file) == type(""):
img = img_math.img_read(car_pic_file)
else:
img = car_pic_file
pic_hight, pic_width = img.shape[:2]
if pic_width > MAX_WIDTH:
resize_rate = MAX_WIDTH / pic_width
img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * resize_rate)), interpolation=cv2.INTER_AREA)
# 缩小图片
blur = 5
img = cv2.GaussianBlur(img, (blur, blur), 0)
oldimg = img
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 转化成灰度图像
Matrix = np.ones((20, 20), np.uint8)
img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, Matrix)
img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0)
# 创建20*20的元素为1的矩阵 开操作,并和img重合
ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img_edge = cv2.Canny(img_thresh, 100, 200)
# Otsu’s二值化 找到图像边缘
Matrix = np.ones((4, 19), np.uint8)
img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, Matrix)
img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix)
return img_edge2, oldimg
def img_color_contours(self, img_contours, oldimg):
"""
:param img_contours: 预处理好的图像
:param oldimg: 原图像
:return: 已经定位好的车牌
"""
if img_contours.any():
config.set_name(img_contours)
pic_hight, pic_width = img_contours.shape[:2]
card_contours = img_math.img_findContours(img_contours)
card_imgs = img_math.img_Transform(card_contours, oldimg, pic_width, pic_hight)
colors, car_imgs = img_math.img_color(card_imgs)
predict_result = []
roi = None
card_color = None
for i, color in enumerate(colors):
if color in ("blue", "yello", "green"):
card_img = card_imgs[i]
gray_img = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY)
# 黄、绿车牌字符比背景暗、与蓝车牌刚好相反,所以黄、绿车牌需要反向
if color == "green" or color == "yello":
gray_img = cv2.bitwise_not(gray_img)
ret, gray_img = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
x_histogram = np.sum(gray_img, axis=1)
x_min = np.min(x_histogram)
x_average = np.sum(x_histogram) / x_histogram.shape[0]
x_threshold = (x_min + x_average) / 2
wave_peaks = img_math.find_waves(x_threshold, x_histogram)
if len(wave_peaks) == 0:
print("peak less 0:")
continue
# 认为水平方向,最大的波峰为车牌区域
wave = max(wave_peaks, key=lambda x: x[1] - x[0])
gray_img = gray_img[wave[0]:wave[1]]
# 查找垂直直方图波峰
row_num, col_num = gray_img.shape[:2]
# 去掉车牌上下边缘1个像素,避免白边影响阈值判断
gray_img = gray_img[1:row_num - 1]
y_histogram = np.sum(gray_img, axis=0)
y_min = np.min(y_histogram)
y_average = np.sum(y_histogram) / y_histogram.shape[0]
y_threshold = (y_min + y_average) / 5 # U和0要求阈值偏小,否则U和0会被分成两半
wave_peaks = img_math.find_waves(y_threshold, y_histogram)
if len(wave_peaks) <= 6:
print("peak less 1:", len(wave_peaks))
continue
wave = max(wave_peaks, key=lambda x: x[1] - x[0])
max_wave_dis = wave[1] - wave[0]
# 判断是否是左侧车牌边缘
if wave_peaks[0][1] - wave_peaks[0][0] < max_wave_dis / 3 and wave_peaks[0][0] == 0:
wave_peaks.pop(0)
# 组合分�
没有合适的资源?快使用搜索试试~ 我知道了~
基于深度学习+opencv的python车牌识别系统源码(Python毕业设计).zip

共47个文件
jpg:21个
png:15个
py:6个

1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉

温馨提示
基于深度学习+opencv的python车牌识别系统源码(Python毕业设计).zip该项目是已经经过导师审核的个人毕设项目源码,评审分达到95分以上,都经过严格调试,确保可以运行只需运行运行GUI.py即可!放心下载使用。 该项目资源主要针对计算机相关专业的学生或从业者下载使用,也可作为期末课程设计、期末课程大作业等,具有较高的学习价值。 基于深度学习+opencv的python车牌识别系统源码(Python毕业设计).zip该项目是已经经过导师审核的个人毕设项目源码,评审分达到95分以上,都经过严格调试,确保可以运行!放心下载使用。 该项目资源主要针对计算机相关专业的学生或从业者下载使用,也可作为期末课程设计、期末课程大作业等,具有较高的学习价值。 基于深度学习+opencv的python车牌识别系统源码(Python毕业设计).zip该项目是已经经过导师审核的个人毕设项目源码,评审分达到95分以上,都经过严格调试,确保可以运行!放心下载使用。 该项目资源主要针对计算机相关专业的学生或从业者下载使用,也可作为期末课程设计、期末课程大作业等,具有较高的学习价值。
资源推荐
资源详情
资源评论


























收起资源包目录





















































共 47 条
- 1
资源评论

- visions_2024-06-09发现一个宝藏资源,资源有很高的参考价值,赶紧学起来~
- m0_477953072025-04-25果断支持这个资源,资源解决了当前遇到的问题,给了新的灵感,感谢分享~

盈梓的博客
- 粉丝: 1w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- X3协同办公自动化-解决方案.ppt
- 加工中心大学本科方案设计书智能换刀PLC.doc
- 移动通信基站施工组织设计.doc
- 计算机组成原理(蒋本珊)第五章汇总.doc
- 如何运用多媒体网络技术优化初中书法教学.docx
- 油田物联网计算机网络安全技术.docx
- 系统安全分析的理论基础与方法.docx
- 浅析互联网传播与广播电视传播的异同.doc
- 大数据环境下农业信息管理对农业经济的影响.docx
- 论水利水电设计的计算机网络信息化建设运用.docx
- 融合监控系统在通信安防中的解决方案-公共场所其他.docx
- 软件工程的管理与应用.docx
- 电子商务专业《会计基础》课程测验考试大纲.docx
- ROS1下基于TensorRT部署pointpillars模型实现点云的3d目标检测
- 单片机控制交通灯大学本科方案设计书方案设计书.doc
- 凹凸模数控铣削加工工艺及程序设计定稿.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
