CoolFace
Datasetpublic

crystal-technologies/CircumSpect

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes232downloads
faces.py85 linesDownload Raw Back to root
1import face_recognition2from PIL import Image3import numpy as np4import pickle5import cv26import os7 8with open('pwd.txt', 'r') as pwd:9    folder_location = pwd.read()10 11def find_encodings(images_):12    encode_list = []13    for imgs in images_:14        imgs = np.array(Image.open('./img/face_recognition/'+imgs))15        imgs = cv2.cvtColor(imgs, cv2.COLOR_BGR2RGB)16        encode = face_recognition.face_encodings(imgs)[0]17        encode_list.append(encode)18    return encode_list19 20def recognize_users(cap):21    path = f'{folder_location}img/face_recognition'22    recognized_users = []  # List to store names of recognized users23    images = []24    classNames = []25    myList = os.listdir(path)26 27    for cl in myList:28        curImg = cv2.imread(f'{path}/{cl}')29        images.append(curImg)30        classNames.append(os.path.splitext(cl)[0])31    try:32        with open(f'{folder_location}models/face_rec', 'rb') as file:33            encodeListKnown = pickle.load(file)34    except:35        path = f'{folder_location}img/face_recognition'36        images = []37        classNames = []38        myList = os.listdir(path)39        images = myList40 41        encodeListKnown = find_encodings(images)42        print(len(encodeListKnown))43        print('Encoding Complete')44 45        with open(f'{folder_location}models/face_rec', 'wb') as file:46            pickle.dump(encodeListKnown, file)47            file.close()48 49    _, img = cap.read()50    img = cv2.flip(img, 2)51    imgS = cv2.resize(img, (0,0), None, 0.25, 0.25)52    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)53    54    facesCurFrame = face_recognition.face_locations(imgS)55    encodeCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)56 57    for encodeFace, faceLoc in zip(encodeCurFrame, facesCurFrame):58        matches = face_recognition.compare_faces(encodeListKnown, encodeFace)59        faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)60        print(faceDis)61        matchIndices = np.where(matches)[0]  # Get indices of all matched faces62        63        for matchIndex in matchIndices:64            name = classNames[matchIndex].upper()65            recognized_users.append(name)  # Append recognized user to the list66            print(name)67            y1, x2, y2, x1 = faceLoc68            y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*469            cv2.rectangle(img, (x1, y1), (x2, y2), (205, 154, 79), 2)70            cv2.putText(img, name, (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 0.7, (255, 255, 255), 2)71        72        if not matchIndices.all():73            name = "UNKNOWN"74            print(name)75            y1, x2, y2, x1 = faceLoc76            y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*477            cv2.rectangle(img, (x1, y1), (x2, y2), (205, 154, 79))78            cv2.putText(img, 'UNKNOWN', (x1+6, y2-6), cv2.FONT_HERSHEY_COMPLEX, 0.7, (255, 255, 255), 2)79 80    # cv2.imshow("Face Recognition", img)81    # cv2.waitKey(1)82 83    return recognized_users, img84 85