0

我目前正在尝试构建一个情绪识别系统来使用 DeepFace 库。通过最初使用以下代码,我能够首先检测到面部:

import cv2
from deepface import DeepFace

cam = cv2.VideoCapture(0)

faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

while True:

    ret, frame = cam.read()
    print(ret)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, 1.1, 4)

    for(x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    font = cv2.FONT_HERSHEY_SIMPLEX

    cv2.putText(frame, "FPS: {}".format(cam.get(cv2.CAP_PROP_FPS)), (10,30), font, 1, (0, 255, 255), 2, cv2.LINE_4) 
    cv2.putText(frame, 'Press q to quit', (200,450), font, 1, (0, 255, 255), 2, cv2.LINE_4)
    cv2.imshow("ERS Visual V1.0 - Emotion Recognition System", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()
quit()

人脸识别结果

但是,添加行后:

emotRes = DeepFace.analyze(frame, actions = ['emotion'])
cv2.putText(frame, emotRes['emotion'], (50, 50), font, 1, (0, 0, 255), 2, cv2.LINE_4)

如图:

import cv2
from deepface import DeepFace

cam = cv2.VideoCapture(0)

faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

while True:

    ret, frame = cam.read()
    print(ret)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, 1.1, 4)

    for(x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    font = cv2.FONT_HERSHEY_SIMPLEX

    emotRes = DeepFace.analyze(frame, actions = ['emotion'])
    cv2.putText(frame, emotRes['emotion'], (50, 50), font, 1, (0, 0, 255), 2, cv2.LINE_4)

    cv2.putText(frame, "FPS: {}".format(cam.get(cv2.CAP_PROP_FPS)), (10,30), font, 1, (0, 255, 255), 2, cv2.LINE_4) 
    cv2.putText(frame, 'Press q to quit', (200,450), font, 1, (0, 255, 255), 2, cv2.LINE_4)
    cv2.imshow("ERS Visual V1.0 - Emotion Recognition System", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()
quit()

该程序基本上试图启动,因为我可以看到相机试图打开,但然后无缘无故地退出而没有任何错误,我不明白它为什么这样做。

情绪识别结果

如果有人能够解释为什么会发生这种情况以及我能做些什么来解决这个问题,我将不胜感激。

4

0 回答 0