简体   繁体   English

带有 opencv 和 dlib face_recognition 库的人脸识别考勤系统给出不正确的识别

[英]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.所以我使用 opencv dlib 和 face_recognition 制作了一个人脸识别考勤系统,但由于某种原因 model 无法正确识别,例如当我使用网络摄像头在一帧中识别多个人时,它会不断更改边界框标签,以此类推超过一个人的出席会被标记,因为盒子的标签会不断变化。 i tried using more than one image, like 30 images for each person but still the same problem, can you help me understand why?我尝试使用不止一张图片,比如每个人 30 张图片,但仍然是同样的问题,你能帮我理解为什么吗?

here is my code:这是我的代码:

encodings.py编码.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网络摄像头识别.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.文献中有几种最先进的模型,dlib resnet model 是其中之一,但不是唯一的。 You can process your web cam stream in real time and apply face recognition within deepface with a few lines of code.您可以实时处理您的 web 凸轮 stream 并通过几行代码在 deepface 中应用人脸识别。

#!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.它将在 my_db 文件夹中的 web cam 上查找检测到的面孔。 You should store your face database in this folder.您应该将您的面部数据库存储在此文件夹中。 Besides, you could change the face recognition model with model_name variable.此外,您可以使用 model_name 变量更改人脸识别 model。 I added the all candidate models.我添加了所有候选模型。 VGG-Face, FaceNet and Dlib overperform than others. VGG-Face、FaceNet 和 Dlib 的表现优于其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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