opencv 3.2 python svm problem
hi ,I have trouble with predict hog image data with SVM.i have trained my own model using two image categories.and labelled as 0 and 1. created svm_data.dat file based on this example http://docs.opencv.org/3.1.0/dd/d3b/t...
import cv2
import numpy as np
SZ=20
bin_n = 16 # Number of bins
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
def hog(img):
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 64 bit vector
return hist
svm=cv2.ml.SVM_load('svm_data.dat') // train data
img=cv2.imread('test.jpg',1)
test=hog(img)
test1 = np.float32(test).reshape(-1,64)
y=svm.predict(test1)
print y
######results for any image gives below one
(0.0, array([[ 0.]], dtype=float32))
what's the "trouble", - exactly ?
btw, somehow, i think, you want a grayscale image as input, not a color one.
actually, i need predict my input image(test.jpg) using SVM model...but always its gives the same answer((0.0, array([[ 0.]], dtype=float32)).
that's probably not the answer, you wanted, but still - a valid one. ;(
so, if your prediction was bad, so was your train input, probably, which is ?
sorry...my question is ..i have trained svm model using negative and positive colour image data.size 100*80. for that, i have used (http://docs.opencv.org/3.1.0/dd/d3b/t...) this example is given in website (little modified for my purpose..I think its fine). then save to data file. after reading a random image from my image collection(same size colour image)..predict with svm.predict function to get label result..problem is its always given for the zero(0) for all positives and negative samples. this is the problem I have faced.
how do you setup the SVM?
(and usually, HOG features are made from grayscale, not color images)
this is how i made SVM data file...yes it is a grayscale image
hmm, LINEAR should be ok, gamma is not used for linear, maybe the C value is too low ?
if you're using grayscale images now for training, don't forget to update your test code, too !
yes i have updated for img=cv2.imread('test.jpg',1) to img=cv2.imread('test.jpg',0) and tested again...not working yet.
any error in above code explanation??
hmm, not really.
did you try trainAuto() ?
normalizing the hog histograms might be a good idea, too.