带有opencv和dlib face_recognition库的人脸识别考勤系统给出不正确的识别
Posted
技术标签:
【中文标题】带有opencv和dlib face_recognition库的人脸识别考勤系统给出不正确的识别【英文标题】:face recognition attendance system with opencv and dlib face_recognition libraries giving incorrect recognitions 【发布时间】:2020-10-23 12:02:53 【问题描述】:所以我使用 opencv dlib 和 face_recognition 制作了一个人脸识别考勤系统,但由于某种原因,模型无法正确识别,例如当我使用网络摄像头在一帧中识别多个人时,它会不断更改边界框标签,这样一来,超过一个人的出勤率就会被标记,因为盒子的标签会不断变化。我尝试使用不止一张图片,比如每个人 30 张图片,但仍然是同样的问题,你能帮我理解为什么吗?
这是我的代码:
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()
网络摄像头识别.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'\nn,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)
【问题讨论】:
【参考方案1】:文献中有几种最先进的模型,dlib resnet 模型是其中之一,但不是唯一的。您可以实时处理您的网络摄像头流,并通过几行代码在 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)
它将在 my_db 文件夹中的网络摄像头上查找检测到的面孔。您应该将您的面部数据库存储在此文件夹中。此外,您可以使用 model_name 变量更改人脸识别模型。我添加了所有候选模型。 VGG-Face、FaceNet 和 Dlib 的表现优于其他。
【讨论】:
以上是关于带有opencv和dlib face_recognition库的人脸识别考勤系统给出不正确的识别的主要内容,如果未能解决你的问题,请参考以下文章
opencv基础opencv和dlib库中rectangle类型之间的转换
dlib库包的介绍与使用,opencv+dlib检测人脸框opencv+dlib进行人脸68关键点检测,opencv+dlib实现人脸识别,dlib进行人脸特征聚类dlib视频目标跟踪