import face_recognition as fr
import cv2
import numpy as np
import os
import time
from playsound import playsound
path = "./train/"
known_names = []
known_name_encodings = []
print("train:")
images = os.listdir(path)
for file_name in images:
print(file_name)
image = fr.load_image_file(path + file_name)
image_path = path + file_name
encoding = fr.face_encodings(image)[0]
#print(encoding)
known_name_encodings.append(encoding)
known_names.append(os.path.splitext(os.path.basename(image_path))[0].capitalize())
print(known_names)
#调用摄像头
cam = cv2.VideoCapture(0)
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)
while True:
print("cam.read")
ret,img = cam.read()
#展示捕获图片
print("imshow")
cv2.imshow('camera',img)
k = cv2.waitKey(20)
print("input key is ",k)
if k == 27: # esc
break
#test_image = "./test/test.jpg"
#image = cv2.imread(test_image)
image = img
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print("face_locations:")
face_locations = fr.face_locations(image)
print("face_encodings:")
face_encodings = fr.face_encodings(image, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
print("compare_faces:")
matches = fr.compare_faces(known_name_encodings, face_encoding, tolerance=0.4)
name = ""
print(matches)
print("face_distance:")
face_distances = fr.face_distance(known_name_encodings, face_encoding)
print(face_distances)
best_match = np.argmin(face_distances)
print(best_match)
if matches[best_match]:
name = known_names[best_match]
print(name)
if(name.lower()=="lintax"):
playsound('./audio/hello_lintax.mp3')
cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(image, (left, bottom - 15), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(image, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
#cv2.imwrite("./output.jpg", image)
time.sleep(5)
#释放资源
cam.release()
cv2.destroyAllWindows()

lintax
- 粉丝: 298
最新资源
- 计算机视觉行业调研与市场研究报告2023年.pptx
- 吉林大学2021年9月《计算机系统结构》作业考核试题及答案参考9.docx
- 计算机病毒的防治.ppt
- 第4章PLC的基本指令 PPT.ppt
- 多功能混凝土超声波检测仪软件系统研究与实现的开题报告.docx
- 浅析新形势下窥探互联网金融风险控制.docx
- 软件信息和电子商务产业发展情况调研报告.doc
- 基于通用软件的中学物理虚拟实验研究的开题报告.docx
- 基于神经网络的异构数据库语义集成的研究的开题报告.docx
- 软件工程职业道德规范培训资料.ppt
- 任务驱动教学在高校计算机基础教学中的应用研究.docx
- 通信管道工程工程量计算规则 ppt.ppt
- C语言实验室设备管理系统.doc
- 安全评价资料、数据采集分析处理原则及方法.doc
- 第9章网络营销综合应用实践.ppt
- (完整版)PMP考试题-成本管理.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0