0
$\begingroup$

I want to my code to return multiple(5) predictions from my trained knn model. I've tried using predict_proba() but it just returns the probabilities and not the names Here is my code:

import cv2
import numpy as np
import pickle
import npwriter
m=open('finalmodel.train','rb')
model = pickle.load(m)
cap = cv2.VideoCapture(0)
classifier = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
X_test = []
while True:
        ret, frame = cap.read()
        cv2.imshow('Press c to capture your face', frame)
        if cv2.waitKey(1) & 0xFF == ord('c'):
            cv2.destroyAllWindows()
            cap.release()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = classifier.detectMultiScale(gray, 1.5, 5)
            for face in faces:
                x, y, w, h = face
                im_face = gray[y:y + h, x:x + w]
                im_face = cv2.resize(im_face, (100, 100))
                X_test.append(im_face.reshape(-1))
            if len(faces) > 0:
                response = model.predict(np.array(X_test))
                print(response)
                break
$\endgroup$

1 Answer 1

1
$\begingroup$

Instead of using the classifier you might need to use a nearest neighbor instance: https://scikit-learn.org/stable/modules/neighbors.html This will allow you to get indices of nearest neighbors. An alternative might be to look at the argmax of the output to get the "most probable" neighbors.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.