├── README.md ├── test.py └── hog.py /README.md: -------------------------------------------------------------------------------- 1 | # Face_Detection_HOG 2 | Face Detection using HOG and SVM 3 | The training file for the data is hog.py 4 | The testing file is test.py...... 5 | 6 | Now to train on more data... Update the file hog.py accordingly. 7 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from sklearn.externals import joblib 3 | from skimage.feature import hog 4 | import numpy as np 5 | 6 | h = 128 7 | w = 64 8 | 9 | # Load the classifier 10 | clf = joblib.load("hog.pkl") 11 | 12 | # Read the input image 13 | im = cv2.imread("image1048.tif") 14 | 15 | img=cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 16 | img=cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC) 17 | 18 | roi_hog_fd = hog(img, orientations=105, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False) 19 | 20 | nbr = clf.predict(np.array([roi_hog_fd], 'float64')) 21 | 22 | print nbr 23 | -------------------------------------------------------------------------------- /hog.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import os 4 | from sklearn.svm import SVC 5 | import matplotlib.pyplot as plt 6 | from sklearn.externals import joblib 7 | #import scikit-image.skimage.data 8 | #, color, exposure 9 | 10 | 11 | array=np.array([]) #empty array for storing all the features 12 | h = 128 #height of the image 13 | w = 64 #width of the image 14 | hog=cv2.HOGDescriptor() 15 | img=cv2.imread('image1048.tif',0) 16 | img1=cv2.imread('image1049.tif',0) 17 | 18 | img=cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC) #resize images 19 | img1=cv2.resize(img1, (w, h), interpolation=cv2.INTER_CUBIC) 20 | for i in range(0,2): 21 | if i==0: 22 | h=hog.compute(img,winStride=(64,128),padding=(0,0)) #storing HOG features as column vector 23 | #hog_image_rescaled = exposure.rescale_intensity(h, in_range=(0, 0.02)) 24 | #plt.figure(1, figsize=(3, 3)) 25 | #plt.imshow(h,cmap=plt.cm.gray) 26 | #plt.show() 27 | #print len(h) 28 | h_trans=h.transpose() #transposing the column vector 29 | 30 | array=np.vstack(h_trans) #appending it to the array 31 | print "HOG features of label 1" 32 | print array 33 | 34 | else: 35 | h=hog.compute(img1,winStride=(64,128),padding=(0,0)) #storing HOG features as column vector 36 | 37 | #hog_image_rescaled = exposure.rescale_intensity(h, in_range=(0, 0.02)) 38 | #plt.figure(1, figsize=(3, 3)) 39 | #plt.imshow(h,cmap=plt.cm.gray) 40 | #plt.show() 41 | #print len(h) 42 | h_trans=h.transpose() #transposing the column vector 43 | 44 | array=np.vstack((array,h_trans)) #appending it to the array 45 | print "HOG features of label 1 & 2" 46 | print (array) 47 | 48 | #print (array.shape) 49 | 50 | 51 | label=[1,4] 52 | 53 | clf=SVC(gamma=0.001,C=10) 54 | 55 | clf.fit(array,label) 56 | 57 | #ypred = clf.predict() 58 | joblib.dump(clf, "hog.pkl", compress=3) 59 | --------------------------------------------------------------------------------