Exemple #1
0
        public async Task <ActionResult> Emotions()
        {
            var personGroupId = Request["PersonGroupId"];
            var model         = new IdentifyFacesModel();

            var groups = await FaceClient.ListPersonGroupsAsync();

            model.PersonGroups = groups.Select(g => new SelectListItem
            {
                Value = g.PersonGroupId,
                Text  = g.Name
            }).ToList();

            if (Request.HttpMethod == "GET")
            {
                return(View(model));
            }

            Face[]           faces   = new Face[] { };
            Guid[]           faceIds = new Guid[] { };
            IdentifyResult[] results = new IdentifyResult[] { };

            await RunOperationOnImage(async stream =>
            {
                var emotionsType = new[] { FaceAttributeType.Emotion };
                faces            = await FaceClient.DetectAsync(stream, returnFaceAttributes: emotionsType);
                faceIds          = faces.Select(f => f.FaceId).ToArray();

                if (faceIds.Length > 0)
                {
                    results = await FaceClient.IdentifyAsync(personGroupId, faceIds);
                }
            });

            if (faceIds.Length == 0)
            {
                model.Error = "No faces detected";
                return(View(model));
            }

            foreach (var result in results)
            {
                var identifiedFace = new IdentifiedFace();
                identifiedFace.Face = faces.FirstOrDefault(f => f.FaceId == result.FaceId);

                foreach (var candidate in result.Candidates)
                {
                    var person = await FaceClient.GetPersonAsync(personGroupId, candidate.PersonId);

                    identifiedFace.PersonCandidates.Add(person.PersonId, person.Name);
                }

                model.IdentifiedFaces.Add(identifiedFace);
                identifiedFace.Color = Settings.ImageSquareColors[model.IdentifiedFaces.Count];
            }

            model.ImageDump = GetInlineImageWithIdentifiedFaces(model.IdentifiedFaces);

            return(View(model));
        }
Exemple #2
0
        async void DetectFaceAndEmotions()
        {
            var face = new FaceClient();

            this.Faces = await face.DetectAsync(new MemoryStream(this.Image) { Position = 0 });



            //var emotion = new EmotionClient();
            //this.EmotionFaces = await emotion.RecognizeAsync(new MemoryStream(this.Image) { Position = 0 });
        }
Exemple #3
0
        public async Task <ActionResult> Landmarks()
        {
            ViewBag.Title = "Face landmarks";

            if (Request.HttpMethod == "GET")
            {
                return(View("Index"));
            }

            var imageResult = "";
            var file        = Request.Files[0];

            Face[] faces = new Face[] { };

            await RunOperationOnImage(async stream =>
            {
                faces = await FaceClient.DetectAsync(stream, returnFaceLandmarks: true);
            });

            ImageToProcess.Seek(0, SeekOrigin.Begin);
            using (var img = new Bitmap(ImageToProcess))
                // make copy, drawing on indexed pixel format image is not supported
                using (var nonIndexedImg = new Bitmap(img.Width, img.Height))
                    using (var g = Graphics.FromImage(nonIndexedImg))
                        using (var mem = new MemoryStream())
                        {
                            g.DrawImage(img, 0, 0, img.Width, img.Height);

                            var pen = new Pen(Color.Red, 5);

                            foreach (var face in faces)
                            {
                                var props = typeof(FaceLandmarks).GetProperties();
                                foreach (var prop in props)
                                {
                                    if (prop.PropertyType == typeof(FeatureCoordinate))
                                    {
                                        var coordinate = (FeatureCoordinate)prop.GetValue(face.FaceLandmarks);
                                        var rect       = new Rectangle((int)coordinate.X, (int)coordinate.Y, 2, 2);
                                        g.DrawRectangle(pen, rect);
                                    }
                                }
                            }

                            nonIndexedImg.Save(mem, ImageFormat.Png);

                            var base64 = Convert.ToBase64String(mem.ToArray());
                            imageResult = String.Format("data:image/png;base64,{0}", base64);
                        }

            return(View("Index", (object)imageResult));
        }
    public static async Task <int> DetectOverMock()
    {
        var response = await s_socketMockService.DetectAsync(s_cancellation.Token, s_imageUrl);

        var status = response.Status;

        if (status != 200)
        {
            throw new Exception();
        }
        response.Dispose();
        return(status);
    }
Exemple #5
0
        public async Task DetectUrl()
        {
            if (NoAccountSettings())
            {
                return;
            }
            var cancellation = new CancellationTokenSource();

            var service  = new FaceClient(s_account, s_key);
            var response = await service.DetectAsync(cancellation.Token, new Uri(@"https://upload.wikimedia.org/wikipedia/commons/5/50/Albert_Einstein_%28Nobel%29.png"));

            Assert.AreEqual("male", response.Result.Gender);
            Assert.Greater(100, response.Result.Age);
            Assert.Less(10, response.Result.Age);

            response.Dispose();
        }
Exemple #6
0
        public async Task DetectLocalPath()
        {
            if (NoAccountSettings())
            {
                return;
            }
            var cancellation = new CancellationTokenSource();

            var service  = new FaceClient(s_account, s_key);
            var response = await service.DetectAsync(cancellation.Token, Path.Join("images", "face2.jpg"));

            Assert.AreEqual("male", response.Result.Gender);
            Assert.Greater(100, response.Result.Age);
            Assert.Less(10, response.Result.Age);

            response.Dispose();
        }
Exemple #7
0
        public async Task DetectResponseHeaders()
        {
            if (NoAccountSettings())
            {
                return;
            }
            var cancellation = new CancellationTokenSource();

            var service  = new FaceClient(s_account, s_key);
            var response = await service.DetectAsync(cancellation.Token, new Uri(@"https://upload.wikimedia.org/wikipedia/commons/5/50/Albert_Einstein_%28Nobel%29.png"));

            Assert.IsTrue(response.TryGetHeader("Content-Length", out var value));
            Assert.IsTrue(response.TryGetHeader("Content-Type", out var type));
            Assert.AreEqual("application/json; charset=utf-8", type);

            // TODO (pri 2): is this the best we can do?
            response.Dispose();
        }
Exemple #8
0
        public async Task <ActionResult> Index()
        {
            ViewBag.Title = "Detect faces";

            if (Request.HttpMethod == "GET")
            {
                return(View());
            }

            string imageResult = "";

            await RunOperationOnImage(async stream =>
            {
                var faces   = await FaceClient.DetectAsync(stream);
                imageResult = GetInlineImageWithFaces(faces);
            });

            return(View((object)imageResult));
        }
        public async Task <ActionResult> Identify()
        {
            var personGroupId = Request["PersonGroupId"];
            var model         = new IdentifyFacesModel();

            var groups = await FaceClient.ListPersonGroupsAsync();

            model.PersonGroups = groups.Select(g => new SelectListItem
            {
                Value = g.PersonGroupId,
                Text  = g.Name
            }).ToList();

            if (Request.HttpMethod == "GET")
            {
                return(View(model));
            }

            try
            {
                Face[]           faces   = new Face[] { };
                Guid[]           faceIds = new Guid[] { };
                IdentifyResult[] results = new IdentifyResult[] { };

                await RunOperationOnImage(async stream =>
                {
                    faces   = await FaceClient.DetectAsync(stream);
                    faceIds = faces.Select(f => f.FaceId).ToArray();

                    if (faceIds.Count() > 0)
                    {
                        results = await FaceClient.IdentifyAsync(personGroupId, faceIds);
                    }
                });

                if (faceIds.Length == 0)
                {
                    model.Error = "No faces detected";
                    //return View("ConfirmationPage", model);
                }

                foreach (var result in results)
                {
                    var identifiedFace = new IdentifiedFace();
                    identifiedFace.Face = faces.FirstOrDefault(f => f.FaceId == result.FaceId);

                    foreach (var candidate in result.Candidates)
                    {
                        await RunOperationOnImage(async stream =>
                        {
                            var person = await FaceClient.GetPersonAsync(personGroupId, candidate.PersonId);
                            identifiedFace.PersonCandidates.Add(person.PersonId, person.Name);
                        });

                        // var person = await FaceClient.GetPersonAsync(personGroupId, candidate.PersonId);
                        // identifiedFace.PersonCandidates.Add(person.PersonId, person.Name);
                    }

                    identifiedFace.Color = Settings.ImageSquareColors[model.IdentifiedFaces.Count];

                    model.IdentifiedFaces.Add(identifiedFace);
                }
            }
            catch {
                model.Error = "No faces detected, Please try again";
            }
            model.ImageDump = GetInlineImageWithFaces(model.IdentifiedFaces.Select(x => x.Face));
            TempData.Add("MyTempData", model);
            return(RedirectToAction("ConfirmationPage"));
        }
        public async Task <ActionResult> Attributes()
        {
            var personGroupId = Request["PersonGroupId"];
            var model         = new IdentifyFacesModel();

            var groups = await FaceClient.ListPersonGroupsAsync();

            model.PersonGroups = groups.Select(g => new SelectListItem
            {
                Value = g.PersonGroupId,
                Text  = g.Name
            }).ToList();
            model.PersonGroups.Insert(0, new SelectListItem {
                Text = "", Value = ""
            });

            if (Request.HttpMethod == "GET")
            {
                return(View(model));
            }

            Face[]           faces   = new Face[] { };
            Guid[]           faceIds = new Guid[] { };
            IdentifyResult[] results = new IdentifyResult[] { };
            var faceAttributeTypes   = new[] {
                FaceAttributeType.Accessories, FaceAttributeType.Age, FaceAttributeType.Blur,
                FaceAttributeType.Exposure, FaceAttributeType.FacialHair, FaceAttributeType.Gender,
                FaceAttributeType.Glasses, FaceAttributeType.Hair, FaceAttributeType.HeadPose,
                FaceAttributeType.Makeup, FaceAttributeType.Noise, FaceAttributeType.Occlusion,
                FaceAttributeType.Smile
            };

            await RunOperationOnImage(async stream =>
            {
                faces   = await FaceClient.DetectAsync(stream, returnFaceAttributes: faceAttributeTypes);
                faceIds = faces.Select(f => f.FaceId).ToArray();

                if (faceIds.Length > 0 && !string.IsNullOrEmpty(personGroupId))
                {
                    results = await FaceClient.IdentifyAsync(personGroupId, faceIds);
                }
            });

            if (faceIds.Length == 0)
            {
                model.Error = "No faces detected";
                return(View(model));
            }

            if (!string.IsNullOrEmpty(personGroupId))
            {
                foreach (var result in results)
                {
                    var identifiedFace = new IdentifiedFace();
                    identifiedFace.Face = faces.FirstOrDefault(f => f.FaceId == result.FaceId);

                    foreach (var candidate in result.Candidates)
                    {
                        var person = await FaceClient.GetPersonAsync(personGroupId, candidate.PersonId);

                        identifiedFace.PersonCandidates.Add(person.PersonId, person.Name);
                    }

                    identifiedFace.Color = Settings.ImageSquareColors[model.IdentifiedFaces.Count];
                    model.IdentifiedFaces.Add(identifiedFace);
                }
            }
            else
            {
                foreach (var face in faces)
                {
                    var identifiedFace = new IdentifiedFace {
                        Face = face
                    };
                    model.IdentifiedFaces.Add(identifiedFace);

                    identifiedFace.Color = Settings.ImageSquareColors[model.IdentifiedFaces.Count];
                }
            }

            model.ImageDump = GetInlineImageWithIdentifiedFaces(model.IdentifiedFaces);

            return(View(model));
        }