python+OpenCV人脸识别考勤系统实现的详细代码
作者:A等天晴 时间:2021-11-01 01:28:44
一、环境配置
安装 Python
请确保您已经安装了 Python 3.x。可以在Python 官网下载并安装。
安装所需库
在命令提示符或终端中运行以下命令来安装所需的库:
pip install opencv-python
pip install opencv-contrib-python
pip install numpy
pip install face-recognition
二、创建数据集
创建文件夹结构
在项目目录下创建如下文件夹结构:
attendance-system/
├── dataset/
│ ├── person1/
│ ├── person2/
│ └── ...
└── src/
将每个人的照片放入对应的文件夹中,例如:
attendance-system/
├── dataset/
│ ├── person1/
│ │ ├── 01.jpg
│ │ ├── 02.jpg
│ │ └── ...
│ ├── person2/
│ │ ├── 01.jpg
│ │ ├── 02.jpg
│ │ └── ...
│ └── ...
└── src/
三、实现人脸识别算法
在 src
文件夹下创建一个名为 face_recognition.py
的文件,并添加以下代码:
import os
import cv2
import face_recognition
import numpy as np
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
return images
def create_known_face_encodings(root_folder):
known_face_encodings = []
known_face_names = []
for person_name in os.listdir(root_folder):
person_folder = os.path.join(root_folder, person_name)
images = load_images_from_folder(person_folder)
for image in images:
face_encoding = face_recognition.face_encodings(image)[0]
known_face_encodings.append(face_encoding)
known_face_names.append(person_name)
return known_face_encodings, known_face_names
def recognize_faces_in_video(known_face_encodings, known_face_names):
video_capture = cv2.VideoCapture(0)
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.8, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
dataset_folder = "../dataset/"
known_face_encodings, known_face_names = create_known_face_encodings(dataset_folder)
recognize_faces_in_video(known_face_encodings, known_face_names)
四、实现考勤系统
在 src
文件夹下创建一个名为 attendance.py
的文件,并添加以下代码:
import os
import datetime
import csv
from face_recognition import create_known_face_encodings, recognize_faces_in_video
def save_attendance(name):
attendance_file = "../attendance/attendance.csv"
now = datetime.datetime.now()
date_string = now.strftime("%Y-%m-%d")
time_string = now.strftime("%H:%M:%S")
if not os.path.exists(attendance_file):
with open(attendance_file, "w", newline="") as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(["Name", "Date", "Time"])
with open(attendance_file, "r+", newline="") as csvfile:
csv_reader = csv.reader(csvfile)
rows = [row for row in csv_reader]
for row in rows:
if row[0] == name and row[1] == date_string:
return
csv_writer = csv.writer(csvfile)
csv_writer.writerow([name, date_string, time_string])
def custom_recognize_faces_in_video(known_face_encodings, known_face_names):
video_capture = cv2.VideoCapture(0)
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
save_attendance(name)
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.8, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
dataset_folder = "../dataset/"
known_face_encodings, known_face_names = create_known_face_encodings(dataset_folder)
custom_recognize_faces_in_video(known_face_encodings, known_face_names)
五、运行考勤系统
运行 attendance.py
文件,系统将开始识别并记录考勤信息。考勤记录将保存在 attendance.csv
文件中。
python src/attendance.py
现在,您的基于人脸识别的考勤系统已经实现。请注意,这是一个基本示例,您可能需要根据实际需求对其进行优化和扩展。例如,您可以考虑添加更多的人脸识别算法、考勤规则等。
来源:https://juejin.cn/post/7235458133505867837
标签:python,OpenCV,人脸识别,考勤系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php5.2 Json不能正确处理中文、GB编码的解决方法
2023-10-26 13:49:28
正则表达式字面量在ECMAScript5中的变化
2012-04-26 16:23:16
Oracle存储过程基本语法介绍
2023-06-27 14:00:26
Oracle数据库处理多媒体信息
2010-07-16 13:01:00
php实现的三个常用加密解密功能函数示例
2023-07-20 06:25:52
![](https://img.aspxhome.com/file/2023/4/55424_0s.jpg)
python实现五子棋程序
2022-05-26 23:24:31
![](https://img.aspxhome.com/file/2023/3/65973_0s.gif)
列表模块是否需要标题
2009-06-25 14:11:00
![](https://img.aspxhome.com/file/UploadPic/20096/25/g2009620101624-60s.jpg)
oracle 的表空间实例详解
2023-06-25 11:39:37
QQ影音感念亲恩皮肤,不只是大按钮这么简单
2009-01-04 14:16:00
![](https://img.aspxhome.com/file/UploadPic/20091/4/0812_qqplayer_bigbutton_01-96s.jpg)
使用php将某个目录下面的所有文件罗列出来的方法详解
2023-09-29 10:47:05
如何在Access数据库中立即得到所插入记录的自动编号?
2010-06-17 12:45:00
Ubuntu查看修改mysql的登录名和密码、安装phpmyadmin
2023-11-21 23:36:59
dl,dt,dd标签 VS 传统table实现数据列表
2009-08-02 20:45:00
![](https://img.aspxhome.com/file/UploadPic/20098/2/dl-vs-table-82s.png)
PHP合并两个或多个数组的方法
2023-06-11 12:06:28
![](https://img.aspxhome.com/file/2023/6/55286_0s.jpg)
Warning: require(): open_basedir restriction in effect,目录配置open_basedir报错问题分析
2023-06-02 23:28:18
![](https://img.aspxhome.com/file/2023/2/55362_0s.png)
详细介绍ASP内置对象Response
2008-06-23 12:42:00
分享一些可视信息设计资源
2009-10-06 15:19:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/ui-info-share-banner-41s.jpg)
让SQL Server数据库自动执行管理任务(一)
2009-03-20 10:35:00
Python调用graphviz绘制结构化图形网络示例
2021-09-15 19:06:49
![](https://img.aspxhome.com/file/2023/3/69063_0s.jpg)
Adobe发布Flash Player 10正式版
2008-10-15 17:15:00