简体   繁体   中英

face recognition attendance system with opencv and dlib face_recognition libraries giving incorrect recognitions

so I have made a face recognition attendance system using opencv dlib and face_recognition but for some reason model is not making correct recognitions, like when I use the webcam to identify multiple people in one frame, it keeps changing the bounding boxes labels, and that way attendance for more than one people gets marked, because the labels of boxes keep changing. i tried using more than one image, like 30 images for each person but still the same problem, can you help me understand why?

here is my code:

encodings.py

import cv2
import face_recognition
import numpy as np
import os
from datetime import datetime
from imutils import paths
import pickle

path='./imageAttendance'
imagePaths= list(paths.list_images(path))

knownEncodings=[]
knownNames=[]

for (i, imagePath) in enumerate(imagePaths):
    print("[INFO] processing image {}/{}".format(i + 1,
                                                 len(imagePaths)))
    name= imagePath.split(os.path.sep)[-2]
    image= cv2.imread(imagePath)
    rgb= cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    boxes= face_recognition.face_locations(rgb, model='cnn')
    encodings= face_recognition.face_encodings(rgb, boxes)

    for encoding in encodings:
        knownEncodings.append(encoding)
        knownNames.append(name)

print("[INFO] serializing encodings...")
data={'encodings': knownEncodings, 'names': knownNames}
f= open('encodings.pickle', 'wb')
f.write(pickle.dumps(data))
f.close()

webcam-recognition.py

import face_recognition
import argparse
import pickle
import cv2
from datetime import datetime

print("[INFO] loading encodings...")
data= pickle.loads(open('encodings.pickle', 'rb').read())

def mark_attendance(n):
    with open('attendance.csv', 'r+') as f:
        myDataList= f.readlines()
        print(myDataList)
        nameList=[]
        for line in myDataList:
            name= line.split(',')[0]
            nameList.append(name)

        if n not in nameList:
            now= datetime.now()
            dtString= now.strftime('%H:%M:%S')
            f.writelines(f'\n{n},{dtString}')

cap= cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    # img = captureScreen()
    image = cv2.resize(img, (0, 0), None, 0.25, 0.25)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    print("[INFO] recognizing faces...")
    boxes = face_recognition.face_locations(rgb,
                                            model='cnn')
    encodings = face_recognition.face_encodings(rgb, boxes)
    names = []

    for encoding in encodings:
        matches = face_recognition.compare_faces(data['encodings'], encoding)
        name = 'Unknown'

        if True in matches:
            matchedIdxs = [i for (i, b) in enumerate(matches) if b]
            counts = {}

            for i in matchedIdxs:
                name = data['names'][i]
                counts[name] = counts.get(name, 0) + 1

            name = max(counts, key=counts.get)

        names.append(name)
    for ((top, right, bottom, left), name) in zip(boxes, names):
        cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
        y = top - 15 if top - 15 > 15 else top + 15
        cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
                    0.75, (0, 255, 0), 2)
        mark_attendance(name)

    cv2.imshow('Webcam', image)
    cv2.waitKey(1)

There are several state-of-the-art models in the literature and dlib resnet model is one of them but it is not the unique one. You can process your web cam stream in real time and apply face recognition within deepface with a few lines of code.

#!pip install deepface
from deepface import DeepFace

models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib']
DeepFace.stream(db_path = 'C:/my_db', model_name = models[0], enable_face_analysis = False)

It will look for detected faces on your web cam in my_db folder. You should store your face database in this folder. Besides, you could change the face recognition model with model_name variable. I added the all candidate models. VGG-Face, FaceNet and Dlib overperform than others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM