南宁网站开发企业,wordpress 数学主题,苏州网站建设网络推广,海兴县做网站简单的人脸识别签到程序
在看完以下代码后#xff0c;略微修改一番#xff0c;你就能够组装出自己的“简单的人脸识别签到程序”了。 请注意库的安装#xff0c;否则会不可用。 你可以通过在cmd中使用#xff1a;pip install来安装。 以下代码运行python 3.8
UI界面
使…简单的人脸识别签到程序
在看完以下代码后略微修改一番你就能够组装出自己的“简单的人脸识别签到程序”了。 请注意库的安装否则会不可用。 你可以通过在cmd中使用pip install来安装。 以下代码运行python 3.8
UI界面
使用前安装PySimpleGUI库
import PySimpleGUI as sg# 创建一个简单的窗口包含一个标签和一个输入框
Layout [[sg.Text(编号: , size(10, 1)), sg.InputText(keyid)],[sg.Text(姓名: , size(10, 1)), sg.InputText(keyname)],[sg.Text(keymsg)],[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 %saffected_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 %saffected_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 %scur.execute(sql, (user_num,))result cur.fetchone()cur.close()con.close()return result[0] if result else None人脸识别
使用前安装配置好cv2opencv-pythonface_recognitionnumpy
import cv2
import face_recognition
import os
import numpy as np
#开启摄像头
c cv2.VideoCapture(0, cv2.CAP_DSHOW)
#文件路径
file_path D:\work\python\open\lianwhile True:ret,frame c.read()if ret:#显示图cv2.imshow(a,frame)if cv2.waitKey(20) 27:breakif 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:continueelse: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()好了现在你可以组装自己的简单的签到程序了。