一、项目介绍:

通过摄像头实时识别人物的表情信息,包含5种表情信息:’neutral’, ‘happy’, ‘sad’, ‘surprise’, ‘anger’

二、环境安装

  • 平台:windows 11
  • 编译器:pycharm
  • cuda 11.3
  • cudnn 8.2.0.53
conda create -n emotion python=3.8
conda activate emotion 
pip install opencv-python
pip install openvino -i https://mirror.baidu.com/pypi/simple

三、执行代码

执行main.py文件

四、核心代码

import cv2 as cv
import numpy as np
from openvino.inference_engine import IECore


# 人脸检测&表情识别
def emotion_detect(frame):
    # 人脸检测模型
    weight_pb = "opencv_face_detector_uint8.pb"
    config_text = "opencv_face_detector.pbtxt"

    # 加载表情识别模型并设置输入与输出
    model_xml = "emotions-recognition-retail-0003.xml"
    model_bin = "emotions-recognition-retail-0003.bin"

    labels = ['neutral', 'happy', 'sad', 'surprise', 'anger']

    ie = IECore()
    emotion_net = ie.read_network(model=model_xml, weights=model_bin)

    input_blob = next(iter(emotion_net.input_info))

    exec_net = ie.load_network(network=emotion_net, device_name="CPU", num_requests=2)

    # 读取人脸检测模型
    net = cv.dnn.readNetFromTensorflow(weight_pb, config=config_text)
    h, w, c = frame.shape
    blobImage = cv.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False)
    net.setInput(blobImage)
    cvOut = net.forward()

    # 绘制检测矩形
    for detection in cvOut[0, 0, :, :]:
        score = float(detection[2])
        if score > 0.5:
            left = detection[3] * w
            top = detection[4] * h
            right = detection[5] * w
            bottom = detection[6] * h

            # roi  关键点检测
            y1 = np.int32(top) if np.int32(top) > 0 else 0
            y2 = np.int32(bottom) if np.int32(bottom) < h else h - 1
            x1 = np.int32(left) if np.int32(left) > 0 else 0
            x2 = np.int32(right) if np.int32(right) < w else w - 1
            roi = frame[y1:y2, x1:x2, :]
            image = cv.resize(roi, (64, 64))
            image = image.transpose((2, 0, 1))  # Change data layout from HWC to CHW
            res = exec_net.infer(inputs={input_blob: [image]})
            prob_emotion = res['prob_emotion']
            probs = np.reshape(prob_emotion, 5)
            txt = labels[np.argmax(probs)]
            cv.putText(frame, txt, (np.int32(left), np.int32(top)), cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2)
            cv.rectangle(frame, (np.int32(left), np.int32(top)),
                         (np.int32(right), np.int32(bottom)), (0, 0, 255), 2, 8, 0)
            # print(txt)
            return txt
            # 将每一帧写入输出文件
        out.write(frame)
        # cv.imshow("emotion-detect-demo", frame)
        # 按Q 退出 waitKey控制播放速度
        if cv.waitKey(1) & 0xFF == ord('q'):
            break

if __name__ == "__main__":
#
#    1调用摄像头
#   cap = cv.VideoCapture(0)

    # 2打开视频
    path = "dance.mp4"
    cap = cv.VideoCapture(path)

    while True:
        ret, frame = cap.read()
        if ret is not True:
            break
        emotion_detect(frame)


        # 显示帧可选
       # cv.imshow('frame', frame)
        # 按Q 退出 waitKey控制播放速度
        if cv.waitKey(1) & 0xFF == ord('q'):
            break

资源下载
免费资源
链接点击下载(提取码: g7zc)复制