如何使用Azure Cognitive Face API验证两张图片?需要示例代码

问题描述 投票:0回答:1

如何使用Azure Cognitive Face API验证两张图像?需要示例代码

我有一个人的两张照片。现在,我想比较那些图像并检查这些图像是否属于同一个人。从文档中我知道,我必须发送两个faceid和url。我试过了,但是没有用。可能是,我缺少了一些东西。请为我提供相同的帮助,并在可能的情况下为我提供一些示例代码。

等待您的回复。

authentication microsoft-cognitive face-recognition azure-cognitive-services face
1个回答
0
投票

尝试下面的控制台应用程序代码:

using Microsoft.Azure.CognitiveServices.Vision.Face;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;
using System;
using System.IO;
using System.Linq;
using System.Threading;

namespace FaceIdentityTest
{
    class Program
    {

        static void Main(string[] args)
        {


            string persionPicPath = @"<some path>\personPic.jpg";

            String[] picsPath = { @"<some path>\pic1.jpg", @"<some path>\pic2.jpg" };

            string endpoint = @"https://<your endpoint name>.cognitiveservices.azure.com/";
            string subscriptionKey = "<your subscription key>";



            IFaceClient faceClient = new FaceClient(
            new ApiKeyServiceClientCredentials(subscriptionKey),
            new System.Net.Http.DelegatingHandler[] { });

            faceClient.Endpoint = endpoint;

            // Create an empty PersonGroup
            Console.WriteLine("create person group");
            string personGroupId = "demogroup";
            faceClient.PersonGroup.CreateAsync(personGroupId, "demo group").GetAwaiter().GetResult();

            // Define a person named Bill
            Console.WriteLine("create a person in group");
            var createPersonResult = faceClient.PersonGroupPerson.CreateAsync(
                // Id of the PersonGroup that the person belonged to
                personGroupId,
                // Name of the person
                "Bill"
            ).GetAwaiter().GetResult();


            //Add a face to Bill
            Console.WriteLine("Add a face to person");
            using (Stream s = File.OpenRead(persionPicPath))
            {
                // Detect faces in the image and add to Anna
                faceClient.PersonGroupPerson.AddFaceFromStreamAsync(
                    personGroupId, createPersonResult.PersonId, s).GetAwaiter().GetResult();
            }

            //Train person group 
            Console.WriteLine("start train person group...");
            faceClient.PersonGroup.TrainAsync(personGroupId).GetAwaiter().GetResult();


            //Check train status
            TrainingStatus trainingStatus = null;
            while (true)
            {
                trainingStatus = faceClient.PersonGroup.GetTrainingStatusAsync(personGroupId).GetAwaiter().GetResult();

                if (trainingStatus.Status != TrainingStatusType.Running)
                {
                    break;
                }
                else {
                    Console.WriteLine("trainning person group...");
                }

                Thread.Sleep(1000);
            }


            foreach (var pic in picsPath) {

                Console.WriteLine("start identify faces in :" + pic);

                using (Stream s = File.OpenRead(pic))
                {
                    var faces = faceClient.Face.DetectWithStreamAsync(s).GetAwaiter().GetResult();
                    var faceIds = faces.Select(face => (Guid)face.FaceId).ToList();

                    var results = faceClient.Face.IdentifyAsync(faceIds, personGroupId).GetAwaiter().GetResult();
                    foreach (var identifyResult in results)
                    {
                        Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
                        if (identifyResult.Candidates.Count == 0)
                        {
                            Console.WriteLine("No one identified");
                        }
                        else
                        {
                            // Get top 1 among all candidates returned
                            var candidateId = identifyResult.Candidates[0].PersonId;
                            var person = faceClient.PersonGroupPerson.GetAsync(personGroupId, candidateId).GetAwaiter().GetResult();
                            Console.WriteLine("Identified as {0}", person.Name);
                        }
                    }

                }

            }
            Console.ReadKey();
        }
    }
}

我的照片:enter image description here

结果:

enter image description here

Btw,无论您使用哪种编程语言,只需按照本演示中的步骤操作,就能使用Face API来识别人脸。

希望它会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.