1, what EASYPR data set;
eASYPR is an open-source Chinese license plate recognition system whose goal is to become a simple, efficient and accurate license plate recognition library under the unrestricted scene (unconstrained situation).
compared to other license plate recognition systems, E ASYPR has the following features:
- It is based on OPENCV, the open Source Library. This means that you can get all the source code and migrate to all the platforms supported by OPENCV.
- It can recognize Chinese. For example, the license plate for SU EUK722 's picture, it can accurately output the std:string type of "Su EUK722" results.
- it has a high recognition rate. Picture clearly, the license plate detection and character recognition can achieve more than 80% accuracy. Point
it to General Data Share license in the form of some Chinese car pictures
Because I used the intersection seriously, so do not need to sub-directory, directly can put the POS and neg pictures placed under a directory.
2, reconstruction algorithm, running in EASYPR;
ImportCv2ImportNumPy asNp fromOs.pathImportJoinImportNumPy asNpImportOsImportMath#在carData建立svm模型并且k_fold测试, ratio=1 indicates that all data is used for testingRATIO =0.2DataPath ="d:/dl4cv/datesets/easyprresources/train/svm/"#分为has和no两个文件夹, take all jpg pictures
#根据Ratio获得训练和测试数据集的图片地址和标签def Get_files(File_dir,ratio):" "Args:File_dir:file DirectoryReturns:list of images and labels" "pos = [] Label_pos = [] neg = [] Label_neg = [] for file inchOs.listdir (file_dir+"has/"): Pos.append (File_dir +"has/"+file) Label_pos.append (1) for file inchOs.listdir (file_dir+"no/"): Neg.append (File_dir +"no/"+file) Label_neg.append (1)
Print(' data sets have%dPOS\ nas well%dneg '%(Len(POS),Len(neg)))#图片list和标签list #hstack level (in column order) to stack the array.Image_list = Np.hstack ((pos, neg)) Label_list = Np.hstack ((Label_pos, label_neg)) temp = Np.array ([Image_list, Label_list ]) temp = Temp.transpose ()#乱序的目的是为了让正样本和负样本混在一起, so you can use it directly.Np.random.shuffle (temp) all_image_list = temp[:,0] All_label_list = temp[:,1] N_sample =Len(all_label_list)#根据比率, determine the number of training and testingN_val = Math.ceil (n_sample*ratio)# Number of validation samplesN_train = N_sample-n_val# Number of trainning samplesTra_images = [] Val_images = []#按照0-n_train for Tra_images, the next bit val_images way to sortTra_images = All_image_list[:n_train] Tra_labels = all_label_list[:n_train] Tra_labels = [int(float(i)) forIinchTra_labels]
Val_images = All_image_list[n_train:] val_labels = all_label_list[n_train:] Val_labels = [int(float(i)) forIinchVal_labels]returnTra_images,tra_labels,val_images,val_labels
#创建sift特征提取Detect = Cv2.xfeatures2d.SIFT_create () extract = Cv2.xfeatures2d.SIFT_create ()#创建基于flann的匹配器Flann_params =Dict(algorithm=1,Trees=5) Matcher = Cv2. Flannbasedmatcher (Flann_params, {})#创建bow训练器Bow_kmeans_trainer = Cv2. Bowkmeanstrainer ( +) Extract_bow = Cv2. Bowimgdescriptorextractor (extract, Matcher)#以灰度方式读取图像, extract the SIFT, and return the resultdef Extract_sift(fn): im = Cv2.imread (fn)Try: Cv2.cvtcolor (Im,im,cv2. Color_bgr2gray)except:Pass returnExtract.compute (IM, detect.detect (IM)) [1]#返回bow的描述符提取器计算得到的描述符def Bow_features(fn): im = Cv2.imread (FN,0)returnExtract_bow.compute (IM, detect.detect (IM))#返回预测的结果def predict(fn): F = bow_features (FN); p = svm.predict (f)Print(FN,"\ t", p[1][0][0])returnP############################################# main ################################################获得训练和测试数据集的图片地址和标签Train_images, Train_labels, val_images, val_labels = Get_files (DataPath, RATIO) traindata, trainlabels = [],[]#为feature模型输入正负样本 forIinch Range( -):Try: Bow_kmeans_trainer.add (Extract_sift (Train_images[i]))except:Pass#feature模型聚类, return to glossaryVOC = Bow_kmeans_trainer.cluster () extract_bow.setvocabulary (VOC)
#创建并训练一个svm模型Print("Create and train a SVM model") forIinch Range(Len(train_images)):Try: Traindata.extend (Bow_features (train_images[i)) Trainlabels.append (Train_labels[i])except:PassSVM = Cv2.ml.SVM_create () svm.train (Np.array (Traindata), Cv2.ml.ROW_SAMPLE, Np.array (trainlabels))Print("Testing on a test set")#在测试集上进行测试result = [] forIinch Range(Len(val_images)):Try: F = bow_features (Val_images[i]); p = svm.predict (f) result.append (p[1][0][0])except: Result.append (0) Np_val_labels = Np.array (Val_labels) [:, Np.newaxis]np_result = Np.array (Result) [:, np.newaxis]matches = Np_result = = NP _val_labels
correct = Np.count_nonzero (matches) accuracy = correct*100.0/Len(Result)Print(accuracy)In this paper, the related algorithms are reconstructed, mainly the following aspects1, the function that obtains the cross data set and several functions of creating features are reconstructed, so the algorithm flow is clear;2. Added abnormal control to avoid errors;3, added some process control. Results:1917 Pos in a data setand 3978 neg .Create and train a SVM modeltesting on a test set96.09838846480068
From for notes (Wiz)
List of attachments
PY4CV Example 2.5 license plate recognition and SVM algorithm reconstruction