-
Notifications
You must be signed in to change notification settings - Fork 14
/
training_SVM.py
45 lines (40 loc) · 1.42 KB
/
training_SVM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from skimage.feature import hog
#from skimage.io import imread
import joblib,glob,os,cv2
from sklearn.svm import LinearSVC
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn import svm, metrics
import numpy as np
from sklearn.preprocessing import LabelEncoder
train_data = []
train_labels = []
pos_im_path = 'DATAIMAGE/positive/'
neg_im_path = 'DATAIMAGE/negative/'
model_path = 'models/models.dat'
# Load the positive features
for filename in glob.glob(os.path.join(pos_im_path,"*.png")):
fd = cv2.imread(filename,0)
fd = cv2.resize(fd,(64,128))
fd = hog(fd,orientations=9,pixels_per_cell=(8,8),visualize=False,cells_per_block=(3,3))
train_data.append(fd)
train_labels.append(1)
# Load the negative features
for filename in glob.glob(os.path.join(neg_im_path,"*.jpg")):
fd = cv2.imread(filename,0)
fd = cv2.resize(fd,(64,128))
fd = hog(fd,orientations=9,pixels_per_cell=(8,8),visualize=False,cells_per_block=(3,3))
train_data.append(fd)
train_labels.append(0)
train_data = np.float32(train_data)
train_labels = np.array(train_labels)
print('Data Prepared........')
print('Train Data:',len(train_data))
print('Train Labels (1,0)',len(train_labels))
print("""
Classification with SVM
""")
model = LinearSVC()
print('Training...... Support Vector Machine')
model.fit(train_data,train_labels)
joblib.dump(model, 'models/models.dat')
print('Model saved : {}'.format('models/models.dat'))