简单的人脸识别签到程序
在看完以下代码后,略微修改一番,你就能够组装出自己的“简单的人脸识别签到程序”了。
请注意库的安装,否则会不可用。
你可以通过在cmd中使用:pip install
来安装。
以下代码运行python = 3.8
UI界面
使用前安装:PySimpleGUI库
import PySimpleGUI as sg
# 创建一个简单的窗口,包含一个标签和一个输入框
Layout = [[sg.Text("编号: ", size=(10, 1)), sg.InputText(key='id')],
[sg.Text("姓名: ", size=(10, 1)), sg.InputText(key='name')],
[sg.Text(key='msg')],
[sg.Button("人脸采集"), sg.Button("关闭"),sg.Button('签到')]]
window = sg.Window("人脸识别", Layout,icon="./icon.ico")
while True:
event, values = window.read()# 读取窗口事件和输入值
print(event, values)
if event == "人脸采集":
id = values["id"]
name = values["name"]
window["msg"].update(f"编号: {id}, 姓名: {name}")# 更新消息框内容
sg.popup("人脸采集")
elif event == "签到":
id = values["id"]
name = values["name"]
window["msg"].update(f"编号: {id}, 姓名: {name}")
if id == "1" and name == "1":
sg.popup("签到成功")
print("签到成功")
else:
sg.popup("签到失败")
print("签到失败")
elif event == "关闭" or event == sg.WIN_CLOSED:
print("退出")
sg.popup_timed("退出")# 弹出提示信息
break
数据库
使用前请安装好MySQL和图形化界面。
CREATE TABLE tu (
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(255) NOT NULL,
user_num INT NOT NULL
);
数据库操作
使用前安装:pymysql库
import pymysql
# 数据库配置
db_config = {
'host': 'localhost',
'user': 'root',
'password': '123456',
'port': 3306,
'database': "name",
'charset': 'utf8'
}
# 增
def connect_mysql(name, num):
con = pymysql.connect(**db_config)
cur = con.cursor()
sql = "insert into tu (user_name,user_num) values(%s,%s)"
cur.execute(sql, (name, num))
con.commit()
if cur.rowcount > 0:
print("数据库添加成功")
else:
print("数据库添加失败")
cur.close()
con.close()
# 改
def update_mysql(name, num):
con = pymysql.connect(**db_config)
cur = con.cursor()
sql = "UPDATE tu SET user_num = %s WHERE user_name = %s"
affected_rows = cur.execute(sql, (num, name))
con.commit()
if affected_rows > 0:
print("更新成功")
else:
print("更新失败或未找到对应的记录")
cur.close()
con.close()
# 删
def delete_mysql(name):
con = pymysql.connect(**db_config)
cur = con.cursor()
sql = "DELETE FROM tu WHERE user_name = %s"
affected_rows = cur.execute(sql, (name,))
con.commit()
if affected_rows > 0:
print("删除成功")
else:
print("删除失败或未找到对应的记录")
cur.close()
con.close()
# 查
def num_to_name(user_num):
con = pymysql.connect(**db_config)
cur = con.cursor()
sql = "SELECT user_name FROM tu WHERE user_num = %s"
cur.execute(sql, (user_num,))
result = cur.fetchone()
cur.close()
con.close()
return result[0] if result else None
人脸识别
使用前安装配置好cv2(opencv-python),face_recognition,numpy
import cv2
import face_recognition
import os
import numpy as np
#开启摄像头
c = cv2.VideoCapture(0, cv2.CAP_DSHOW)
#文件路径
file_path = "D:\work\python\open\lian"
while True:
ret,frame = c.read()
if ret:
#显示图
cv2.imshow("a",frame)
if cv2.waitKey(20) == 27:
break
if cv2.waitKey(20) == 113:
# 检测人脸
face_list = face_recognition.face_locations(frame)
if len(face_list) > 0:
print("视频检测到人脸")
# 遍历目录
path = os.listdir(file_path)
# print(path)
for i in path:
#获取人脸特征
img = cv2.imread(f"{file_path}\\{i}")
encodings = face_recognition.face_encodings(img)
if len(encodings) == 0:
continue
else:
en1 = face_recognition.face_encodings(img)[0]
en2 = face_recognition.face_encodings(frame)[0]
iss = np.linalg.norm(en1-en2)
# print(iss)
if iss < 0.5:
print("与图像",i,"中欧几里得距离:",round(iss,2),"是同一个人")
else:
print("与图像",i,"欧几里得距离为 :",round(iss,2),"不是同一个人")
cv2.destroyAllWindows()
好了,现在你可以组装自己的简单的签到程序了。