Ask Your Question
0

How to predict HOG features each frame with trained SVM classifier (and possibly get the accuracy of the prediction)

asked 2018-04-04 04:54:15 -0500

LeBorzi gravatar image

I created and trained an SVM emotion classifier using a set of 700 image samples and after applying some testing, it scored very well. My problem is that when I use the svm.predict function that I used to score the classifier on the current frame (after I extracted the HOG features), I get the error "samples.cols == var_count && samples.type() == 5 in function predict". I made sure my extracted features are float32 numpy arrays. If someone could help me find out why this is happening and how to solve it, I would really appreciate it! Thank you in advance.

# Create video capture object
    video_capture = cv2.VideoCapture(0);
    # Start creating loop, using webcam image every frame
    while True:
        # read method returns two elements, the last being the last frame captured by the camera - the _ allows us to ignore the first element
        _, frame = video_capture.read();
        # convert the camera frame into a grayscale version
        grayscale_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Create dependancies for live detection
        aoi = grayscale_frame;
        aoi = pre_process_img(aoi);
        # Extract feature
        current_frame_features = hog.compute(aoi, (64, 64));
        # Convert features to numpay array
        current_frame_features = np.array(current_frame_features, dtype = np.float32);
        # Use classifier to make a prediction
        predicted_label = my_svm.predict(current_frame_features);
        # Print the prediction on our frame
        display_text = "Emotion: " + str(predicted_label);
        font = cv2.FONT_HERSHEY_SIMPLEX;
        cv2.putText(frame, display_text, (10, 50), font, 1, (255,255,255), 2, cv2.LINE_AA);
        # Use open CV to display the final camera frame
        cv2.imshow('Video', frame);
        # implement way to discontinue the application
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    video_capture.release();
    # destroy window
    cv2.destroyAllWindows();
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-04-04 05:22:24 -0500

berak gravatar image

updated 2018-04-04 05:42:53 -0500

hog.compute() returns a single column (a "vertical" array), while you probably need a single row here

look at your training code, and reshape() it in the same way

then, while there is a RAW_OUTPUT flag for the SVM prediction, it only would return the distance to the margin, not a probability, and using that would only make sense in a binary case (single support vector, yours is probably multi-class).

so, while an estimation for a single prediction is difficult, you could still run your classifier on a large (labelled) testset, and make a confusion matrix, to see how good it is in general.

edit flag offensive delete link more

Comments

Thanks for your reply! I was able to reshape it, by making current_frame_features a new list and appending a feature for each detected face. From what I understand, based on what you mentioned, doing a live detection won't work (because I have 0 - 7 labels)? After running my code, I seem to be getting the same prediction repeatedly. Is there a good way to make a confusion matrix in OpenCV? Because right now, I am only returning the overall accuracy of my classifier after testing.

LeBorzi gravatar imageLeBorzi ( 2018-04-04 06:23:16 -0500 )edit

why should a live prediction not work ?

i was only trying to point out, that obtaining a confidence value per prediction might get difficult here.

"I am only returning the overall accuracy of my classifier after testing." -- that's probably , what i meant.

berak gravatar imageberak ( 2018-04-04 06:27:48 -0500 )edit
1

Finally, I would like to add that I was able to get everything working, due to your guidance I also got a nice confusion matrix with sklearn - thank you very much!

LeBorzi gravatar imageLeBorzi ( 2018-04-04 09:23:41 -0500 )edit

ah sure, sklearn has it builtin. but it's not rocket-science at all: you make an all-zero NxN mat,(where N is numclasses), and increase confusion[expected][predicted] with every prediction.

berak gravatar imageberak ( 2018-04-04 09:27:48 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-04-04 04:54:15 -0500

Seen: 220 times

Last updated: Apr 04 '18