|
|
@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
# 实验环境:python 3.6 + opencv-python 3.4.14.51
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 首先读取config文件,第一行代表当前已经储存的人名个数,接下来每一行是(id,name)标签和对应的人名
|
|
|
|
|
|
|
|
id_dict = {} # 字典里存的是id——name键值对
|
|
|
|
|
|
|
|
Total_face_num = 999 # 已经被识别有用户名的人脸个数,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init(): # 将config文件内的信息读入到字典中
|
|
|
|
|
|
|
|
f = open('config.txt')
|
|
|
|
|
|
|
|
global Total_face_num
|
|
|
|
|
|
|
|
Total_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():
|
|
|
|
|
|
|
|
print("正在从摄像头录入新人脸信息 \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: # 从摄像头读取图片
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global success
|
|
|
|
|
|
|
|
global img # 因为要显示在可视化的控件内,所以要用全局的
|
|
|
|
|
|
|
|
success, img = camera.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 转为灰度图片
|
|
|
|
|
|
|
|
if success is True:
|
|
|
|
|
|
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 检测人脸,将每一帧摄像头记录的数据带入OpenCv中,让Classifier判断人脸
|
|
|
|
|
|
|
|
# 其中gray为要检测的灰度图像,1.3为每次图像尺寸减小的比例,5为minNeighbors
|
|
|
|
|
|
|
|
faces = 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))
|
|
|
|
|
|
|
|
# 样本数加1
|
|
|
|
|
|
|
|
sample_num += 1
|
|
|
|
|
|
|
|
# 保存图像,把灰度图片看成二维数组来检测人脸区域,这里是保存在data缓冲文件夹内
|
|
|
|
|
|
|
|
T = Total_face_num
|
|
|
|
|
|
|
|
cv2.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:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
else: # 控制台内输出进度条
|
|
|
|
|
|
|
|
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() # 刷新控件以实时显示进度
|
|
|
|
|
|
|
|
|