DeepFace出现无法使用的情况 #11
Unanswered
Justjustifyjudge
asked this question in
Q&A
Replies: 1 comment
-
对stream_cut的暂存图片路径进行了修改,再加了一些魔法,似乎有时行有时不行。 import cv2
import face_recognition
# 初始化摄像头
cap = cv2.VideoCapture(0)
# 调整摄像头捕捉的图像大小
cap.set(3, 640) # 宽度
cap.set(4, 480) # 高度
# 加载已知人脸图像并提取特征
known_face_img = face_recognition.load_image_file(r"C:\Users\linyiwu\Desktop\datasets\face\train\0002.jpg")
known_face_encoding = face_recognition.face_encodings(known_face_img)[0]
while True:
ret, frame = cap.read()
# 将图像转换为 RGB 格式(face_recognition 要求输入为 RGB 格式)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 在图像中检测人脸
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
# 对检测到的人脸进行处理
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# 进行人脸识别
matches = face_recognition.compare_faces([known_face_encoding], face_encoding)
name = "Unknown"
if matches[0]:
name = "Known Person"
# 在人脸周围绘制矩形并显示姓名
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# 显示图像
cv2.imshow('Face Recognition', frame)
# 按下 'q' 键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头资源并关闭窗口
cap.release()
cv2.destroyAllWindows() deepface: '''
Author: lin
Time: 2024年4月22日10:40:22
Description: 实现摄像头的调用,人脸检测和验证的合并
'''
import cv2
from deepface import DeepFace
# 初始化摄像头
cap = cv2.VideoCapture(0)
# 调整摄像头捕捉的图像大小
cap.set(3, 640) # 宽度
cap.set(4, 480) # 高度
# 设置用于比对的人脸图像文件夹路径
base_folder = r'C:\Users\linyiwu\Desktop\datasets\face\train'
img2_pathe = r"C:\Users\linyiwu\Desktop\datasets\face\train\0001.jpg"
while True:
ret, frame = cap.read()
# 保存摄像头捕捉的图像到临时文件
temp_face_path = r"C:\Users\linyiwu\Desktop\datasets\face\train\temp_face.jpg"
cv2.imwrite(temp_face_path, frame)
# 调用DeepFace进行人脸验证
try:
# result = DeepFace.verify(img1_path=temp_face_path, img2_path=img2_path, model_name='GhostFaceNet', enforce_detection=False)
# Debug
# result = DeepFace.verify(img1_path=img2_pathe, img2_path=img2_pathe, model_name='DeepFace', enforce_detection=False)
result = DeepFace.verify(img1_path=r'C:\Users\linyiwu\Desktop\datasets\face\train\temp_face.jpg', img2_path=r"C:\Users\linyiwu\Desktop\datasets\face\train\0001.jpg")
# 提取验证结果
verified = result['verified']
distance = result['distance']
if verified:
verified_text = f"Verified: {verified} - distance: {distance:0.4}"
else:
verified_text = f"Not Verified - distance: {distance:0.4}"
cv2.putText(frame, verified_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# 显示图像
cv2.imshow('Face Verification', frame)
except:
cv2.imshow('Face Verification', frame)
print("No face detected")
# 按下 'q' 键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头资源并关闭窗口
cap.release()
cv2.destroyAllWindows() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
DeepFace处理单张图片时没有问题,但是在streamcut之后处理会发生报错:
ValueError: Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
代码如下:(其中我是用try把报错给跳了,直接做try里面的内容时可以看到有关报错)
现在暂时是使用face_recognition库的方法,作为替换的补丁,大家有什么思路吗?
Beta Was this translation helpful? Give feedback.
All reactions