Ask Your Question

Revision history [back]

Why does my custom HOG detector always draws a rectangle on the center of image even when I have obtained good accuracies on Test data?

I am trying to make a custom HOG detector. Before working on my dataset, I am trying to verify the results on INRIA dataset. I have extracted features from hog.compute function and then used those features to train an SVM classifier.

Using the svm.predict command, I tested the accuracies on test data provided by INRIA. I got 97.3% accuracy on pos test data, and 99.6% on neg test data (neg images were resized to 64x128. I tried with cropping to 64x128 as well and the accuracy was 95.4%)

I used the following code to find the support vectors and rho to provide to the setSVMdetector function:

(rho, alpha, supportVectorIndices) = svm.getDecisionFunction(0)
supportVectors = svm.getSupportVectors().ravel()
supportVectors = np.append(supportVectors, -rho)
pickle.dump(supportVectors, open("svm.pickle", 'wb'))

However, when I run the code, the rectangle is always drawn in the center before or after the NMS. Check here: image description

Below is the code provided by OpenCV for HOG detector. I have just made a change in the setSVMDetector function where I am providing my trained support vectors now.

hog = cv2.HOGDescriptor() svm = pickle.load(open("svmcoeff.pickle", 'rb')) hog.setSVMDetector(np.array(svm))

for i in range(1,9): image = cv2.imread('test-'+str(i)+'.png') image = imutils.resize(image, width=min(300, image.shape[1])) orig = image.copy() (rects, weights) = hog.detectMultiScale(image)

for (x, y, w, h) in rects: cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255),2) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None,overlapThresh=0.65)

for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) cv2.imshow("Before NMS", orig) cv2.imshow("After NMS", image) key = cv2.waitKey(0) if key == 27: continue