0 項(xiàng)目說明
基于opencv與SVM的車牌識別系統(tǒng)
提示:適合用于課程設(shè)計(jì)或畢業(yè)設(shè)計(jì),工作量達(dá)標(biāo),源碼開放
1 主要實(shí)現(xiàn)
用python3+opencv3做的中國車牌識別,包括算法和客戶端界面,只有2個(gè)文件,surface.py是界面代碼,predict.py是算法代碼,界面不是重點(diǎn)所以用tkinter寫得很簡單。
2 環(huán)境配置
python3.7.3
opencv4.0.0.21
numpy1.16.2
Tkinter
PIL5.4.1
3 界面效果
4 算法實(shí)現(xiàn)
算法思想來自于網(wǎng)上資源,先使用圖像邊緣和車牌顏色定位車牌,再識別字符。
- 車牌定位在predict方法中,為說明清楚,完成代碼和測試后,加了很多注釋,請參看源碼。
- 車牌字符識別也在predict方法中,請參看源碼中的注釋,需要說明的是,車牌字符識別使用的算法是opencv的SVM,
opencv的SVM使用代碼來自于opencv附帶的sample,StatModel類和SVM類都是sample中的代碼。 - SVM訓(xùn)練使用的訓(xùn)練樣本來自于github上的EasyPR的c++版本。
由于訓(xùn)練樣本有限,測試時(shí)會(huì)發(fā)現(xiàn),車牌字符識別,可能存在誤差,尤其是第一個(gè)中文字符出現(xiàn)的誤差概率較大。源碼中,上傳了EasyPR中的訓(xùn)練樣本,在train\目錄下,如果要重新訓(xùn)練請解壓在當(dāng)前目錄下,并刪除原始訓(xùn)練數(shù)據(jù)文件svm.dat和svmchinese.dat。文章來源:http://www.zghlxwxcb.cn/news/detail-828414.html
5 項(xiàng)目源碼
import tkinter as tk
from tkinter.filedialog import *
from tkinter import ttk
import predict
import cv2
from PIL import Image, ImageTk
import threading
import time
class Surface(ttk.Frame):
pic_path = ""
viewhigh = 600
viewwide = 600
update_time = 0
thread = None
thread_run = False
camera = None
color_transform = {"green":("綠牌","#55FF55"), "yello":("黃牌","#FFFF00"), "blue":("藍(lán)牌","#6666FF")}
def __init__(self, win):
ttk.Frame.__init__(self, win)
frame_left = ttk.Frame(self)
frame_right1 = ttk.Frame(self)
frame_right2 = ttk.Frame(self)
win.title("車牌識別")
win.state("zoomed")
self.pack(fill=tk.BOTH, expand=tk.YES, padx="5", pady="5")
frame_left.pack(side=LEFT,expand=1,fill=BOTH)
frame_right1.pack(side=TOP,expand=1,fill=tk.Y)
frame_right2.pack(side=RIGHT,expand=0)
ttk.Label(frame_left, text='原圖:').pack(anchor="nw")
ttk.Label(frame_right1, text='車牌位置:').grid(column=0, row=0, sticky=tk.W)
from_pic_ctl = ttk.Button(frame_right2, text="來自圖片", width=20, command=self.from_pic)
from_vedio_ctl = ttk.Button(frame_right2, text="來自攝像頭", width=20, command=self.from_vedio)
self.image_ctl = ttk.Label(frame_left)
self.image_ctl.pack(anchor="nw")
self.roi_ctl = ttk.Label(frame_right1)
self.roi_ctl.grid(column=0, row=1, sticky=tk.W)
ttk.Label(frame_right1, text='識別結(jié)果:').grid(column=0, row=2, sticky=tk.W)
self.r_ctl = ttk.Label(frame_right1, text="")
self.r_ctl.grid(column=0, row=3, sticky=tk.W)
self.color_ctl = ttk.Label(frame_right1, text="", width="20")
self.color_ctl.grid(column=0, row=4, sticky=tk.W)
from_vedio_ctl.pack(anchor="se", pady="5")
from_pic_ctl.pack(anchor="se", pady="5")
self.predictor = predict.CardPredictor()
self.predictor.train_svm()
def get_imgtk(self, img_bgr):
img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
im = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=im)
wide = imgtk.width()
high = imgtk.height()
if wide > self.viewwide or high > self.viewhigh:
wide_factor = self.viewwide / wide
high_factor = self.viewhigh / high
factor = min(wide_factor, high_factor)
wide = int(wide * factor)
if wide <= 0 : wide = 1
high = int(high * factor)
if high <= 0 : high = 1
im=im.resize((wide, high), Image.ANTIALIAS)
imgtk = ImageTk.PhotoImage(image=im)
return imgtk
def show_roi(self, r, roi, color):
if r :
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
roi = Image.fromarray(roi)
self.imgtk_roi = ImageTk.PhotoImage(image=roi)
self.roi_ctl.configure(image=self.imgtk_roi, state='enable')
self.r_ctl.configure(text=str(r))
self.update_time = time.time()
try:
c = self.color_transform[color]
self.color_ctl.configure(text=c[0], background=c[1], state='enable')
except:
self.color_ctl.configure(state='disabled')
elif self.update_time + 8 < time.time():
self.roi_ctl.configure(state='disabled')
self.r_ctl.configure(text="")
self.color_ctl.configure(state='disabled')
def from_vedio(self):
if self.thread_run:
return
if self.camera is None:
self.camera = cv2.VideoCapture(0)
if not self.camera.isOpened():
mBox.showwarning('警告', '攝像頭打開失?。?)
self.camera = None
return
self.thread = threading.Thread(target=self.vedio_thread, args=(self,))
self.thread.setDaemon(True)
self.thread.start()
self.thread_run = True
def from_pic(self):
self.thread_run = False
self.pic_path = askopenfilename(title="選擇識別圖片", filetypes=[("jpg圖片", "*.jpg")])
if self.pic_path:
img_bgr = predict.imreadex(self.pic_path)
self.imgtk = self.get_imgtk(img_bgr)
self.image_ctl.configure(image=self.imgtk)
resize_rates = (1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4)
for resize_rate in resize_rates:
print("resize_rate:", resize_rate)
r, roi, color = self.predictor.predict(img_bgr, resize_rate)
if r:
break
#r, roi, color = self.predictor.predict(img_bgr, 1)
self.show_roi(r, roi, color)
@staticmethod
def vedio_thread(self):
self.thread_run = True
predict_time = time.time()
while self.thread_run:
_, img_bgr = self.camera.read()
self.imgtk = self.get_imgtk(img_bgr)
self.image_ctl.configure(image=self.imgtk)
if time.time() - predict_time > 2:
r, roi, color = self.predictor.predict(img_bgr)
self.show_roi(r, roi, color)
predict_time = time.time()
print("run end")
def close_window():
print("destroy")
if surface.thread_run :
surface.thread_run = False
surface.thread.join(2.0)
win.destroy()
if __name__ == '__main__':
win=tk.Tk()
surface = Surface(win)
win.protocol('WM_DELETE_WINDOW', close_window)
win.mainloop()
6 最后
項(xiàng)目分享:https://gitee.com/asoonis/feed-neo文章來源地址http://www.zghlxwxcb.cn/news/detail-828414.html
到了這里,關(guān)于計(jì)算機(jī)java項(xiàng)目 - 基于opencv與SVM的車牌識別系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!