1 | initial version |
if you train your svm on hog features, you need the same hog features for the prediction as well !
(you can't simply throw an image at the prediction)
#
# train it:
#
hog = cv2.HOGDescriptor()
train_list = []
response_list = []
for i in range(2,7):
img = cv2.imread(str(i) + "_.png", cv2.IMREAD_GRAYSCALE) ## GRAYSCALE, please!
h = hog.compute(img)
train_list.append(h)
response_list.append(i)
model = cv2.SVM()
x = model.train(np.array(train_list), np.array(response_list))
model.save('trained.xml')
#
# predict:
#
svm = cv2.SVM()
svm.load('trained.xml')
img = cv2.imread('2.jpg',cv2.IMREAD_GRAYSCALE)
h = hog.compute(img)
p = svm.predict(h)