something
py -m pip install opencv-python
py -m pip install deepface opencv-python
py -m pip install deepface -i https://mirrors.aliyun.com/pypi/simple/
py -m pip install deepface tensorflow-gpu
上面不行看这个:
python -m pip install opencv-python
# 基础安装(CPU)
python -m pip install deepface opencv-python
# 国内镜像加速
python -m pip install deepface -i https://mirrors.aliyun.com/pypi/simple/
# GPU 支持(需先配置 CUDA)
python -m pip install deepface tensorflow-gpu
还不行的话把python加上3
代码:
from deepface import DeepFace
import cv2
# 分析本地图片
result = DeepFace.analyze(
img_path="test.jpg",
actions=["emotion", "age", "gender"],
detector_backend="opencv"
)
print("检测结果:", result)
# 实时摄像头分析
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
print("当前情绪:", result[0]["dominant_emotion"])
if cv2.waitKey(1) == ord("q"):
break
cap.release()
还有一个:
import cv2
from deepface import DeepFace
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# 分析当前帧的表情
try:
result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
emotion = result[0]["dominant_emotion"]
cv2.putText(frame, f"Emotion: {emotion}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
except:
pass
cv2.imshow("Real-time Emotion Detection", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()