对https://blog.csdn.net/weixin_46291251/article/details/117996591这哥们代码的一些修改
import cv2
import numpy as np
import os
import shutil
import threading
import tkinter as tk
from PIL import Image, ImageTkchoice = 0# 首先读取config文件,第一行代表当前已经储存的人名个数,接下来每一行是(id,name)标签和对应的人名
id_dict = {} # 字典里存的是id——name键值对
Total_face_num = 999 # 已经被识别有用户名的人脸个数,camera = cv2.VideoCapture(0) # 摄像头
success, img = camera.read() # 从摄像头读取照片
W_size = 0.1 * camera.get(3)
H_size = 0.1 * camera.get(4)def init(): # 将config文件内的信息读入到字典中f = open('config.txt')global Total_face_numTotal_face_num = int(f.readline())for i in range(int(Total_face_num)):line = f.readline()id_name = line.split(' ')id_dict[int(id_name[0])] = id_name[1]f.close()init()# 加载OpenCV人脸检测分类器Haar
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")# 准备好识别方法LBPH方法
recognizer = cv2.face.LBPHFaceRecognizer_create()# 打开标号为0的摄像头
# camera = cv2.VideoCapture(0) # 摄像头
# success, img = camera.read() # 从摄像头读取照片
# W_size = 0.1 * camera.get(3)
# H_size = 0.1 * camera.get(4)system_state_lock = 0 # 标志系统状态的量 0表示无子线程在运行 1表示正在刷脸 2表示正在录入新面孔。
# 相当于mutex锁,用于线程同步'''
============================================================================================
以上是初始化
============================================================================================
'''def Get_new_face():global choiceprint("正在从摄像头录入新人脸信息 \n")# 存在目录data就清空,不存在就创建,确保最后存在空的data目录filepath = "data"if not os.path.exists(filepath):os.mkdir(filepath)else:shutil.rmtree(filepath)os.mkdir(filepath)sample_num = 0 # 已经获得的样本数while True: # 从摄像头读取图片choice = 2global successglobal img # 因为要显示在可视化的控件内,所以要用全局的success, img = camera.read()# 转为灰度图片if success is True:gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)else:break# 检测人脸,将每一帧摄像头记录的数据带入OpenCv中,让Classifier判断人脸# 其中gray为要检测的灰度图像,1.3为每次图像尺寸减小的比例,5为minNeighborsfaces = face_cascade.detectMultiScale(gray, 1.3, 5)# 框选人脸,for循环保证一个能检测的实时动态视频流for (x, y, w, h) in faces:# xy为左上角的坐标,w为宽,h为高,用rectangle为人脸标记画框cv2.rectangle(img, (x, y), (x + w, y + w), (255, 0, 0))# 样本数加1sample_num += 1# 保存图像,把灰度图片看成二维数组来检测人脸区域,这里是保存在data缓冲文件夹内T = Total_face_numcv2.imwrite("./data/User." + str(T) + '.' + str(sample_num) + '.jpg', gray[y:y + h, x:x + w])pictur_num = 1000 # 表示摄像头拍摄取样的数量,越多效果越好,但获取以及训练的越慢cv2.waitKey(1)if sample_num > pictur_num:breakelse: # 控制台内输出进度条l = int(sample_num / pictur_num * 50)r = int((pictur_num - sample_num) / pictur_num * 50)print("\r" + "%{:.1f}".format(sample_num / pictur_num * 100) + "=" * l + "->" + "_" * r, end="")var.set("%{:.1f}".format(sample_num / pictur_num * 100)) # 控件可视化进度信息# tk.Tk().update()window.update() # 刷新控件以实时显示进度def Train_new_face():print("\n正在训练")# cv2.destroyAllWindows()path = 'data'# 初始化识别的方法recog = recognizer# 调用函数并将数据喂给识别器训练faces, ids = get_images_and_labels(path)print('本次用于训练的识别码为:') # 调试信息print(ids) # 输出识别码# 训练模型 #将输入的所有图片转成四维数组recog.train(faces, np.array(ids))# 保存模型yml = str(Total_face_num) + ".yml"rec_f = open(yml, "w+")recog.save(yml)rec_f.close()# recog.save('aaa.yml')# 创建一个函数,用于从数据集文件夹中获取训练图片,并获取id
# 注意图片的命名格式为User.id.sampleNum
def get_images_and_labels(path):image_paths = [os.path.join(path, f) for f in os.listdir(path)]# 新建连个list用于存放face_samples = []ids = []# 遍历图片路径,导入图片和id添加到list中for image_path in image_paths:# 通过图片路径将其转换为灰度图片img = Image.open(image_path).convert('L')# 将图片转化为数组img_np = np.array(img, 'uint8')if os.path.split(image_path)[-1].split(".")[-1] != 'jpg':continue# 为了获取id,将图片和路径分裂并获取id = int(os.path.split(image_path)[-1].split(".")[1])# 调用熟悉的人脸分类器detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')faces = detector.detectMultiScale(img_np)# 将获取的图片和id添加到list中for (x, y, w, h) in faces:face_samples.append(img_np[y:y + h, x:x + w])ids.append(id)return face_samples, idsdef write_config():global user_nameprint("新人脸训练结束")f = open('config.txt', "a")T = Total_face_numf.write(str(T) + " "+ user_name + " \n")f.close()id_dict[T] = user_name# 这里修改文件的方式是先读入内存,然后修改内存中的数据,最后写回文件f = open('config.txt', 'r+')flist = f.readlines()flist[0] = str(int(flist[0]) + 1) + " \n"f.close()f = open('config.txt', 'w+')f.writelines(flist)f.close()'''
============================================================================================
以上是录入新人脸信息功能的实现
============================================================================================
'''def scan_face():# 使用之前训练好的模型for i in range(Total_face_num): # 每个识别器都要用i += 1yml = str(i) + ".yml"print("\n本次:" + yml) # 调试信息recognizer.read(yml)ave_poss = 0global choicefor times in range(10): # 每个识别器扫描十遍times += 1cur_poss = 0global successglobal imgglobal system_state_lockwhile system_state_lock == 2: # 如果正在录入新面孔就阻塞print("\r刷脸被录入面容阻塞", end="")passchoice = 1success, img = camera.read()gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 识别人脸faces = face_cascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(int(W_size), int(H_size)))# 进行校验for (x, y, w, h) in faces:# global system_state_lockwhile system_state_lock == 2: # 如果正在录入新面孔就阻塞print("\r刷脸被录入面容阻塞", end="")pass# 这里调用Cv2中的rectangle函数 在人脸周围画一个矩形cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)# 调用分类器的预测函数,接收返回值标签和置信度idnum, confidence = recognizer.predict(gray[y:y + h, x:x + w])conf = confidence# 加载一个字体用于输出识别对象的信息font = cv2.FONT_HERSHEY_SIMPLEX# 输出检验结果以及用户名# 展示结果# cv2.imshow('camera', img)print("conf=" + str(conf), end="\t")if 65 > conf > 0:cur_poss = 1 # 表示可以识别else:cur_poss = 0 # 表示不可以识别k = cv2.waitKey(1)if k == 27:# cam.release() # 释放资源cv2.destroyAllWindows()breakave_poss += cur_possif ave_poss >= 5: # 有一半以上识别说明可行则返回return ireturn 0 # 全部过一遍还没识别出说明无法识别'''
============================================================================================
以上是关于刷脸功能的设计
============================================================================================
'''def f_scan_face_thread():global choice# 使用之前训练好的模型# recognizer.read('aaa.yml')var.set('刷脸')ans = scan_face()if ans == 0:print("最终结果:无法识别")var.set("最终结果:无法识别")else:ans_name = "最终结果:" + str(ans) + id_dict[ans]print(ans_name)var.set(ans_name)global system_state_lockprint("锁被释放0")system_state_lock = 0 # 修改system_state_lock,释放资源choice = 0def f_scan_face():print(choice)global system_state_lockprint("\n当前锁的值为:" + str(system_state_lock))if system_state_lock == 1:print("阻塞,因为正在刷脸")return 0elif system_state_lock == 2: # 如果正在录入新面孔就阻塞print("\n刷脸被录入面容阻塞\n""")return 0system_state_lock = 1p = threading.Thread(target=f_scan_face_thread)p.setDaemon(True) # 把线程P设置为守护线程 若主线程退出 P也跟着退出p.start()def f_rec_face_thread():global choicevar.set('录入')cv2.destroyAllWindows()global Total_face_numTotal_face_num += 1Get_new_face() # 采集新人脸print("采集完毕,开始训练")global system_state_lock # 采集完就可以解开锁print("锁被释放0")system_state_lock = 0choice = 0Train_new_face() # 训练采集到的新人脸write_config() # 修改配置文件# recognizer.read('aaa.yml') # 读取新识别器# global system_state_lock
# print("锁被释放0")
# system_state_lock = 0 # 修改system_state_lock,释放资源def f_rec_face():global user_nameglobal choiceglobal system_state_lockprint("当前锁的值为:" + str(system_state_lock))user_name = var_user_name.get()if system_state_lock == 2:print("阻塞,因为正在录入面容")return 0else:system_state_lock = 2 # 修改system_state_lockprint("改为2", end="")print("当前锁的值为:" + str(system_state_lock))p = threading.Thread(target=f_rec_face_thread)p.setDaemon(True) # 把线程P设置为守护线程 若主线程退出 P也跟着退出p.start()# tk.Tk().update()# system_state_lock = 0 # 修改system_state_lock,释放资源def f_exit(): # 退出按钮exit()'''
============================================================================================
以上是关于多线程的设计
============================================================================================
'''window = tk.Tk()
window.title('wjh lxq\' Face_rec 3.0') # 窗口标题
window.geometry('1000x500') # 这里的乘是小x# 在图形界面上设定标签,类似于一个提示窗口的作用
var = tk.StringVar()
l = tk.Label(window, textvariable=var, bg='green', fg='white', font=('Arial', 12), width=50, height=4)
# 说明: bg为背景,fg为字体颜色,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
l.pack() # 放置l控件
var.set("wjh lxq 人脸识别系统")# 在窗口界面设置放置Button按键并绑定处理函数
button_a = tk.Button(window, text='开始刷脸', font=('Arial', 12), width=10, height=2, command=f_scan_face)
button_a.place(x=800, y=120)button_b = tk.Button(window, text='录入人脸', font=('Arial', 12), width=10, height=2, command=f_rec_face)
button_b.place(x=800, y=220)button_c = tk.Button(window, text='退出', font=('Arial', 12), width=10, height=2, command=f_exit)
button_c.place(x=800, y=320)panel = tk.Label(window, width=500, height=350) # 摄像头模块大小
panel.place(x=10, y=100) # 摄像头模块的位置
window.config(cursor="arrow")tk.Label(window, text='name: ').place(x=600, y=235)
var_user_name = tk.StringVar()
entry_user_name = tk.Entry(window, textvariable=var_user_name)
entry_user_name.place(x=650, y=235)def video_loop(): # 用于在label内动态展示摄像头内容(摄像头嵌入控件)global successglobal imgglobal choiceif not choice:success, img = camera.read()if success:cv2.waitKey(1)cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA) # 转换颜色从BGR到RGBAcurrent_image = Image.fromarray(cv2image) # 将图像转换成Image对象imgtk = ImageTk.PhotoImage(image=current_image)panel.imgtk = imgtkpanel.config(image=imgtk)window.after(1, video_loop)video_loop()# 窗口循环,用于显示
window.mainloop()'''
============================================================================================
以上是关于界面的设计
============================================================================================
'''
自备config.txt和haarcascade_frontalface_default.xml文件
config.txt 第一行写一个0即可
录脸的时候输入name.