Как обнаружить эмоции с помощью Azure API? - PullRequest
0 голосов
/ 19 февраля 2020

Я хотел бы создать простое Python приложение, распознающее эмоции лица по заданному URL-адресу через Azure Face / Emotions API. Я следую этой документации:

Пока что я выполнил часть по распознаванию лиц, но я немного застрял, как вызвать модель 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.

Любая помощь будет принята с благодарностью. Спасибо!

1 Ответ

1 голос
/ 19 февраля 2020

Вы можете использовать следующий код для определения эмоций,

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
...