我有这段代码可以检测面部表情,但它会在没有面部表情的地方找到面部表情。所以我想知道是否可以放心地做到这一点。
我尝试在谷歌上搜索如何做到这一点,但没有找到任何有用的东西。
import cv2
from deepface import DeepFace
# Load face cascade classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Start capturing video
cap = cv2.VideoCapture('ap.mp4')
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Convert frame to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert grayscale frame to RGB format
rgb_frame = cv2.cvtColor(gray_frame, cv2.COLOR_GRAY2RGB)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
# Extract the face ROI (Region of Interest)
face_roi = rgb_frame[y:y + h, x:x + w]
# Perform emotion analysis on the face ROI
result = DeepFace.analyze(face_roi, actions=['emotion'], enforce_detection=False)
# Determine the dominant emotion
print(result[0]['emotion'])
emotion = result[0]['dominant_emotion']
# Draw rectangle around face and label with predicted emotion
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Real-time Emotion Detection', frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and close all windows
cap.release()
cv2.destroyAllWindows()
可以通过过滤掉置信度低于特定阈值的情绪来增加对 DeepFace 结果的置信度。以下是更新后的代码,其中包含一个添加的阈值:
import cv2
from deepface import DeepFace
# Load face cascade classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Start capturing video
cap = cv2.VideoCapture('ap.mp4')
# 设置置信度阈值
confidence_threshold = 0.6 # 例如 60% 的置信度
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Convert frame to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert grayscale frame to RGB format
rgb_frame = cv2.cvtColor(gray_frame, cv2.COLOR_GRAY2RGB)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
# Extract the face ROI (Region of Interest)
face_roi = rgb_frame[y:y + h, x:x + w]
# Perform emotion analysis on the face ROI
result = DeepFace.analyze(face_roi, actions=['emotion'], enforce_detection=False)
# Get the dominant emotion and its confidence
dominant_emotion = result[0]['dominant_emotion']
confidence = result[0]['emotion'][dominant_emotion]
# Only display the emotion if confidence is above the threshold
if confidence > confidence_threshold:
# Draw rectangle around face and label with predicted emotion
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(frame, dominant_emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Real-time Emotion Detection', frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and close all windows
cap.release()
cv2.destroyAllWindows()
在此代码中:
-
我们设置了一个
confidence_threshold
来定义显示情绪所需的最小置信度级别。 -
我们从
DeepFace.analyze()
的结果中提取了dominant_emotion
和它的confidence
。 -
我们仅在
confidence
高于confidence_threshold
时才显示检测到的情绪。
通过调整
confidence_threshold
,可以控制在图像上绘制情绪标签之前的置信度级别。更高的阈值(例如,0.8 或 0.9)将导致更少但可能更准确的情绪检测,而较低的阈值将显示更多检测结果,但可能包括更多误报。