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
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'):
            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))
            if len(faces) > 0:
                response = model.predict(np.array(X_test))

1 Answer 1


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.


Your Answer

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

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