Search code examples
pythonopencvface-recognitionfacial-identification

How to make a face recognition(identification and verification) program?


So, I made a face detection program for webcam in Python using OpenCV2. But this just detects the face, it doesn't recognise/identify who it is. So, how to I train my program to recognise if it is me?
My code so far:


video = cv2.VideoCapture(0)

while True:
    check, frame = video.read()
    faces = face_cascade.detectMultiScale(frame,
                                          scaleFactor=1.1, minNeighbors=5)
    for x,y,w,h in faces:
        frame = cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)

    cv2.imshow('Face Detector', frame)

    key = cv2.waitKey(1)

    if key == ord('q'):
        break

video.release()
cv2.destroyAllWindows()

I also wanted to know if there was a way for the program to tell how many faces it is detecting?


Solution

  • you could use the pretrained openface model with opencv's dnn module, to get feature vectors from cropped face images, which can be easily compared with L2 or cosine distance

    get the model from here: https://storage.cmusatyalab.org/openface-models/nn4.small2.v1.t7

    net = cv2.dnn.readNetFromTorch("nn4.small2.v1.t7")
    
    # then, for each image:
    blob = cv2.dnn.blobFromImage(img, 1./255, (96,96), (0,0,0), True, False)
    net.setInput(blob)
    feature = net.forward() # 128 floats
    
    if cv2.norm(f1,f2) < 0.5:
        # SAME !!!