コード例 #1
0
        async void IdentifyAsync()
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

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

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

            HttpResponseMessage response;

            // Request body
            if (textBox_faceidss.Lines.Length == 0)
            {
                return;
            }
            List <string> facesIds = new List <string>();

            for (int i = 0; i < textBox_faceidss.Lines.Length; i++)
            {
                facesIds.Add(textBox_faceidss.Lines[i]);
            }
            IdentifyJson identifyJson = new IdentifyJson
            {
                personGroupId = textBox_IdentifyPersonGroupId.Text,
                faceIds       = facesIds.ToArray(),
                maxNumOfCandidatesReturned = Convert.ToInt32(textBox_maxNumOfCandidatesReturned.Text),
                confidenceThreshold        = trackBar_confidenceThreshold.Value / 100.0
            };
            string json = JsonConvert.SerializeObject(identifyJson, Formatting.Indented);

            byte[] byteData = Encoding.UTF8.GetBytes(json);

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

                json = await response.Content.ReadAsStringAsync();

                textBox_Output.Text = JsonHelper.ConvertJsontoReadableString(json);
            }
        }
コード例 #2
0
        async void Identify()
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

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

            var uri = GeneralConstants.str_API_URL + "/face/v1.0/identify?" + queryString;

            HttpResponseMessage response;

            // Request body
            FaceClass[] faces = JsonConvert.DeserializeObject <FaceClass[]>(textBox_DetectResult.Text);
            if (faces.Length == 0)
            {
                return;
            }
            List <string> facesIds = new List <string>();

            foreach (FaceClass fc in faces)
            {
                facesIds.Add(fc.faceId);
            }
            IdentifyJson identifyJson = new IdentifyJson
            {
                personGroupId = textBox_PersonGroupId.Text,
                faceIds       = facesIds.ToArray(),
                maxNumOfCandidatesReturned = 1,
                confidenceThreshold        = trackBar_confidenceThreshold.Value / 100.0
            };
            string json = JsonConvert.SerializeObject(identifyJson, Formatting.Indented);

            byte[] byteData = Encoding.UTF8.GetBytes(json);

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

                json = await response.Content.ReadAsStringAsync();

                IdentifyResult[] ir = JsonConvert.DeserializeObject <IdentifyResult[]>(json);
                if (ir.Length > 0)
                {
                    for (int i = 0; i < ir.Length; i++)
                    {
                        if (ir[i].candidates.Length > 0)
                        {
                            string str_Name = await QueryPersonNameByPersonId(ir[i].candidates[0].personId);

                            textBox_IdentifyResult.Text += "姓名:" + str_Name + "\r\n可信度" + ir[i].candidates[0].confidence + "\r\n";
                        }
                        else
                        {
                            textBox_IdentifyResult.Text = "No candidates";
                        }
                    }
                }
                else
                {
                    textBox_IdentifyResult.Text = "Can not indentify from known faces";
                }
            }
        }