ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
52.94k stars 13.44k forks source link

open cv with KNN Classifier #650

Open BraaZain opened 5 years ago

BraaZain commented 5 years ago

is there any way to use this KNN Classifier with open cv to predict faces in a live video. i have tried to combine them but didn't find a way to do it.

ageitgey commented 5 years ago

Sure, but what did you already try? It should just involve combining the KNN example with the facerec from video example.

BraaZain commented 5 years ago

i only changed the main to take a frame from video capture and send it to the predict function instead of the image path :

if __name__ == "__main__":

    #step1: use open cv to create a video capture
    video_capture =  cv2.VideoCapture(0)

    while True:
        ret, frame = video_capture.read()
        rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        #get predictions
        predictions = predict(x_img = rgb_frame, model_path = "Classifier.plk")

        #show predictions
        result_img = show_predictions(rgb_frame, predictions)

        #display the video
        final_frame = np.array(result_img)
        final_frame = cv2.cvtColor(final_frame, cv2.COLOR_RGB2BGR)
        cv2.imshow("test", final_frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()

it worked but the video is really slow , i don't know if it is because my laptop specification or the code is slow?? could you suggest me something to fix this??