it is possible to create a real time face recognition with openCV and Face-recognition library?

Viewed 29

I am trying to build a face recognition in Python

at first I build a face detection with openCV and using documentation example in Face-Recognition library I did that

and right now im trying to merge those two library and I get an error heres are my example code im trying build

import cv2
import pathlib
import face_recognition


casface = pathlib.Path(cv2.__file__).parent.absolute() / "data/haarcascade_frontalface_default.xml"

clf = cv2.CascadeClassifier(str(casface))

video = cv2.VideoCapture(0)

picture_of_me = face_recognition.load_image_file("images/art.jpg") # my image location
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]


while True:
    suc, img = video.read()
    frame = cv2.flip(img, 1) # Flip camera vertically
    vds_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    small_frame = cv2.resize(img,(0,0), fx=0.25,fy=0.25)
    # smalller_fram = small_frame[:,:,::-1]
    
    face_detect = clf.detectMultiScale(
        vds_gray,
        scaleFactor=1.2,
        minNeighbors=1,
        minSize=(30,30),
        )
    

    # the faces detected on my webcam and encode it on facerecognition
    face_locas = face_recognition.face_locations(small_frame)
    face_ecnod = face_recognition.face_encodings(face_locas)[0]
    
    try:
        # comparing my image and  webcam
        results = face_recognition.compare_faces([my_face_encoding], face_ecnod)

        if results[0]:
            for (x,y,w,h) in face_detect:
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 1)
                cv2.putText(
                frame,
                'it is me!',
                (x,y+h + 30),
                cv2.FONT_HERSHEY_PLAIN,
                2,
                (0,255,0),
                2,
                cv2.LINE_AA
            )
        else:
            print("It's not a picture of me!")
            for (x,y,w,h) in face_detect:
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 1)
                cv2.putText(
                frame,
                'hindi',
                (x,y+h + 30),
                cv2.FONT_HERSHEY_PLAIN,
                2,
                (0,255,0),
                2,
                cv2.LINE_AA
            )
    except IndexError as e:
        print(e)


    cv2.imshow('frame',frame)
    

    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break
    
    
video.release()
cv2.destroyAllWindows()

im beginner and 4th year college student is there any recommendation such as library i use ? i mean there is no training data

I tried to look up Pytorch tensorFlow keras

packages but it kind of hard for me to understand , any recommendation or website/articles for begginer to understand the facial recognition

0 Answers
Related