资源描述:
《车牌识别Python程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#-*-coding:utf-8-*-__author__='d1bysj'importpymysqldb=pymysql.connect(host='',#远程主机的ip地址,user='',#MySQL用户名db='',#database名passwd='',#数据库密码port=3306,#数据库监听端口,默认3306charset="utf8")#指定utf8编码的连接cur=db.cursor()sql="select*fromsex"try:cur.execute(sql)re=cur.f
2、etchall()foritinre:name=it[0]num=it[1]print(name,num)exceptExceptionase:raiseefinally:db.close()importcv2importnumpyasnpfromnumpy.linalgimportnormimportsysimportosimportjsonSZ=20#训练图片长宽MAX_WIDTH=1000#原始图片最大宽度Min_Area=2000#车牌区域允许最大面积PROVINCE_START=1000#读
3、取图片文件defimreadex(filename):returncv2.imdecode(np.fromfile(filename,dtype=np.uint8),cv2.IMREAD_COLOR)defpoint_limit(point):ifpoint[0]<0:point[0]=0ifpoint[1]<0:point[1]=0#根据设定的阈值和图片直方图,找出波峰,用于分隔字符deffind_waves(threshold,histogram):up_point=-1#上升点is_peak=F
4、alseifhistogram[0]>threshold:up_point=0is_peak=Truewave_peaks=[]fori,xinenumerate(histogram):ifis_peakandx2:is_peak=Falsewave_peaks.append((up_point,i))elifnotis_peakandx>=threshold:is_peak=Trueup_point=iifis_peakandup_point!=-1a
5、ndi-up_point>4:wave_peaks.append((up_point,i))returnwave_peaks#根据找出的波峰,分隔图片,从而得到逐个字符图片defseperate_card(img,waves):part_cards=[]forwaveinwaves:part_cards.append(img[:,wave[0]:wave[1]])returnpart_cards#来自opencv的sample,用于svm训练defdeskew(img):m=cv2.moments(i
6、mg)ifabs(m['mu02'])<1e-2:returnimg.copy()skew=m['mu11']/m['mu02']M=np.float32([[1,skew,-0.5*SZ*skew],[0,1,0]])img=cv2.warpAffine(img,M,(SZ,SZ),flags=cv2.WARP_INVERSE_MAP
7、cv2.INTER_LINEAR)returnimg#来自opencv的sample,用于svm训练defpreprocess_hog(digits):samples
8、=[]forimgindigits:gx=cv2.Sobel(img,cv2.CV_32F,1,0)gy=cv2.Sobel(img,cv2.CV_32F,0,1)mag,ang=cv2.cartToPolar(gx,gy)bin_n=16bin=np.int32(bin_n*ang/(2*np.pi))bin_cells=bin[:10,:10],bin[10:,:10],bin[:10,10:],bin[10:,10:]mag_cells=mag[:10,:10],mag[10:,:10],mag
9、[:10,10:],mag[10:,10:]hists=[np.bincount(b.ravel(),m.ravel(),bin_n)forb,minzip(bin_cells,mag_cells)]hist=np.hstack(hists)#transformtoHellingerkerneleps=1e-7hist/=hist.sum()+epshist=np.sqrt(hist)hist/=norm(hist)+epssamples.append(