login register Sysop! about ME  
qrcode
    최초 작성일 :    2018년 01월 15일
  최종 수정일 :    2018년 01월 15일
  작성자 :    soggun
  편집자 :    soggun (송원석)
  읽음수 :    12,682

강좌 목록으로 돌아가기

필자의 잡담~

이번 문서는 Microsoft 인공지능 서비스인 Cognitive Service의 Face API를 C#으로 시작하는 방법에 대해 설명합니다.

개발자들이 쉽게 접근하고, 사용할 수 있는 Cognitive Service의 다양한 컬럼들을 계속해서 번역하여 제공할 예정입니다.

본 번역문서는 개인적인 취지로 번역되어 제공되는 문서로, 원문을 비롯한 모든 저작권은 마이크로소프트사에 있습니다. 마이크로소프트사의 요청이 있을 경우, 언제라도 게시가 중단될 수 있습니다. 본 번역문서에는 오역이 포함되어 있을 수 있으며 주석도 번역자 개인의 견해일뿐입니다. 마이크로소프트사는 본 문서의 번역된 내용에 대해 일체의 보장을 하지 않습니다. 번역이 완료된 뒤에도 제품이 업그레이드 되거나 기능이 변경됨에 따라 원문도 변경되거나 보완되었을 수 있으므로 참고하시기 바랍니다.

원문: https://docs.microsoft.com/ko-kr/azure/cognitive-services/face/quickstarts/csharp

Face API C# 퀵 스타트

본문에서는 C#과 Face API를 사용해서 다음과 같은 작업을 신속하게 수행하기 위해 필요한 유용한 정보와 예제 코드를 제공합니다:

요구 사항

  • Microsoft Face API Windows SDK는 여기에서 얻을 수 있습니다.
  • 무료 구독 키를 발급받는 방법은 구독 키 발급받기 문서를 참고하시기 바랍니다.

C#을 이용해서 Face API로 이미지의 얼굴 감지하기

Face - Detect 메서드를 이용해서 이미지의 얼굴을 감지하고 다음과 같은 얼굴 특징을 반환받습니다:

  • 얼굴 ID: 다양한 Face API 시나리오에서 사용되는 얼굴의 고유 ID
  • 얼굴 사각형: 이미지 내의 얼굴 위치를 나타내는 Left, Top, Width, Height
  • 랜드마크: 얼굴 구성 요소의 중요한 위치를 나타내는 27 곳의 얼굴 랜드마크 배열
  • 나이, 성별, 미소짓는 정도, 머리 자세 및 얼굴의 털을 비롯한 얼굴 특징

얼굴 감지 C# 요청 예제

본문의 예제는 Face API 클라이언트 라이브러리를 이용해서 C#으로 작성되었습니다.

  1. Visual Studio에서 새로운 콘솔 솔루션을 생성합니다.
  2. Program.cs 파일의 내용을 다음 코드로 대체합니다.
  3. subscriptionKey 값을 여러분이 발급받은 유효한 구독 키로 대체합니다.
  4. uriBase 값을 여러분이 구독 키를 발급받은 지역을 사용하도록 변경합니다.
  5. 프로그램을 실행합니다.
  6. 하드 드라이브에 위치한 이미지의 경로를 입력합니다.
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
 
namespace CSHttpClientSample
{
    static class Program
    {
        // **********************************************
        // *** Update or verify the following values. ***
        // **********************************************
 
        // Replace the subscriptionKey string value with your valid subscription key.
        const string subscriptionKey = "13hc77781f7e4b19b5fcdd72a8df7156";
 
        // Replace or verify the region.
        //
        // You must use the same region in your REST API call as you used to obtain your subscription keys.
        // For example, if you obtained your subscription keys from the westus region, replace 
        // "westcentralus" in the URI below with "westus".
        //
        // NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using
        // a free trial subscription key, you should not need to change this region.
        const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";
 
        static void Main()
        {
            // Get the path and filename to process from the user.
            Console.WriteLine("Detect faces:");
            Console.Write("Enter the path to an image with faces that you wish to analzye: ");
            string imageFilePath = Console.ReadLine();
 
            // Execute the REST API call.
            MakeAnalysisRequest(imageFilePath);
 
            Console.WriteLine("\nPlease wait a moment for the results to appear. Then, press Enter to exit...\n");
            Console.ReadLine();
        }
 
        /// <summary>
        /// Gets the analysis of the specified image file by using the Computer Vision REST API.
        /// </summary>
        /// <param name="imageFilePath">The image file.</param>
        static async void MakeAnalysisRequest(string imageFilePath)
        {
            HttpClient client = new HttpClient();
 
            // Request headers.
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
 
            // Request parameters. A third optional parameter is "details".
            string requestParameters = "returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise";
 
            // Assemble the URI for the REST API Call.
            string uri = uriBase + "?" + requestParameters;
 
            HttpResponseMessage response;
 
            // Request body. Posts a locally stored JPEG image.
            byte[] byteData = GetImageAsByteArray(imageFilePath);
 
            using (ByteArrayContent content = new ByteArrayContent(byteData))
            {
                // This example uses content type "application/octet-stream".
                // The other content types you can use are "application/json" and "multipart/form-data".
                content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
 
                // Execute the REST API call.
                response = await client.PostAsync(uri, content);
 
                // Get the JSON response.
                string contentString = await response.Content.ReadAsStringAsync();
 
                // Display the JSON response.
                Console.WriteLine("\nResponse:\n");
                Console.WriteLine(JsonPrettyPrint(contentString));
            }
        }
 
        /// <summary>
        /// Returns the contents of the specified file as a byte array.
        /// </summary>
        /// <param name="imageFilePath">The image file to read.</param>
        /// <returns>The byte array of the image data.</returns>
        static byte[] GetImageAsByteArray(string imageFilePath)
        {
            FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
            BinaryReader binaryReader = new BinaryReader(fileStream);
            return binaryReader.ReadBytes((int)fileStream.Length);
        }
 
        /// <summary>
        /// Formats the given JSON string by adding line breaks and indents.
        /// </summary>
        /// <param name="json">The raw JSON string to format.</param>
        /// <returns>The formatted JSON string.</returns>
        static string JsonPrettyPrint(string json)
        {
            if (string.IsNullOrEmpty(json))
                return string.Empty;
 
            json = json.Replace(Environment.NewLine, "").Replace("\t""");
 
            StringBuilder sb = new StringBuilder();
            bool quote = false;
            bool ignore = false;
            int offset = 0;
            int indentLength = 3;
 
            foreach (char ch in json)
            {
                switch (ch)
                {
                    case '"':
                        if (!ignore) quote = !quote;
                        break;
                    case '\'':
                        if (quote) ignore = !ignore;
                        break;
                }
 
                if (quote)
                    sb.Append(ch);
                else
                {
                    switch (ch)
                    {
                        case '{':
                        case '[':
                            sb.Append(ch);
                            sb.Append(Environment.NewLine);
                            sb.Append(new string(' ', ++offset * indentLength));
                            break;
                        case '}':
                        case ']':
                            sb.Append(Environment.NewLine);
                            sb.Append(new string(' ', --offset * indentLength));
                            sb.Append(ch);
                            break;
                        case ',':
                            sb.Append(ch);
                            sb.Append(Environment.NewLine);
                            sb.Append(new string(' ', offset * indentLength));
                            break;
                        case ':':
                            sb.Append(ch);
                            sb.Append(' ');
                            break;
                        default:
                            if (ch != ' ') sb.Append(ch);
                            break;
                    }
                }
            }
 
            return sb.ToString().Trim();
        }
    }
}

얼굴 감지 결과 응답 살펴보기

정상적으로 처리된 응답은 JSON 형식으로 반환됩니다. 다음은 성공한 응답의 사례입니다:

Response:
 
[
   {
      "faceId""f7eda569-4603-44b4-8add-cd73c6dec644",
      "faceRectangle": {
         "top": 131,
         "left": 177,
         "width": 162,
         "height": 162
      },
    "faceAttributes": {
        "smile": 0.0,
        "headPose": {
            "pitch": 0.0,
            "roll": 0.1,
            "yaw": -32.9
        },
        "gender""female",
        "age": 22.9,
        "facialHair": {
            "moustache": 0.0,
            "beard": 0.0,
            "sideburns": 0.0
        },
        "glasses""NoGlasses",
        "emotion": {
            "anger": 0.0,
            "contempt": 0.0,
            "disgust": 0.0,
            "fear": 0.0,
            "happiness": 0.0,
            "neutral": 0.986,
            "sadness": 0.009,
            "surprise": 0.005
        },
        "blur": {
            "blurLevel""low",
            "value": 0.06
        },
        "exposure": {
            "exposureLevel""goodExposure",
            "value": 0.67
        },
        "noise": {
            "noiseLevel""low",
            "value": 0.0
        },
        "makeup": {
            "eyeMakeup"true,
            "lipMakeup"true
        },
        "accessories": [
 
        ],
        "occlusion": {
            "foreheadOccluded"false,
            "eyeOccluded"false,
            "mouthOccluded"false
        },
        "hair": {
            "bald": 0.0,
            "invisible"false,
            "hairColor": [
                {
                    "color""brown",
                    "confidence": 1.0
                },
                {
                    "color""black",
                    "confidence": 0.87
                },
                {
                    "color""other",
                    "confidence": 0.51
                },
                {
                    "color""blond",
                    "confidence": 0.08
                },
                {
                    "color""red",
                    "confidence": 0.08
                },
                {
                    "color""gray",
                    "confidence": 0.02
                }
            ]
        }
    }
   }
]

C#을 이용해서 Face API로 인물 그룹 생성하기

Person Group - Create a Person Group 메서드를 이용해서 지정한 인물 그룹 ID (personGroupId), 이름 (name) 및 사용자가 제공한 사용자 데이터로 (userData) 인물 그룹을 생성합니다.

Person Group - Create a Person Group C# 요청 예제

Visual Studio에서 새로운 콘솔 솔루션을 생성한 다음, Program.cs 파일의 내용을 다음 코드로 대체합니다. string uri를 여러분이 구독 키를 발급받은 지역을 사용하도록 변경하고, "Ocp-Apim-Subscription-Key" 값을 유효한 구독 키로 대체합니다.

using System;
using System.Net.Http.Headers;
using System.Net.Http;
 
namespace CSHttpClientSample
{
    static class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter an ID for the group you wish to create:");
            Console.WriteLine("(Use numbers, lower case letters, '-' and '_'. The maximum length of the personGroupId is 64.)");
 
            string personGroupId = Console.ReadLine();
            MakeCreateGroupRequest(personGroupId);
 
            Console.WriteLine("\n\n\nWait for the result below, then hit ENTER to exit...\n\n\n");
            Console.ReadLine();
        }
 
        static async void MakeCreateGroupRequest(string personGroupId)
        {
            var client = new HttpClient();
 
            // Request headers - replace this example key with your valid key.
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key""6726adbabb494773a28a7a5a21d5974a");
 
            // Request URI string.
            // 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 westus, replace "westcentralus" in the 
            //   URI below with "westus".
            string uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/" + personGroupId;
 
            // Here "name" is for display and doesn't have to be unique. Also, "userData" is optional.
            string json = "{\"name\":\"My Group\", \"userData\":\"Some data related to my group.\"}";
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
            HttpResponseMessage response = await client.PutAsync(uri, content);
 
            // If the group was created successfully, you'll see "OK".
            // Otherwise, if a group with the same personGroupId has been created before, you'll see "Conflict".
            Console.WriteLine("Response status: " + response.StatusCode);
        }
    }
}

authored by


 
 
.NET과 Java 동영상 기반의 교육사이트

로딩 중입니다...

서버 프레임워크 지원 : NeoDEEX
based on ASP.NET 3.5
Creative Commons License
{5}
{2} 읽음   :{3} ({4})