Repository: HandsomeHans/SVM-classification-localization Branch: master Commit: a40c48689328 Files: 12 Total size: 28.8 KB Directory structure: gitextract_laln79t4/ ├── .github/ │ └── FUNDING.yml ├── 10_EdgeBoxes+SVM+NMS_cam.py ├── 1_HoG_extract_feature.py ├── 2_Train_SVM.py ├── 3_Test_SVM.py ├── 4_Train_PCA+SVM.py ├── 5_Test_PCA+SVM.py ├── 6_PSO+PCA.py ├── 7_Hard_Negative_Mining+SVM.py ├── 8_SlidingWindow+SVM+NMS_image.py ├── 9_SlidingWindow+SVM+NMS_cam.py └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] patreon: # Replace with a single Patreon username open_collective: # Replace with a single Open Collective username ko_fi: # Replace with a single Ko-fi username tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry liberapay: # Replace with a single Liberapay username issuehunt: # Replace with a single IssueHunt username otechie: # Replace with a single Otechie username custom: https://raw.githubusercontent.com/HansRen1024/HansRen1024.github.io/master/Get_paid_QR.jpg ================================================ FILE: 10_EdgeBoxes+SVM+NMS_cam.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Jul 4 15:10:57 2017 @author: hans """ from imutils.object_detection import non_max_suppression from edge_boxes_python import edge_boxes_python import numpy as np from skimage.feature import hog from sklearn.externals import joblib import cv2 import os import time def rgb2gray(im): gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140 return gray def getFeat(data): normalize = True visualize = False block_norm = 'L2-Hys' cells_per_block = [2,2] pixels_per_cell = [20,20] orientations = 9 gray = rgb2gray(data)/255.0 fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize) return fd if __name__ == "__main__": model_path = './models/svm_pso_less_hnm_50.model' clf = joblib.load(model_path) c = cv2.VideoCapture(0) num = 0 while 1: t0 = time.time() num += 1 ret, image = c.read() rects = [] eb = edge_boxes_python(os.path.expanduser('~') + '/HoG_SVM/cup/sf.dat') bbs = eb.get_edge_boxes(image) for (xmin, ymin, width, height, hb) in bbs[0:10]: xmin = int(xmin) ymin = int(ymin) width = int(width) height = int(height) win = image[ymin:ymin + height, xmin:xmin + width] window = cv2.resize(win,(200,200),interpolation=cv2.INTER_CUBIC) win_fd = getFeat(window) win_fd.shape = 1,-1 result = int(clf.predict(win_fd)) if result == 1: rects.append([xmin, ymin, xmin + width, ymin + height]) if len(rects) != 0: rects = np.array(rects) pick = non_max_suppression(rects, probs=None, overlapThresh=0.1) for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) font=cv2.FONT_HERSHEY_SIMPLEX t1 = time.time() cv2.putText(image,'%.2f' %(1/(t1-t0)),(0,30),font,0.9,(255,255,255),2) cv2.imshow("After NMS", image) cv2.waitKey(1) ================================================ FILE: 1_HoG_extract_feature.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Jun 13 10:24:50 2017 @author: hans """ from skimage.feature import hog from sklearn.externals import joblib import xml.dom.minidom as xdm import numpy as np import Image import cv2 import os import time # define parameter normalize = True visualize = False block_norm = 'L2-Hys' cells_per_block = [2,2] pixels_per_cell = [20,20] orientations = 9 # xml path train_xml_filePath = r'./train/Annotation' def getBox(childDir): f_xml = os.path.join(train_xml_filePath, '%s.xml' %childDir.split('.')[0]) # organise path xml = xdm.parse(f_xml) # load xml file filename = xml.getElementsByTagName('filename') filename = filename[0].firstChild.data.encode("utf-8") # read file name xmin = xml.getElementsByTagName('xmin') # coordinate of top left pixel xmin = int(xmin[0].firstChild.data) ymin = xml.getElementsByTagName('ymin') ymin = int(ymin[0].firstChild.data) xmax = xml.getElementsByTagName('xmax') # coordinate of down right pixel xmax = int(xmax[0].firstChild.data) ymax = xml.getElementsByTagName('ymax') ymax = int(ymax[0].firstChild.data) box = (xmin,ymin,xmax,ymax) return box def getDataWithCrop(filePath,label): Data = [] num = 0 for childDir in os.listdir(filePath): f_im = os.path.join(filePath, childDir) image = Image.open(f_im) # open the image box = getBox(childDir) region = image.crop(box) # cut off image data = np.asarray(region) # put the data of image into an N-dinimeter array data = cv2.resize(data,(200,200),interpolation=cv2.INTER_CUBIC) # resize image data = np.reshape(data, (200*200,3)) data.shape = 1,3,-1 fileName = np.array([[childDir]]) datalebels = zip(data, label, fileName) # organise data Data.extend(datalebels) # pou the organised data into a list num += 1 print "%d processing: %s" %(num,childDir) return Data,num def getData(filePath,label): # get the full image without cutting Data = [] num = 0 for childDir in os.listdir(filePath): f = os.path.join(filePath, childDir) data = cv2.imread(f) data = cv2.resize(data,(200,200),interpolation=cv2.INTER_CUBIC) data = np.reshape(data, (200 * 200,3)) data.shape = 1,3,-1 fileName = np.array([[childDir]]) datalebels = zip(data, label, fileName) Data.extend(datalebels) num += 1 print "%d processing: %s" %(num,childDir) return Data,num def getFeat(Data,mode): # get and save feature valuve num = 0 for data in Data: image = np.reshape(data[0], (200, 200, 3)) gray = rgb2gray(image)/255.0 # trans image to gray fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize) # fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize) fd = np.concatenate((fd, data[1])) # add label in the end of the array filename = list(data[2]) fd_name = filename[0].split('.')[0]+'.feat' # set file name if mode == 'train': fd_path = os.path.join('./features/train/', fd_name) else: fd_path = os.path.join('./features/test/', fd_name) joblib.dump(fd, fd_path,compress=3) # save data to local num += 1 print "%d saving: %s." %(num,fd_name) def rgb2gray(im): gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140 return gray if __name__ == '__main__': t0 = time.time() # deal with Positive test dataset and trainset with cutting Ptrain_filePath = r'./train/positive' Ptest_filePath = r'./test/positive' PTrainData,P_train_num = getDataWithCrop(Ptrain_filePath,np.array([[1]])) getFeat(PTrainData,'train') PTestData,P_test_num = getData(Ptest_filePath,np.array([[1]])) getFeat(PTestData,'test') # deal with positive trainset without cutting Pres_train_filePath = r'./train/positive_rest' PresTrainData,Pres_train_num = getData(Pres_train_filePath,np.array([[1]])) getFeat(PresTrainData,'train') # deal with negative test dataset and train dataset without cutting Ntrain_filePath = r'./train/negative' Ntest_filePath = r'./test/negative' NTrainData,N_train_num = getData(Ntrain_filePath,np.array([[0]])) getFeat(NTrainData,'train') NTestData,N_test_num = getData(Ntest_filePath,np.array([[0]])) getFeat(NTestData,'test') t1 = time.time() print "------------------------------------------------" print "Train Positive: %d" %(P_train_num + Pres_train_num) print "Train Negative: %d" %N_train_num print "Train Total: %d" %(P_train_num + Pres_train_num + N_train_num) print "------------------------------------------------" print "Test Positive: %d" %P_test_num print "Test Negative: %d" %N_test_num print "Test Total: %d" %(P_test_num+N_test_num) print "------------------------------------------------" print 'The cast of time is:%f'%(t1-t0) ================================================ FILE: 2_Train_SVM.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Jun 15 16:38:03 2017 @author: hans """ import sklearn.svm as ssv from sklearn.externals import joblib import glob import os import time if __name__ == "__main__": model_path = './models/svm.model' train_feat_path = './features/train' fds = [] labels = [] num=0 for feat_path in glob.glob(os.path.join(train_feat_path, '*.feat')): num += 1 data = joblib.load(feat_path) fds.append(data[:-1]) labels.append(data[-1]) print "%d Dealing with %s" %(num,feat_path) t0 = time.time() #------------------------SVM-------------------------------------------------- clf = ssv.SVC(kernel='rbf') # for training initial model # clf = ssv.SVC(kernel='rbf', C=17.255220940030252, gamma=1.2943653125547475e-06) # for training svm_pso.model(origin model) print "Training a SVM Classifier." clf.fit(fds, labels) joblib.dump(clf, model_path) #------------------------SVM-------------------------------------------------- t1 = time.time() print "Classifier saved to {}".format(model_path) print 'The cast of time is :%f seconds' % (t1-t0) ================================================ FILE: 3_Test_SVM.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Jun 15 16:44:53 2017 @author: hans """ from sklearn.externals import joblib import glob import os import time if __name__ == "__main__": model_path = './models/svm.model' test_feat_path = './features/test' total=0 num=0 t0 = time.time() clf = joblib.load(model_path) for feat_path in glob.glob(os.path.join(test_feat_path, '*.feat')): total += 1 print "%d processing: %s" %(total, feat_path) data_test = joblib.load(feat_path) data_test_feat = data_test[:-1].reshape((1,-1)) result = clf.predict(data_test_feat) if int(result) == int(data_test[-1]): num += 1 rate = float(num)/total t1 = time.time() print 'The classification accuracy is %f' %rate print 'The cast of time is :%f seconds' % (t1-t0) ================================================ FILE: 4_Train_PCA+SVM.py ================================================ #!/usr/bin/env python #encoding:utf-8 """ Created on Thu Jun 15 17:29:22 2017 @author: hans """ import numpy as np import sklearn.svm as ssv from sklearn.externals import joblib import glob import os import time m = '20pixel' # HoG choses pixel per cell will gain different number of feature values. def zeroMean(dataMat): # zero normalisation meanVal=np.mean(dataMat,axis=0) # calculate mean value of every column. joblib.dump(meanVal,'./features/PCA/%s/meanVal_train_%s.mean' %(m,m)) # save mean value newData=dataMat-meanVal return newData,meanVal def pca(dataMat,n): print "Start to do PCA..." t1 = time.time() newData,meanVal=zeroMean(dataMat) covMat=np.cov(newData,rowvar=0) eigVals,eigVects=np.linalg.eig(np.mat(covMat)) # calculate feature value and feature vector joblib.dump(eigVals,'./features/PCA/%s/eigVals_train_%s.eig' %(m,m),compress=3) joblib.dump(eigVects,'./features/PCA/%s/eigVects_train_%s.eig' %(m,m),compress=3) # eigVals = joblib.load('./features/PCA/%s/eigVals_train_%s.eig' %(m,m)) # eigVects = joblib.load('./features/PCA/%s/eigVects_train_%s.eig' %(m,m)) eigValIndice=np.argsort(eigVals) # sort feature value n_eigValIndice=eigValIndice[-1:-(n+1):-1] # take n feature value n_eigVect=eigVects[:,n_eigValIndice] # take n feature vector joblib.dump(n_eigVect,'./features/PCA/%s/n_eigVects_train_%s_%s.eig' %(m,m,n)) lowDDataMat=newData*n_eigVect # calculate low dimention data # reconMat=(lowDDataMat*n_eigVect.T)+meanVal t2 = time.time() print "PCA takes %f seconds" %(t2-t1) return lowDDataMat if __name__ == "__main__": n = 100 # this is to define how dimentions u want model_path = './models/%s/svm_%s_pca_%s.model' %(m,m,n) train_feat_path = './features/train' fds = [] labels = [] num=0 for feat_path in glob.glob(os.path.join(train_feat_path, '*.feat')): num += 1 data = joblib.load(feat_path) fds.append(data[:-1]) labels.append(data[-1]) print "%d Dealing with %s" %(num,feat_path) #------------------------PCA-------------------------------------------------- fds = np.array(fds,dtype = int) #TODO, force to int format may damage the value to be all 0. fds.shape = 2327,-1 # 2327 is the number of trainset fds= pca(fds,n) #------------------------PCA-------------------------------------------------- t0 = time.time() #------------------------SVM-------------------------------------------------- clf = ssv.SVC(kernel='rbf') print "Training a SVM Classifier." clf.fit(fds, labels) joblib.dump(clf, model_path) #------------------------SVM-------------------------------------------------- t1 = time.time() print "Classifier saved to {}".format(model_path) print 'The cast of time is :%f seconds' % (t1-t0) ================================================ FILE: 5_Test_PCA+SVM.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Jun 15 17:29:22 2017 @author: hans """ from sklearn.externals import joblib import glob import os import time m = '20pixel' n = 100 if __name__ == "__main__": model_path = './models/%s/svm_%s_pca_%s.model' %(m,m,n) test_feat_path = './features/test' total=0 num=0 t0 = time.time() clf = joblib.load(model_path) for feat_path in glob.glob(os.path.join(test_feat_path, '*.feat')): total += 1 print "%d processing: %s" %(total, feat_path) data_test = joblib.load(feat_path) data_test_feat = data_test[:-1].reshape((1,-1)) #------------------------PCA-------------------------------------------------- meanVal = joblib.load('./features/PCA/%s/meanVal_train_%s.mean' %(m,m)) data_test_feat = data_test_feat - meanVal n_eigVects = joblib.load('./features/PCA/%s/n_eigVects_train_%s_%s.eig' %(m,m,n)) data_test_feat = data_test_feat * n_eigVects #------------------------PCA-------------------------------------------------- result = clf.predict(data_test_feat) if int(result) == int(data_test[-1]): num += 1 rate = float(num)/total t1 = time.time() print 'The classification accuracy is %f' %rate print 'The cast of time is :%f seconds' % (t1-t0) ================================================ FILE: 6_PSO+PCA.py ================================================ from sklearn.externals import joblib import os from svmutil import svm_train import numpy as np import glob import random import copy n = 2000 train_feat_path = './features/train' birds = 20 # size of population maxgen = 50 pos = [] # population of class speed = [] bestpos = [] initpos = [] tempfit = [] birdsbestpos = [] fds = [] dict_fds = [] labels = [] allbestpos = [] w = 1 # best belongs to [0.8,1.2] c1 = 2 c2 = 2 r1 = random.uniform(0,1) r2 = random.uniform(0,1) m = 'pso' def zeroMean(dataMat): meanVal=np.mean(dataMat,axis=0) # joblib.dump(meanVal,'./features/PCA/meanVal_train_%s.mean' %m) newData=dataMat-meanVal return newData,meanVal def pca(dataMat,n): print "Start to do PCA..." newData,meanVal=zeroMean(dataMat) # covMat=np.cov(newData,rowvar=0) # eigVals,eigVects=np.linalg.eig(np.mat(covMat)) # joblib.dump(eigVals,'./features/PCA/eigVals_train_%s.eig' %m,compress=3) # joblib.dump(eigVects,'./features/PCA/eigVects_train_%s.eig' %m,compress=3) eigVals = joblib.load('./features/PCA/eigVals_train_%s.eig' %m) eigVects = joblib.load('./features/PCA/eigVects_train_%s.eig' %m) eigValIndice=np.argsort(eigVals) n_eigValIndice=eigValIndice[-1:-(n+1):-1] n_eigVect=eigVects[:,n_eigValIndice] # joblib.dump(n_eigVect,'./features/PCA/n_eigVects_train_%s_%s.eig' %(m,n)) lowDDataMat=newData*n_eigVect return lowDDataMat for feat_path in glob.glob(os.path.join(train_feat_path, '*.feat')): data = joblib.load(feat_path) fds.append(data[:-1]) labels.append(data[-1]) fds = np.array(fds,dtype = float) fds= pca(fds,n) fds = np.array(fds,dtype = float) for i in range(len(fds[:,0])): dict_data = dict(zip(range(len(data))[1:],fds[i,:])) dict_fds.append(dict_data) for i in range(birds): pos.append([]) speed.append([]) bestpos.append([]) initpos.append([]) tempfit.append([]) def CalDis(list): fitness=0.0 param = '-t 2 -v 3 -c %s -g %s' %(list[0],list[1]) fitness = svm_train(labels, dict_fds, param) return fitness for i in range(birds): #initial all birds' pos,speed pos[i].append(random.uniform(10,30)) pos[i].append(random.uniform(0.5e-06, 1e-06)) # 1/num_features speed[i].append(float(0)) speed[i].append(float(0)) # speed[i].append(random.uniform(-10,10)) # speed[i].append(random.uniform(-0.00002,0.00002)) bestpos[i] = copy.deepcopy(pos[i]) initpos[i] = copy.deepcopy(pos[i]) def FindBirdsMostPos(): best=CalDis(bestpos[0]) index = 0 for i in range(birds): print "\n>>>>>The %d'd time to find globel best pos. Total %d times.\n" %(i+1, birds) tempfit[i] = CalDis(bestpos[i]) if tempfit[i] > best: best = tempfit[i] index = i print '------- %d: %f' %(index, best) return best, bestpos[index] print "\n-------------------------Initial Globel Best Pos----------------------------------\n" best_predict, birdsbestpos = FindBirdsMostPos() #initial birdsbestpos print "\n-------------------------Done Globel Best Pos----------------------------------\n" def NumMulVec(num,list): #result is in list for i in range(len(list)): list[i] *= num return list def VecSubVec(list1,list2): #result is in list1 for i in range(len(list1)): list1[i] -= list2[i] return list1 def VecAddVec(list1,list2): #result is in list1 for i in range(len(list1)): list1[i] += list2[i] return list1 def UpdateSpeed(): #global speed for i in range(birds): temp1 = NumMulVec(w,speed[i][:]) temp2 = VecSubVec(bestpos[i][:],pos[i]) temp2 = NumMulVec(c1*r1,temp2[:]) temp1 = VecAddVec(temp1[:],temp2) temp2 = VecSubVec(birdsbestpos[:],pos[i]) temp2 = NumMulVec(c2*r2,temp2[:]) speed[i] = VecAddVec(temp1,temp2) def UpdatePos(): print "Update Pos." global bestpos,birdsbestpos,tempfit for i in range(birds): if pos[i][0]+speed[i][0] > 0 and pos[i][1]+speed[i][1] > 0: VecAddVec(pos[i],speed[i]) if CalDis(pos[i]) > tempfit[i]: bestpos[i] = copy.deepcopy(pos[i]) best_predict, birdsbestpos = FindBirdsMostPos() return best_predict, birdsbestpos for asd in range(maxgen): print "\n>>>>>>>>The %d'd time to update parameters. Total %d times\n" %(asd+1, maxgen) UpdateSpeed() best_predict, best_para = UpdatePos() allbestpos.append([best_para, best_predict]) f=open('result/PSO_%s-%s-%s.txt' %(birds,maxgen,n),'w') f.write(str(allbestpos)) f.close() print "After %d iterations\nthe best C is: %f\nthe best gamma is: %f" %(maxgen,best_para[0],best_para[1]) ================================================ FILE: 7_Hard_Negative_Mining+SVM.py ================================================ #!/usr/bin/env python #encoding:utf-8 import numpy as np import sklearn.svm as ssv from sklearn.externals import joblib from skimage.feature import hog import random import glob import os import cv2 def trainSvm(datas, labels): clf = ssv.SVC(kernel='rbf', C=17.255220940030252, gamma=1.2943653125547475e-06) #20,50,500,0.842850 print "Training a SVM Classifier." clf.fit(datas, labels) return clf def loadData(path): fds_all = [] # fds = [] # labels = [] num=0 for feat_path in glob.glob(os.path.join(path, '*.feat')): num += 1 data = joblib.load(feat_path) fds_all.append(data) # fds.append(data[:-1]) # labels.append(data[-1]) print "%d Dealing with %s" %(num,feat_path) return fds_all def sliding_window(image, stepSize, windowSize): for y in xrange(0, image.shape[0], stepSize): for x in xrange(0, image.shape[1], stepSize): yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) def rgb2gray(im): gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140 return gray def savefeat(childDir, num_win, fd): fd_name = childDir.split('.')[0] + '_%d.feat' %num_win fd_path = os.path.join('./features/train_hnm/', fd_name) joblib.dump(fd, fd_path,compress=3) if __name__ == "__main__": normalize = True visualize = False block_norm = 'L2-Hys' cells_per_block = [2,2] pixels_per_cell = [20,20] orientations = 9 new_model_path = './models/svm_pso_hnm.model' train_feat_path = './features/train_' fds_all = loadData(train_feat_path) model_path = './models/svm_pso.model' clf = joblib.load(model_path) #--------------------hard_negative_mining------------------------------------- negative_img_path = './train/negative' num = 0 for childDir in os.listdir(negative_img_path): num += 1 num_win = 0 print "num: %d hard negative mining: %s" %(num,childDir) f = os.path.join(negative_img_path, childDir) data = cv2.imread(f) scales = [(100, 100), (200,200), (300,300), (400,400), (500, 500), (600,600), (800, 800)] for (winW,winH) in scales: for (x, y, window) in sliding_window(data, stepSize=100, windowSize=(winW,winH)): result = 0 if window.shape[0] != winH or window.shape[1] != winW: continue if window.shape[0] != 200 or window.shape[1] != 200: window = cv2.resize(window,(200,200),interpolation=cv2.INTER_CUBIC) gray = rgb2gray(window)/255.0 window_fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize) win_fd = window_fd.reshape(1, -1) result = int(clf.predict(win_fd)) if result == 1: num_win += 1 fd = np.concatenate((window_fd, (float(0),))) fds_all.append(fd) savefeat(childDir, num_win, fd) # fds.append(window_fd) # labels.append(float(0)) random.shuffle(fds_all) fds = np.numpy(fds_all)[:, :-1] labels = np.numpy(fds_all)[:, -1] new_clf = trainSvm(fds, labels) #----------------------------------------------------------------------------- joblib.dump(new_clf, new_model_path) print "Classifier saved to {}".format(new_model_path) ================================================ FILE: 8_SlidingWindow+SVM+NMS_image.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Jul 4 15:10:57 2017 @author: hans """ from imutils.object_detection import non_max_suppression import numpy as np from skimage.feature import hog from sklearn.externals import joblib import time import cv2 normalize = True visualize = False block_norm = 'L2-Hys' cells_per_block = [2,2] pixels_per_cell = [20,20] orientations = 9 def rgb2gray(im): gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140 return gray def getFeat(data): gray = rgb2gray(data)/255.0 fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize) return fd def sliding_window(image, stepSize, windowSize): # slide a window across the image for y in xrange(0, image.shape[0], stepSize): for x in xrange(0, image.shape[1], stepSize): # yield the current window yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) if __name__ == "__main__": # image_path = str(raw_input("Please enter the path of an image: ")) image_path = 'test/positive/n03147509_3599.JPEG' t0 = time.time() model_path = './models/svm_pso.model' clf = joblib.load(model_path) image = cv2.imread(image_path) image = cv2.resize(image,(500,500),interpolation=cv2.INTER_CUBIC) orig = image.copy() orig = cv2.resize(orig,(500,500),interpolation=cv2.INTER_CUBIC) rects = [] scales = [(200,200), (300,300), (400, 400), (image.shape[1],image.shape[0])] for (winW,winH) in scales: for (x, y, window) in sliding_window(image, stepSize=90, windowSize=(winW,winH)): result = 0 if window.shape[0] != winH or window.shape[1] != winW: continue cv2.imshow("asd", window) cv2.waitKey(0) print window.shape if window.shape[0] != 200 or window.shape[1] != 200: window = cv2.resize(window,(200,200),interpolation=cv2.INTER_CUBIC) win_fd = getFeat(window) win_fd.shape = 1,-1 result = int(clf.predict(win_fd)) print 'smamll image result is %d' %result if result == 1: rects.append([x, y, x + winW, y + winH]) cv2.rectangle(orig, (x, y), (x + winW, y + winH), (0, 0, 255), 2) rects = np.array(rects) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) t1 = time.time() print 'The cast of time is :%f seconds' % (t1-t0) cv2.imshow("Before NMS", orig) cv2.imshow("After NMS", image) cv2.waitKey(0) ================================================ FILE: 9_SlidingWindow+SVM+NMS_cam.py ================================================ #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Jul 4 15:10:57 2017 @author: hans """ from imutils.object_detection import non_max_suppression import numpy as np from skimage.feature import hog from sklearn.externals import joblib import cv2 def rgb2gray(im): gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140 return gray def getFeat(data): normalize = True visualize = False block_norm = 'L2-Hys' cells_per_block = [2,2] pixels_per_cell = [20,20] orientations = 9 gray = rgb2gray(data)/255.0 fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize) return fd def sliding_window(image, stepSize, windowSize): for y in xrange(0, image.shape[0], stepSize): for x in xrange(0, image.shape[1], stepSize): yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) if __name__ == "__main__": model_path = './models/svm_pso_85_hnm_50.model' clf = joblib.load(model_path) c = cv2.VideoCapture(0) while 1: ret, image = c.read() rects = [] # image = cv2.resize(image,(500,500),interpolation=cv2.INTER_CUBIC) scales = [(200,200), (300,300)] # scales = [(200,200), (250, 250), (300,300)] for (winW,winH) in scales: for (x, y, window) in sliding_window(image, stepSize=100, windowSize=(winW,winH)): result = 0 if window.shape[0] != winH or window.shape[1] != winW: continue if window.shape[0] != 200 or window.shape[1] != 200: window = cv2.resize(window,(200,200),interpolation=cv2.INTER_CUBIC) win_fd = getFeat(window) win_fd.shape = 1,-1 result = int(clf.predict(win_fd)) if result == 1: rects.append([x, y, x + winW, y + winH]) rects = np.array(rects) pick = non_max_suppression(rects, probs=None, overlapThresh=0.1) minx = 10000 miny = 10000 maxx = 0 maxy = 0 for (xA, yA, xB, yB) in pick: if xA < minx: minx = xA if yA < miny: miny = yA if xB > maxx: maxx = xB if yB > maxy: maxy = yB if (abs(maxx - minx) < image.shape[1]) and (abs(maxy - miny) < image.shape[0]): cv2.rectangle(image, (minx, miny), (maxx, maxy), (0, 255, 0), 2) cv2.imshow("After NMS", image) cv2.waitKey(1) ================================================ FILE: README.md ================================================ # SVM-classification-detection (Python2.7) HoG, PCA, PSO, Hard Negative Mining, Sliding Window, NMS ![image](https://github.com/HansRen1024/SVM-classification-localization/blob/master/example.gif) Best way to do detection is: HoG(features) -> PCA(less features) + PSO(best C&gamma) -> origin SVM -> HNM(more features) -> better SVM -> SW -> NMS(bbox regression) Sorry for my laziness. I think I should clarify the steps for the program. 1. Extract HoG features (script 1) 2. Train an initial model for pso (script 2) 3. Do pca and pso for better parameters C and gamma (script 6) 4. Use no-pca features and the best parameters to train the second model (script 2) 5. In order to increase the accuracy, use the second model to do hnm and get the final model(script 7) 6. Finally, choose an algorithm you like to do location(script 8 or 9 or 10) **PS:** 1. The reason I use pca is to accelerate the speed of pso. To be honestly, pso is really slow. 2. For step 4, you can also use features processed by pca, but I strongly advise you to hold as possible as more features. Because more features, higher accuracy. 杯子数据集(Dataset): https://pan.baidu.com/s/18ho4UI50x4YP6lkrjPm7Kw