tkinter写一个人脸识别系统(opencv)

一、UI界面

训练:
1.输入姓名,点击“信息采集”,等待采集100张图片后自动关闭,可以再次输入姓名,再次采集其他人的面部信息,图片保存在 facedata 目录下
2.点击模型训练,训练完成后下方状态栏会出现提示“{0} faces trained”

使用:
1.点击“识别”
2.关闭时点击“结束”

在这里插入图片描述

# ui.pyw
import tkinter as tk
import cv2,os,time
from PIL import ImageTk,Image
import getdata,train,recognise


class Display():
    def __init__(self,wide=800, height=500):
        self.W, self.H = wide, height
        self.button_w, self.button_h = 100, 30
        #button stretch
        self.button_hstretch=10
        self.button_wstretch = 10

        self.samlpe_path="facedata"
        self.cv_model='cv_model'
        self.recognition_model_path='recogn_model'

        self.show_state=False
        self.video_source=None

        self._init_root()

    def run(self):
        self.root.mainloop()

    def _init_root(self,):
        #creat window
        self.root=tk.Tk()
        self.root.title("Face Detector System")
        self.root.config(background="white")
        self.root.geometry(str(self.W) + "x" + str(self.H) + "-500-200")
        self.root.resizable(0, 0)
        #self.root.iconbitmap("./image/icon.ico")
        self.root.attributes("-alpha", 0.95)

        """left area"""
        # creat video area
        self.video_w, self.video_h= int(3 / 4 * self.W) - 1, int(self.H) - 2
        self.video = tk.Label(self.root, bg="black", fg="green",cursor="cross")
        self.video.place(x=1, y=1, w=self.video_w, h=self.video_h-35)

        #state area
        self.text = tk.Label(self.root, text="----------------")
        self.text.place(x=1, y=self.video_h-32, w=self.video_w, h=30)

        """right area"""
        right_x, right_y = 7 / 8 * self.W - 1 / 2 * self.button_w, 3 / 11 * self.H
        #entry
        tk.Label(self.root, text="name:",bg="white",justify="left")\
            .place(x=right_x, y= right_y-70, w=40, h=20)
        self.name_input=tk.Entry(self.root,bd=5, relief="groove")
        self.name_input.place(x=right_x, y= right_y-50, w=self.button_w, h=self.button_h)

        #button
        tk.Button(self.root, text="信息采集", relief="groove", command=self._sample_collect)\
            .place(x=right_x, y= right_y-20, w=self.button_w, h=self.button_h)
        tk.Button(self.root, text="模型训练", relief="groove", command=self._train)\
            .place(x=right_x, y= right_y+2*self.button_h+self.button_hstretch, w=self.button_w, h=self.button_h)
        tk.Button(self.root, text="识别", relief="groove", command=self._detect_start)\
            .place(x=right_x, y= right_y+4*(self.button_h+self.button_hstretch), w=1/2*self.button_w, h=self.button_h)
        tk.Button(self.root, text="结束", relief="groove", command=self._detect_end)\
            .place(x=right_x+1/2*self.button_w, y= right_y+4*(self.button_h+self.button_hstretch), w=1/2*self.button_w, h=self.button_h)

    def _sample_collect(self):
        self.text.config(text='Collect Object: '+"")
        self.text.update()

        face_id = len(self._load_labels())
        # 生成器
        self.video_source=getdata.getdata(face_id,self.video_w,self.cv_model)
        for img in self.video_source:
            self.video.config(image=img)
            self.video.update()

        # names save all name,and face_id is id of the name
        self._save_labels()

    def _train(self):
        self.text.config(text="training...")
        self.text.update()
        info=train.train(datapath = self.samlpe_path)
        self.text.config(text=info)
        self.text.update()

    def _detect_start(self):
        # 生成器
        self.video_source=recognise.recognise( names =self._load_labels(),video_w=self.video_w,cv_model=self.cv_model)
        for img,id,confidence in self.video_source:
            self.video.config(image=img)
            self.video.update()
            self.text.config(text='Object: '+id+" confidence:"+confidence)
            self.text.update()

    def _detect_end(self):
        if self.video_source is not None:
            self.video_source.close()

    #save name
    def _save_labels(self):
        new_label=self.name_input.get()
        import labels
        names=labels.names
        if new_label not in names and new_label!="":
            names.append(new_label)
        with open("labels.py","w",encoding="utf-8") as f:
            f.write("#coding=utf-8\n")
            f.write("names="+str(names))

    def _load_labels(self):
        import labels
        return labels.names


if __name__=="__main__":
    ds=Display()
    ds.run()

二、Labels

模型预测出列表names的index,根据index得到人名

# labels.py
#coding=utf-8
names=['unknow', ]

三、数据采集

使用CascadeClassifier级联分类器获取视频流中人脸位置,将人脸图像帧保存到本地供训练使用,默认采集100张。

CascadeClassifier使用opencv已训练好的模型,copy python\Lib\site-packages\cv2\data到当前目录,命名为cv_model
在这里插入图片描述

# getdata.py
import cv2,os
from PIL import Image,ImageTk

def getdata(face_id,video_w,cv_model="cv_model"):
    face_detector = cv2.CascadeClassifier(os.path.join(cv_model, 'haarcascade_frontalface_default.xml'))

    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    count = 0
    while True:
        sucess, img = cap.read()
        if sucess:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = face_detector.detectMultiScale(gray, 1.3, 5)

            if len(faces) != 0:
                x, y, w, h = faces[0]
                cv2.rectangle(img, (x, y), (x + w, y + w), (255, 0, 0))
                cv2.imwrite("facedata/User." + str(face_id) + '.' + str(count) + '.jpg', gray[y: y + h, x: x + w])
                count += 1

            h, w, _ = img.shape
        	wc ,hc= video_w,int(h * (video_w / w))
            current_image = Image.fromarray(img,"RGB").resize((wc,hc),Image.ANTIALIAS)
            img= ImageTk.PhotoImage(image=current_image)
            yield img

        cv2.waitKey(10)
        if count >= 100:
            break
    cap.release()
    cv2.destroyAllWindows()


if __name__=="__main__":
    images=getdata(face_id=0)
    cv2.namedWindow("show",0)
    # for i in range(100):
        # cv2.imshow("show",images.__next__())
    # for i in images:
    #     cv2.imshow("show",i)

四、识别模型训

检测模型使用opencv训练好的参数,见上一部分

用采集的数据训练自己的识别模型,训练完成后模型保存在recogn_model

使用LBPH算法:将图像划分区域,计算每个像素点的LBP特征,计算每个区域的特征直方图得特征向量,所有区域特征向量拼接,相似度函数对比。

# train.py
import numpy as np
from PIL import Image
import os
import cv2

def train(datapath = 'facedata'):

    #pip install opencv-contrib-python,if not install :AttributeError: module 'cv2.cv2' has no attribute 'face'
    recognizer = cv2.face.LBPHFaceRecognizer_create()

    #load data
    detector = cv2.CascadeClassifier("cv_model/haarcascade_frontalface_default.xml")
    faces, ids = getImagesAndLabels(datapath,detector)

    #train recogn model
    recognizer.train(faces, np.array(ids))

    #save model
    recognizer.write(r'recogn_model\trainer.yml')

    return "{0} faces trained.".format(len(np.unique(ids)))

def getImagesAndLabels(path,detector=None):
    imagePaths = [os.path.join(path, f) for f in os.listdir(path)]  # join函数的作用?
    faceSamples = []
    ids = []
    for imagePath in imagePaths:
        PIL_img = Image.open(imagePath).convert('L')   # convert it to grayscale
        img_numpy = np.array(PIL_img, 'uint8')
        id = int(os.path.split(imagePath)[-1].split(".")[1])
        faces = detector.detectMultiScale(img_numpy)

        for (x, y, w, h) in faces:
            faceSamples.append(img_numpy[y:y + h, x: x + w])
            ids.append(id)
    return faceSamples, ids

if __name__=="__main__":
    info=train()
    print(info)

五、使用

# recognise.py
import cv2,os
from PIL import ImageTk,Image

def recognise(names,video_w,cv_model="cv_model"):
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    recognizer.read('recogn_model/trainer.yml')

    faceCascade = cv2.CascadeClassifier(os.path.join(cv_model,"haarcascade_frontalface_default.xml"))

    cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    minW = 0.1 * cam.get(3)
    minH = 0.1 * cam.get(4)

    while True:
        ret, img = cam.read()
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(int(minW), int(minH))
        )

        ###
        idnum = "unknown"
        confidence="0%"
        if faces!=():
            x, y, w, h = faces[0]
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
            idnum, confidence = recognizer.predict(gray[y:y + h, x:x + w])
            print(idnum,confidence)
            if confidence < 100 and idnum < len(names):
                idnum = names[idnum]
                confidence = "{0}%".format( confidence)
            else:
                idnum = "unknown"
                confidence = "{0}%".format( confidence)

        h, w, _ = img.shape
        wc ,hc= video_w,int(h * (video_w / w))
        current_image = Image.fromarray(img, "RGB").resize((wc, hc), Image.ANTIALIAS)
        img = ImageTk.PhotoImage(image=current_image)

        yield img, str(idnum), str(confidence)
        
        cv2.waitKey(10)
    cam.release()
    cv2.destroyAllWindows()

if __name__=="__main__":
    recognise(    names = ['jim', 'Bob'])
  • 18
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
【项目介绍】 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到94.5分,放心下载使用! 该资源适合计算机相关专业(如人工智能、通信工程、自动化、软件工程等)的在校学生、老师或者企业员工下载,适合小白学习或者实际项目借鉴参考! 当然也可作为毕业设计、课程设计、课程作业、项目初期立项演示等。如果基础还行,可以在此代码基础之上做改动以实现更多功能。 人脸识别考勤系统 本项目是一个基于 Python人脸识别考勤系统,主要使用了 `dlib`、`opencv`、`tkinter`、`PIL`、`pymysql` 等库,并使用了 KNN 算法进行人脸识别。 环境安装 在运行本项目之前,请确保已经安装了以下环境和库: - Python - PyCharm - dlib(需要 Cmake、Visual Basic、C++ 依赖环境) - opencv 库 - tkinter 库 - pymysql - PIL - threading - time 功能介绍 本项目具有以下功能: - 打开系统按钮:打开摄像头捕获人脸。 - 注册按钮:将人脸、学号、姓名信息存储到数据库中。 - 查看缺勤名单:将没有识别签到的学生学号和姓名导出到打开的名单界面。 - 是否开始训练:点击“是”按钮进行人脸模型的训练。 - 帮助按钮:查看使用软件的注意事项。 注意事项 在使用本项目之前,请注意以下事项: 1. 请先打开系统。 2. 注册时请对准人脸,光线充足。 3. 训练时禁止进行其他操作。 文件说明 本项目包含以下文件: - `recognition.py`:主文件。 - `db.py`:数据库文件。 - `help_tip.py`:帮助窗口文件。 - `precamera.py`:预处理文件。 - `late_name.py`:缺勤名单文件。 - `trained_knn_model.clf`:KNN 算法模型文件。 - `knn_examples` 文件夹:包含了训练集和测试集,仅供学习交流探讨使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值