< 100: # 可以识别出已经训练的对象——直接输出姓名在屏幕上
if idnum in id_dict:
user_name = id_dict[idnum]
else:
# print("无法识别的ID:{} ".format(idnum), end="")
user_name = "Untagged user:" + str(idnum)
confidence = "{0}%", format(round(100 - confidence))
else: # 无法识别此对象,那么就开始训练
user_name = "unknown"
# print("检测到陌生人脸
")
# cv2.destroyAllWindows()
# global Total_face_num
# Total_face_num += 1
# Get_new_face() # 采集新人脸
# Train_new_face() # 训练采集到的新人脸
# write_config() # 修改配置文件
# recognizer.read('aaa.yml') # 读取新识别器
# 加载一个字体用于输出识别对象的信息
font = cv2.FONT_HERSHEY_SIMPLEX
# 输出检验结果以及用户名
cv2.putText(img, str(user_name), (x + 5, y - 5), font, 1, (0, 0, 255), 1)
cv2.putText(img, str(confidence), (x + 5, y + h - 5), font, 1, (0, 0, 0), 1)
# 展示结果
# cv2.imshow('camera', img)
print("conf=" + str(conf), end=" ")
if 15 > conf > 0:
cur_poss = 1 # 表示可以识别
elif 60 > conf > 35:
cur_poss = 1 # 表示可以识别
else:
cur_poss = 0 # 表示不可以识别
k = cv2.waitKey(1)
if k == 27:
# cam.release() # 释放资源
cv2.destroyAllWindows()
break
ave_poss += cur_poss
if ave_poss >= 5: # 有一半以上识别说明可行则返回
return i
return 0 # 全部过一遍还没识别出说明无法识别
'''
============================================================================================
以上是关于刷脸功能的设计
============================================================================================
'''
def f_scan_face_thread():
# 使用之前训练好的模型
# 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_lock
print("锁被释放0")
system_state_lock = 0 # 修改system_state_lock,释放资源
def f_scan_face():
global system_state_lock
print("
当前锁的值为:" + str(system_state_lock))
if system_state_lock == 1:
print("阻塞,因为正在刷脸")
return 0
elif system_state_lock == 2: # 如果正在录入新面孔就阻塞
print("
刷脸被录入面容阻塞
"
"")
return 0
system_state_lock = 1
p = threading.Thread(target=f_scan_face_thread)
p.setDaemon(True) # 把线程P设置为守护线程 若主线程退出 P也跟着退出
p.start()
def f_rec_face_thread():
var.set('录入')
cv2.destroyAllWindows()
global Total_face_num
Total_face_num += 1
Get_new_face() # 采集新人脸
print("采集完毕,开始训练")
global system_state_lock # 采集完就可以解开锁
print("锁被释放0")
system_state_lock = 0
Train_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 system_state_lock
print("当前锁的值为:" + str(system_state_lock))
if system_state_lock == 2:
print("阻塞,因为正在录入面容")
return 0
else:
system_state_lock = 2 # 修改system_state_lock
print("改为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('Cheney' 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控件
# 在窗口界面设置放置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_b = tk.Button(window, text='退出', font=('Arial', 12), width=10, height=2, command=f_exit)
button_b.place(x=800, y=320)
panel = tk.Label(window, width=500, height=350) # 摄像头模块大小
panel.place(x=10, y=100) # 摄像头模块的位置
window.config(cursor="arrow")
def video_loop(): # 用于在label内动态展示摄像头内容(摄像头嵌入控件)
# success, img = camera.read() # 从摄像头读取照片
global success
global img
if success:
cv2.waitKey(1)
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)
window.after(1, video_loop)
video_loop()
# 窗口循环,用于显示
window.mainloop()
'''
============================================================================================
以上是关于界面的设计
============================================================================================
'''
复制代码