コード例 #1
0
ファイル: Program.cs プロジェクト: Kagigz/faceAPI-dotnetcore
        private static async Task IngestionPipeline()
        {
            Console.WriteLine("\n Ingestion Pipeline");

            Console.WriteLine("\n Enter new image url:");
            string img = Console.ReadLine();

            Console.WriteLine("\n Enter Large Face List ID:");
            string faceListID = Console.ReadLine();

            try
            {
                List <Face> detectedFaces = await FaceAPIHelpers.DetectFaces(img, true);

                foreach (Face f in detectedFaces)
                {
                    try
                    {
                        PersistedFace persistedFace = await FaceAPIHelpers.AddFace(img, faceListID, f.faceRectangle);

                        Console.WriteLine("\n Face " + persistedFace.persistedFaceId + " added.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"\n{e.GetType().Name}: Could not add face to Large Face List.");
                        throw (e);
                    }
                }

                try
                {
                    await FaceAPIHelpers.TrainFaceList(faceListID);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\n{e.GetType().Name}: Could not train Large Face List.");
                    throw (e);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"\n{e.GetType().Name}: Could not detect faces.");
                throw (e);
            }

            AskUser();
        }
コード例 #2
0
        /// <summary>
        /// LargeFaceList - Add Face API: adds a face to a Large Face List
        /// If several faces are in the given image, the target rectangle must be provided
        /// </summary>
        public static async Task <PersistedFace> AddFace(string img, string faceListID, Rectangle target = null, string userData = "")
        {
            Console.WriteLine("\n Adding face to Large Face List...");

            string uri = "https://westeurope.api.cognitive.microsoft.com/face/v1.0/largefacelists/" + faceListID + "/persistedfaces";

            if (userData != "" || target != null)
            {
                uri += "?";
            }
            if (userData != "")
            {
                uri += "userData=";
                uri += userData;
            }
            if (target != null)
            {
                if (userData != "")
                {
                    uri += "&";
                }
                uri += "targetFace=";
                uri += target.left;
                uri += ",";
                uri += target.top;
                uri += ",";
                uri += target.width;
                uri += ",";
                uri += target.height;
            }

            string        response = null;
            PersistedFace result   = null;

            try
            {
                response = await RequestHelpers.PostRequest(uri, new JsonContent(new
                {
                    url = img
                }));

                Console.WriteLine($"\nFace added to Large Face List.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"\n{e.GetType().Name}: Error adding face to Large Face List.");
                throw;
            }

            if (response != null)
            {
                try
                {
                    result = JsonConvert.DeserializeObject <PersistedFace>(response);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\n{e.GetType().Name}: Error parsing response after adding face to Large Face List.");
                    throw;
                }
            }

            return(result);
        }