Ask Your Question

Always get single return in middle of image with custom SVM using hog.detectMultiScale

asked 2015-03-08 12:09:35 -0500

crd319 gravatar image

updated 2015-03-08 12:47:41 -0500

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)
        j = j+2
sv = np.asarray(sv)    
for i in range(0,len(sv)):
    while (len(sv[i]) < 3781):
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])
return primalForm

And the main detector routine:

def runDetector():
  if not (os.path.isfile('./model/hog')):

  img = cv2.imread('./INRIAPerson/Test/pos/crop_000024.png', 0)
  vect = parseSVMmodel('./model/hog')
  vect = np.asarray(vect)

  bb,w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale =1.05)
  for (x,y,w,h) in bb:

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:

image description

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 with detectMultiscale, which is much more ideal for my application that resizing the image and calling detect multiple times myself.

I've tested this on both OpenCV 2.4.10 and 3.0 and got the same retun.

edit retag flag offensive close merge delete


I experience exactly the same issue - I am getting single detection in the middle of image no matter what it contains. Have you found the solution maybe?

glukoz gravatar imageglukoz ( 2017-02-17 10:16:33 -0500 )edit

I save opencv svm model in .xml ( following this tutorial , (, and parse .xml model then save to pickle. The pickle file contains float number, and when I tried load .pickle file to hog.setSVMDetector(), error occured, hog.setSVMDetector(np.array(svm)) cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\objdetect\src\hog.cpp:117: error: (-215) checkDetectorSize() in function cv::HOGDescriptor::setSVMDetector. What are the possible causes ?

adamaulia gravatar imageadamaulia ( 2017-04-26 09:22:50 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-10-01 22:11:27 -0500

The reason why single rectangle is created at the center is because the detector classified almost all region as "human". By default, detectMultiScale suppress the overlap of the rectangles. So you can only see the single rectangle at the center. This suppression can be turned off with finalThreshold option of detectMultiScale.

bb,w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale =1.05, finalThreshold=0)

You can see almost all regions are filled by the rectangle color.

Now, please try the followings.

  • svm kernel type set to linear
  • change the sign of rho
  • change the training index (positive: 0, negative: 1)
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-03-08 12:09:35 -0500

Seen: 827 times

Last updated: Mar 08 '15