Emotion API Android Java 퀵 스타트

등록일시: 2018-01-18 08:00,  수정일시: 2018-01-18 08:00
조회수: 4,065
이 문서는 Cognitive Services 기술을 널리 알리고자 하는 개인적인 취지로 제공되는 번역문서입니다. 이 문서에 대한 모든 저작권은 마이크로소프트에 있으며 요청이 있을 경우 언제라도 게시가 중단될 수 있습니다. 번역 내용에 오역이 존재할 수 있고 주석은 번역자 개인의 의견일 뿐이며 마이크로소프트는 이에 관한 어떠한 보장도 하지 않습니다. 번역이 완료된 이후에도 대상 제품 및 기술이 개선되거나 변경됨에 따라 원문의 내용도 변경되거나 보완되었을 수 있으므로 주의하시기 바랍니다.
본문에서는 Android Java를 사용해서 Emotion API를 사용하는 방법을 살펴봅니다.
중요

기존의 Video API 미리보기는 2017년 10월 30일에 만료되었습니다. 새로운 Video Indexer API 미리보기를 이용해서 동영상에서 손쉽게 의미 있는 정보를 얻고, 말/음성, 얼굴, 인물 및 감정을 감지하여 검색 결과 같은 콘텐츠 탐색 경험을 향상시켜 보십시오. 보다 자세한 정보는 Video Indexer (미리보기)를 참고하시기 바랍니다.

본문에서는 Emotion API Android 클라이언트 라이브러리를 통해서 Emotion API의 Recognize 메서드로 Emotion API 미리보기를 신속하게 시작할 수 있는 유용한 정보와 예제 코드를 제공합니다. 본문의 예제는 Java를 사용해서 사람들이 표현하는 감정을 인식하는 방법을 보여줍니다.

역주

위의 설명과는 달리, 본문의 예제는 Emotion API Android 클라이언트 라이브러리를 사용하지 않습니다. 본문의 코드를 직접 테스트해보시려면 Face API Java 퀵 스타트 문서를 참고해서 Java 프로젝트를 구성하시기 바랍니다.

요구 사항

감정 인식 Android Java 요청 예제

// // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Main
{
    public static void main(String[] args)
    {
        HttpClient httpClient = new DefaultHttpClient();

        try
        {
            // NOTE: You must use the same region in your REST call as you used to obtain your subscription keys.
            //   For example, if you obtained your subscription keys from westcentralus, replace "westus" in the 
            //   URL below with "westcentralus".
            URIBuilder uriBuilder = new URIBuilder("https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize");

            URI uri = uriBuilder.build();
            HttpPost request = new HttpPost(uri);

            // Request headers. Replace the example key below with your valid subscription key.
            request.setHeader("Content-Type", "application/json");
            request.setHeader("Ocp-Apim-Subscription-Key", "13hc77781f7e4b19b5fcdd72a8df7156");

            // Request body. Replace the example URL below with the URL of the image you want to analyze.
            StringEntity reqEntity = new StringEntity("{ \"url\": \"http://example.com/images/test.jpg\" }");
            request.setEntity(reqEntity);

            HttpResponse response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null)
            {
                System.out.println(EntityUtils.toString(entity));
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

감정 인식 결과 응답 살펴보기

호출에 성공하면 얼굴 항목들과 각 항목에 관련된 감정 점수들의 배열이 얼굴 사각형 크기의 역순으로 반환됩니다. 반면 빈 응답은 얼굴이 감지되지 않았음을 의미합니다. 감정 항목은 다음과 같은 필드들을 포함하고 있습니다:

  • faceRectangle - 이미지 상의 얼굴 직사각형의 위치.
  • scores - 이미지의 각 얼굴들에 대한 감정 점수.
application/json 
[
  {
    "faceRectangle": {
      "left": 68,
      "top": 97,
      "width": 64,
      "height": 97
    },
    "scores": {
      "anger": 0.00300731952,
      "contempt": 5.14648448E-08,
      "disgust": 9.180124E-06,
      "fear": 0.0001912825,
      "happiness": 0.9875571,
      "neutral": 0.0009861537,
      "sadness": 1.889955E-05,
      "surprise": 0.008229999
    }
  }
]