I'm working on developing a custom people detector, building off results from the base people detector. This is ultimately going to involve training custom HOG detectors so to start with, I've been attempting to just train a custom SVM using libSVM to detect full bodies to test my work flow. Once I train the SVM model, I parse the text file into the primal form so that I can set it as the SVM detector.
The SVM was trained using the INRIA data set with ~2400 positive samples and ~4000 negative samples
The parse SVM to Primal model routine (it's not pretty but it works):
def parseSVMmodel(svmModel):
primalForm = []
sv = []
lines = tuple(open(svmModel, 'r'))
x = lines[4].split()
rho = x[1]
for i in range(10, len(lines)):
st = lines[i]
st = st.replace(":", " ")
x = st.split()
idx = 1
j = 1
while (j < len(x)):
if (idx != int(x[j])):
x.insert(j, idx)
x.insert(j+1, 0)
idx=idx+1
j = j+2
sv.append(x[::2])
sv = np.asarray(sv)
for i in range(0,len(sv)):
while (len(sv[i]) < 3781):
sv[i].append(0)
for j in range(1, len(sv[0])):
value = 0
for i in range(0, len(sv)):
value = value + ((-1)*float(sv[i][0]))*float(sv[i][j])
primalForm.append(value)
primalForm.append(rho)
return primalForm
And the main detector routine:
def runDetector():
if not (os.path.isfile('./model/hog')):
trainDetector(hog)
img = cv2.imread('./INRIAPerson/Test/pos/crop_000024.png', 0)
vect = parseSVMmodel('./model/hog')
vect = np.asarray(vect)
hog.setSVMDetector(vect.astype(np.float))
bb,w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale =1.05)
for (x,y,w,h) in bb:
cv2.rectangle(img,(int(x),int(y)),(int((x+w)),int((y+h))),(255,0,0),2)
cv2.imshow('Hog',img)
Running the above code, no matter what input image, always gives me a single positive return in the exact center of the image, as seen below:
Using just hog.detect gives multiple returns (with a large number of false alarms, but I plan to correct that with bootstrapping regardless) but I fail to understand why I always get such a return.
I've tested this on both OpenCV 2.4.10 and 3.0 and got the same retun.