Esempio n. 1
0
        static async Task CreatePersonGroup(String subscription_key)
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscription_key);

            var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/hwasa?" + queryString;


            HttpResponseMessage response;

            String json_str = "{\"name\":\"Hwasa1\"}"; //目前 : recognition_01

            //Console.WriteLine(json_str);

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes(json_str);

            string result;

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = await client.PutAsync(uri, content);

                result = await response.Content.ReadAsStringAsync();
            }

            myJsonParsing my_jsonParse = new myJsonParsing();
            string        str_decode   = my_jsonParse.myJsonParse(result);

            Console.WriteLine("Return Json in the CreatePersonGroup() process : \n" + str_decode);
        }
Esempio n. 2
0
        static async Task AddFace(String subscription_key, String personId, string url)
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscription_key);


            var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/hwasa/persons/" + personId + "/persistedFaces?" + queryString;

            HttpResponseMessage response;

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes("{\"url\":\"" + url + "\"}");

            string result;

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = await client.PostAsync(uri, content);

                result = await response.Content.ReadAsStringAsync();
            }
            myJsonParsing my_jsonParse = new myJsonParsing();
            string        str_decode   = my_jsonParse.myJsonParse(result);

            Console.WriteLine("Return Json in the AddFace() process : \n" + str_decode);
        }
Esempio n. 3
0
        static async Task <bool> DetectFace(String subscription_key, string url)
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscription_key);

            // Request parameters
            queryString["returnFaceId"] = "true";

            var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?" + queryString;

            HttpResponseMessage response;

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes("{\"url\":\"" + url + "\"}");

            string result;

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = await client.PostAsync(uri, content);

                result = await response.Content.ReadAsStringAsync();
            }

            bool detectSuccess;

            if (result.Contains("error"))
            {
                myJsonParsing my_jsonParse = new myJsonParsing();
                string        str_decode   = my_jsonParse.myJsonParse(result);
                Console.WriteLine("Return Json in the DetectFace() process : \n" + str_decode);
                detectSuccess = false;
            }
            else if (result.Equals("[]")) // no face detected.
            {
                Console.WriteLine("Return Json in the DetectFace() process : \n");
                Console.WriteLine("No face detected.");
                detectSuccess = false;
            }
            else
            {
                result = result.Remove(0, 1);
                result = result.Remove(result.Length - 1, 1);
                myJsonParsing my_jsonParse = new myJsonParsing();
                string        str_decode   = my_jsonParse.myJsonParse(result);
                Console.WriteLine("Return Json in the DetectFace() process : \n" + str_decode);
                detectSuccess = true;
            }

            return(detectSuccess);
        }
Esempio n. 4
0
        static async Task <String> CreatePerson(String subscription_key)
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscription_key);

            var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/hwasa/persons?" + queryString;

            HttpResponseMessage response;

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes("{\"name\":\"hwasa\"}");
            string result;

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = await client.PostAsync(uri, content);

                result = await response.Content.ReadAsStringAsync();

                //Console.WriteLine(result);
            }

            myJsonParsing my_jsonParse = new myJsonParsing();
            string        str_decode   = my_jsonParse.myJsonParse(result);

            Console.WriteLine("Return Json in the CreatePerson() process : \n" + str_decode);
            string personId;

            if (!str_decode.Contains("error"))
            {
                int    index = str_decode.IndexOf("=");
                string temp  = str_decode.Remove(0, index + 1);
                temp     = temp.Trim();
                personId = temp;
            }
            else
            {
                personId = "error";
            }
            return(personId);
        }
Esempio n. 5
0
        static async Task <bool> FaceIdentitfy(String subscription_key)
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscription_key);

            var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/identify?" + queryString;

            HttpResponseMessage response;

            faceId = "[\"" + faceId + "\"]";

            // Request body
            byte[] byteData = Encoding.UTF8.GetBytes("{\"personGroupId\":\"hwasa\",\"faceIds\":" + faceId + "}");

            string result;
            bool   identifySuccess;

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = await client.PostAsync(uri, content);

                result = await response.Content.ReadAsStringAsync();
            }
            if (result.Contains("error"))
            {
                myJsonParsing my_jsonParse = new myJsonParsing();
                string        str_decode   = my_jsonParse.myJsonParse(result);
                Console.WriteLine("Return Json in the FaceIdentitfy() process : \n" + str_decode);
                identifySuccess = false;
            }
            else
            {
                result = result.Remove(0, 1);
                result = result.Remove(result.Length - 1, 1);
                myJsonParsing my_jsonParse = new myJsonParsing();
                string        str_decode   = my_jsonParse.myJsonParse(result);
                Console.WriteLine("Return Json in the FaceIdentitfy() process : \n" + str_decode);
                identifySuccess = true;
            }
            return(identifySuccess);
        }
Esempio n. 6
0
        static async Task DeletePersonGroup(String subscription_key)
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscription_key);

            var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/hwasa?" + queryString;

            var response = await client.DeleteAsync(uri);

            string result = await response.Content.ReadAsStringAsync();

            myJsonParsing my_jsonParse = new myJsonParsing();
            string        str_decode   = my_jsonParse.myJsonParse(result);

            Console.WriteLine("Return Json in the DeletePersonGroup() process : \n" + str_decode);
        }