[Anaconda][OpenVINO]表情检测

原文链接:应用 | OpenCV + OpenVINO实现人脸表情识别_英特尔边缘计算社区-CSDN博客

1.环境准备

先安装python 3.6.1,再在cmd里运行下面指令
python -m pip install --upgrade pip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openvino
python main.py

@2022.1.18添加说明

        如果按照以上步奏,出现“from .ie_api import * ”  模块未找到的问题,可能是你的VS环境没装好,建议装2005到2019的VC运行环境

2.python代码

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

weight_pb = "./opencv/opencv_face_detector_uint8.pb";
config_text = "./opencv/opencv_face_detector.pbtxt";

model_xml = "./openvino/emotions-recognition-retail-0003/emotions-recognition-retail-0003.xml"
model_bin = "./openvino/emotions-recognition-retail-0003/emotions-recognition-retail-0003.bin"

labels = ['neutral', 'happy', 'sad', 'surprise', 'anger']
emotion_labels = ["neutral", "anger", "disdain", "disgust", "fear", "happy", "sad", "surprise"]

emotion_net = IENetwork(model=model_xml, weights=model_bin)
ie = IECore()
versions = ie.get_versions("CPU")
input_blob = next(iter(emotion_net.inputs))
n, c, h, w = emotion_net.inputs[input_blob].shape
print(emotion_net.inputs[input_blob].shape)

output_info = emotion_net.outputs[next(iter(emotion_net.outputs.keys()))]
output_info.precision = "FP32"
exec_net = ie.load_network(network=emotion_net, device_name="CPU")
root_dir = "D:/facedb/emotion_dataset/"
count_ = 1

def emotion_detect(frame):
    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 and detect landmark
            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)

if __name__ == "__main__":
    capture = cv.VideoCapture(0)
    while True:
        ret, frame = capture.read()
        if ret is not True:
            continue
        emotion_detect(frame)
        cv.imshow("emotion-detect-demo", frame)
        c = cv.waitKey(30)
        if c == 27:
            break

 工程链接:阿里云盘分享

说明:本工程使用PyCharm Community Edition 2021.2.3

猜你喜欢

转载自blog.csdn.net/qq_36251561/article/details/121102652