代码拉取完成,页面将自动刷新
import face_recognition
import numpy as np
from detect import *
# 人脸认证相关
def face(path):
known_names = []
known_encodings = []
for image_name in os.listdir(path):
load_image = face_recognition.load_image_file(os.path.join(path, image_name))
image_face_encoding = face_recognition.face_encodings(load_image)[0]
known_names.append(image_name.split('.')[0])
known_encodings.append(image_face_encoding)
video_capture = cv2.VideoCapture(0)
# video_capture = cv2.VideoCapture('./image/WeChat_20230808163758.mp4') # 读取视频
process_this_frame = True
while True:
ret, frame = video_capture.read()
rgb_frame = np.ascontiguousarray(frame[:, :, ::-1])
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
face_names = []
for face_encodings in face_encodings:
matches = face_recognition.compare_faces(known_encodings, face_encodings, tolerance=0.5)
if True in matches:
first_match_index = matches.index(True)
name = 'name:' + known_names[first_match_index]
else:
name = 'weiluru'
face_names.append(name)
process_this_frame = not process_this_frame
if len(face_locations) == 0:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
else:
continue
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255))
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
face('data/images')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。