首页 > 解决方案 > 如何使用 Azure API 检测情绪?

问题描述

我想创建一个简单的 Python 应用程序,通过 Azure Face/Emotions API 从给定的 URL 识别面部情绪。我正在关注此文档:

所以,到目前为止,我做了人脸识别部分,但我有点卡住了如何调用 Emotion 模型和显示结果。

import urllib.request  
from azure.cognitiveservices.vision.face import FaceClient
from azure.cognitiveservices.vision.face.models import Emotion
from msrest.authentication import CognitiveServicesCredentials

# Image
URL = "https://upload.wikimedia.org/wikipedia/commons/5/55/Dalailama1_20121014_4639.jpg"

# API
KEY = "xxx"
ENDPOINT = "https://happyai.cognitiveservices.azure.com/"

# Now there is a trained endpoint that can be used to make a prediction
predictor = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY))

detected_faces = predictor.face.detect_with_url(url=URL)
if not detected_faces:
    raise Exception('No face detected from image {}'.format(URL))

# Display the detected face ID in the first single-face image.
# Face IDs are used for comparison to faces (their IDs) detected in other images.
print('Detected face ID from', URL, ':')
for face in detected_faces: print (face.face_id)
print()

# Save this ID for use in Find Similar
first_image_face_ID = detected_faces[0].face_id

# Call Emotion model

# Display the results.

任何帮助将不胜感激。谢谢!

标签: pythonazureface-detection

解决方案


您可以使用以下代码进行情绪检测,

def det_emotion(self, frame, count):

        image_path = self.path_folder + "/img/frame%d.jpg" % count
        image_data = open(image_path, "rb")

        params = {
            'returnFaceId': 'true',
            'returnFaceLandmarks': 'false',
            'returnRecognitionModel':'false',
        }

        response = requests.post(self.face_api_url, params=params,data=image_data)
        response.raise_for_status()
        faces = response.json()
        frame = self.add_square(frame, faces)

        return frame

推荐阅读