基于python的人脸识别签到系 统
程序界面
第一次使用时需要在在“签到名单”文件夹中新建一个名为签到次数、内容为字符’0’的txt文件
import cv2
import face_recognition
from tkinter import *
known_face_names = []
known_face_names2 = []
known_face_encodings = []
known_face_encodings2 = []
name_list = []
ls = []
录入模块
def admission():
face_id = entry1.get()
print('\n 数据初始化中,请直视摄像机录入数据....')
vc = cv2.VideoCapture(0)
while True:
ret, img = vc.read()
faces = face_recognition.face_locations(img)
if cv2.waitKey(1) == 32:
for i in range(1,2):
known_face_names.append(face_id)
print(known_face_names)
cv2.imwrite('Face/User/' + str(face_id) + '.jpg',img)
cv2.imshow('image', img)
fo = open("face_names/face_id.csv", "a")
fo.write(",".join(known_face_names) + "\n")
fo.close()
known_face_names.clear()
print('录入完成,退出')
vc.release()
cv2.destroyWindow('vdo')
break
else:
for (top, right, bottom, left) in faces:
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.imshow('vdo', img)
识别打卡模块
def checkFace():
vc = cv2.VideoCapture(0)
i = 0
dao = 0
fo = open("face_names/face_id.csv", "r")
ls = []
for line in fo:
line = line.replace("\n","")
ls.append(line.split(","))
for face_id in ls:
for id in face_id:
known_face = cv2.imread('Face/User/' + str(id) + '.jpg')
face_encoding = face_recognition.face_encodings(known_face)
known_face_encodings2.append(face_encoding)
known_face_names2.append(str(id))
fo.close()
while True:
ret, img = vc.read()
if not ret:
print('没有捕获到人脸')
break
locations = face_recognition.face_locations(img)
face_encodings = face_recognition.face_encodings(img, locations)
for (top, right, bottom, left), face_encoding in zip(locations, face_encodings):
matchs = face_recognition.compare_faces(known_face_encodings2, face_encoding)
name = 'unknown'
for machs, known_name in zip(matchs, known_face_names2):
if any(machs):
name = known_name
break
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(img, name, (left, top - 20), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
fq = open("签到名单/签到次数.txt", "r")
a = fq.read()
if a == str(1):
fc = open("签到名单/签到名单.csv", "r")
for line in fc:
line = line.replace("\n", "")
ls = line.split(",")
for name_1 in ls:
if name_1 == name:
print("你已经签到过")
else:
fb = open("签到名单/签到名单.csv", "a+")
name_list.append(name)
fb.write(",".join(name_list) + "\n")
fb.close()
print("签到成功")
name_list.clear()
ls.clear()
if a == str(0):
fo = open("签到名单/签到名单.csv", "a+")
name_list.append(name)
fo.write(",".join(name_list) + "\n")
a = str(1)
fw = open("签到名单/签到次数.txt", "w")
fw.write(str(a))
print("签到成功")
name_list.clear()
fw.close()
fq.close()
fo.close()
cv2.imshow('vidio', img)
if cv2.waitKey(1) == 27:
vc.release()
cv2.destroyAllWindows()
break
程序界面
win = Tk()
win.title('人脸识别签到系统')
win.geometry('300x200')
w = L
abel(win,text="当相机窗口打开后,按空格键录入人脸\n签到完成后按Esc退出相机窗口\n(头像的名字就是签到者,出现即签到成功)\n签到成功后在程序目录\签到名单文件下查看签到名单\n\n系统暂不支持输入中文,更多功能敬请期待")
w.pack()
entry1 = Entry(win, width=50)
entry1.pack()
button = Button(win, text="输入名字,点我录入", command=admission)
button2 = Button(win, text="签到", command=checkFace)
button.pack()
button2.pack()
win.mainloop()