先介绍一下 需要安装的东西
先win+R打开在这里插入图片描述

直接输入就行 ,需要安装的有 pip install baidu-aip pip install opencv-python

利用百度的人脸识别来进行操作。
如果之前没有用过可以先去https://login.bce.baidu.com 创建一个账号
进入之后
在这里插入图片描述
点击这个人脸识别

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
图片中的三个东西是在后面调用的时候需要用到的

在这里插入图片描述

在这里插入图片描述

创建一个文件夹
在这里插入图片描述
register.py

from aip import AipFace
import base64

APP_ID = '前面的APP_ID'
API_KEY = '前面的API_KEY'
SECRET_KEY = '前面的SECRET_KEY'

groupIdList = "Matrix_studio"
print("初始化完成")
imageType = "BASE64"
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

filePath = (r"capture.jpg")

def register():
    f = open(filePath, "rb")
    data = base64.b64encode(f.read())
    f.close()
    image = str(data, 'UTF-8')

    #image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串"
    imageType = "BASE64"
    groupId = "Matrix_studio"  #你创建的人脸库里面的组的名称
    userId = "test"   #组里面每一位成员的名称
    """ 调用人脸注册 """
    client.addUser(image, imageType, groupId, userId)
    """ 如果有可选参数 """
    options = {}
    options["user_info"] = "user's info"
    options["quality_control"] = "NORMAL"
    options["liveness_control"] = "LOW"
    options["action_type"] = "REPLACE"
    """ 带参数调用人脸注册 """
    answer = client.addUser(image, imageType, groupId, userId, options)
    if answer['error_msg'] == 'SUCCESS' :
        print("注册成功")
    

check.py

在这里插入代码片from aip import AipFace
import base64

APP_ID = '前面的APP_ID'
API_KEY ='前面的API_KEY'
SECRET_KEY = '前面的SECRET_KEY'

groupIdList = "Matrix_studio"  #创建的组的名称
print("初始化完成")
imageType = "BASE64"
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

filePath = (r"capture.jpg")

def run():
    f = open(filePath, "rb")
    data = base64.b64encode(f.read())
    f.close()
    image = str(data, 'UTF-8')
    result = client.search(image, imageType, groupIdList)
    print(result["result"]["user_list"][0]["group_id"])  # 用户组名称
    print(result["result"]["user_list"][0]["user_id"])  # 用户ID
    print(result["result"]["user_list"][0]["score"])  # 相似度

    if result["error_msg"] == "SUCCESS":
        score = result["result"]["user_list"][0]["score"]
        user_id = result["result"]["user_list"][0]["user_id"]
        if score > 85:
            result_ = 'yes'
            print(user_id, ":识别成功")
            return result_
        else:
            result_ = 'no'
            print("人脸库无此人")
            return result_
    else:
        print("error:", result["error_msg"])
    


ues.py

from tkinter import *
import cv2
from PIL import Image, ImageTk
import threading
import time
import check


root = Tk()  # Tk  一大写一小写
root.title('Matrix_工作室 考勤系统')  # 名字
root.geometry('600x600')



l1 = Label(root, text='Matrix 工作室', bg='yellow', font=(
    'Arial', 12), width=15, height=2)  # 显示工作室的名字
l1.pack()


# 显示考勤的结果
var = StringVar()
l2 = Label(root,textvariable=var,bg='yellow',font=('Arial',12),width = 10,height = 2)
l2.pack()


def video_loop():
    success, img = camera.read()  # 从摄像头读取照片
    cv2.imwrite("capture.jpg", img)  # 写入图片
    if success:
        cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)  # 转换颜色从BGR到RGBA
        current_image = Image.fromarray(cv2image)  # 将图像转换成Image对象
        imgtk = ImageTk.PhotoImage(image=current_image)
        panel.imgtk = imgtk
        panel.config(image=imgtk)
        root.after(1, video_loop)




camera = cv2.VideoCapture(0)  # 摄像头  如果为0则使用内置摄像头,比如笔记本的摄像头,用1或其他的就是切换摄像头。!!!!!
success, img = camera.read()  # 从摄像头读取照片
cv2.imwrite("capture.jpg", img)  # 写入图片


panel = Label(root)  # initialize image panel
panel.pack(padx=10, pady=10)
root.config(cursor="arrow")


def check_():
    global var
    while True:
        try:
            time.sleep(1)
            res = check.run()
            if res[0] == 'yes' :
                var.set('考勤成功')
            elif res[0] == 'no' :
                var.set('考勤失败')
        except TypeError:
            var.set('考勤失败')
            continue





def check_main():
    thread_check = threading.Thread(target=check_)
    thread_check.start()


check_main()

video_loop()


root.mainloop()
# 当一切都完成后,关闭摄像头并释放所占资源
camera.release()
cv2.destroyAllWindows()



硬件参考https://blog.csdn.net/milk_paramecium/article/details/112321449

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐