Exemplo n.º 1
0
        async Task LoadStatisticPercentages()
        {
            var listPersonAttributes = new List <PersonAttributes>();

            foreach (var person in Persons)
            {
                if (string.IsNullOrWhiteSpace(person.FaceImageUrl))
                {
                    continue;
                }

                var detectResults = await FaceApiService.DetectFaces(person.FaceImageUrl);

                DetectFaceResponse detectFaceResponse = detectResults.FirstOrDefault();

                if (detectFaceResponse == null || detectFaceResponse.FaceAttributes == null)
                {
                    continue;
                }

                listPersonAttributes.Add(new PersonAttributes
                {
                    Emotion = GetEmotion(detectFaceResponse.FaceAttributes?.Emotion),
                    Age     = detectFaceResponse.FaceAttributes.Age,
                    Gender  = detectFaceResponse.FaceAttributes.Gender
                });
            }
            if (listPersonAttributes != null)
            {
                GetMaxEmotionOnGroup(listPersonAttributes);
                GetMediumAgeOnGroup(listPersonAttributes);
                GetPercentageFemaleOnGroup(listPersonAttributes);
            }
        }
Exemplo n.º 2
0
        private async void IdentifyCheck_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Checking if it works...");

            var stream = await capturePhotoStreamAsync();

            if (stream != null)
            {
                var image = new BitmapImage();
                await image.SetSourceAsync(stream);

                photo.Source = image;

                var res = await FaceApiService.IdentifyPersonAsync(personGroupId, stream);

                viewModel.FaceRecognized = (currentPerson.personId == res);
                Debug.WriteLine($"Logged in person: {currentPerson.personId}, identified person: {res}");

                stream.Dispose();
            }
            else
            {
                viewModel.OxfordStatus = "Unable to capture photo.";
            }
        }
Exemplo n.º 3
0
        private async Task ResumeAfterPictureClarification(IDialogContext context, IAwaitable <IEnumerable <Attachment> > result)
        {
            await context.PostAsync(Response.CrowdInsights_PictureSent);

            try
            {
                var attachments = await result;
                var contentUrl  = attachments.First().ContentUrl;

                // Let the Cognitive Services to their work
                var detectFacesAndGenderTask = FaceApiService.DetectFacesAndGenderAsync(contentUrl);
                var visionTask = ComputerVisionService.DescribeAsync(contentUrl);
                await Task.WhenAll(detectFacesAndGenderTask, visionTask);

                var facesAndGender = await detectFacesAndGenderTask;
                var vision         = await visionTask;

                // Parse the result
                await context.PostAsync($"I think you're looking at _{vision.Text}_ , neat! I'm about _{Math.Floor(vision.Confidence * 100)}_% sure.");

                await context.PostAsync($"Your crowd consists of *{facesAndGender.Length}* people, from which *{facesAndGender.Where(x => x.FaceAttributes.Gender.Equals("male")).Count()}* are male and *{facesAndGender.Where(x => x.FaceAttributes.Gender.Equals("female")).Count()}* are female.");
            }
            catch (Exception ex)
            {
                await context.PostAsync($"ERROR: {ex.Message}");

                await context.PostAsync(Response.Error);
            }

            context.Done <object>(null);
        }
Exemplo n.º 4
0
 async Task OnDeletePerson(Person person)
 {
     await ExecuteAsync(async() =>
     {
         await FaceApiService.DeletePersonFromGroup(_personGroup.PersonGroupId, person.PersonId);
         Persons.Remove(person);
     });
 }
Exemplo n.º 5
0
 public override async Task InitAsync()
 {
     await ExecuteAsync(async() =>
     {
         FacePhoto = "face_default";
         _persons  = await FaceApiService.GetPersons();
     });
 }
Exemplo n.º 6
0
 public override async Task InitAsync()
 {
     await ExecuteAsync(async() =>
     {
         var persons = await FaceApiService.GetPersons();
         Persons     = new ObservableCollection <Person>(persons);
     });
 }
        public async Task <ActionResult> Upload(HttpPostedFileBase file)
        {
            if (file.ContentLength > 0)
            {
                var apiService = new FaceApiService("9444c937d213494facbea9a862cedf86");

                //byte[] resultImage = await apiService.UploadAndDetectFace(file);
                byte[] resultImageBytes;

                //Detect Faces
                using (var imageStream = new MemoryStream())
                {
                    file.InputStream.CopyTo(imageStream);
                    imageStream.Position = 0;
                    //Call DetectFaces method from Face API service file
                    var faces = await apiService.DetectFaces(imageStream);

                    var attr = new List <string>();
                    foreach (var face in faces)
                    {
                        attr.Add(face.FaceAttributes.Age.ToString());
                        attr.Add(face.FaceAttributes.FacialHair.Beard.ToString());
                        attr.Add(face.FaceAttributes.FacialHair.Moustache.ToString());
                        attr.Add(face.FaceAttributes.FacialHair.Sideburns.ToString());
                        attr.Add(face.FaceAttributes.Gender.ToString());
                        attr.Add(face.FaceAttributes.Glasses.ToString());
                        attr.Add(face.FaceAttributes.HeadPose.Pitch.ToString());
                        attr.Add(face.FaceAttributes.HeadPose.Roll.ToString());
                        attr.Add(face.FaceAttributes.HeadPose.Yaw.ToString());
                        attr.Add(face.FaceAttributes.Smile.ToString());
                    }

                    TempData["attributes"] = attr;
                    var facePositions = faces.Select(face => face.FaceRectangle);

                    // Draw rectangles over original image.
                    using (var img = apiService.DrawRectangles(imageStream, facePositions))
                    {
                        using (var ms = new MemoryStream())
                        {
                            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                            resultImageBytes = ms.ToArray();
                        }
                    }
                }
                CloudBlobContainer container = _blobStorageServices.GetCloudBlobContainer();
                CloudBlockBlob     blob      = container.GetBlockBlobReference(file.FileName);
                blob.UploadFromByteArray(resultImageBytes, 0, resultImageBytes.Count());

                TempData["resultImageBase64"] = GetImageBase64String(resultImageBytes);
                return(RedirectToAction("ViewFaces"));
            }

            return(RedirectToAction("Upload"));
        }
Exemplo n.º 8
0
        public override async Task InitAsync()
        {
            await ExecuteAsync(async() =>
            {
                HasToShowPicker = true;
                var persons     = await FaceApiService.GetPersons();

                Persons        = new ObservableCollection <Person>(persons);
                PersonSelected = persons?.FirstOrDefault();
            });
        }
Exemplo n.º 9
0
        private static void InitializeServices()
        {
            IAppConfiguration config = new AppConfiguration();

            ddbService = new DocumentDbService(config);

            faceApiService = new FaceApiService(ConfigurationManager.AppSettings["FaceApiSubscriptionKey"]);

            storageService =
                new StorageService(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString,
                                   "faces");
        }
Exemplo n.º 10
0
        public async Task <ActionResult> FileUpload(HttpPostedFileBase file)
        {
            if (file == null)
            {
                return(RedirectToAction("Upload"));
            }

            var apiService = new FaceApiService(ConfigurationManager.AppSettings["FaceApiKey"]);

            byte[] resultImage = await apiService.UploadAndDetectFace(file);

            TempData["resultImageBase64"] = GetImageBase64String(resultImage);
            return(RedirectToAction("ViewFaces"));
        }
Exemplo n.º 11
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            VisualStateManager.GoToState(this, "InitializingPhoto", false);

            viewModel = (MainPageViewModel)DataContext;

            RegisterOrientationEventHandlers();

            var token = App.Settings.Values["Token"].ToString();

            HttpClient hc = new HttpClient();

            hc.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            var result = await hc.GetAsync("https://graph.microsoft.com/v1.0/me");

            if (result.IsSuccessStatusCode)
            {
                string res = await result.Content.ReadAsStringAsync();

                OfficeUser user = JsonConvert.DeserializeObject <OfficeUser>(res);

                ((MainPageViewModel)DataContext).UserName = user.displayName;

                viewModel.OxfordStatus = "Checking if we know you...";

                bool userRegistered = await checkUserRegistrationAsync();

                if (!userRegistered)
                {
                    viewModel.OxfordStatus = "Looks like we haven't seen you yet. \nNo problem, take some pictures of your face. Five should be enough.";
                }
                else
                {
                    viewModel.OxfordStatus = "Welcome back!";

                    var ud = FaceApiService.DeserializeUserData(currentPerson.userData);
                    ud.AccessToken         = token;
                    currentPerson.userData = FaceApiService.SerializeUserData(ud);

                    await FaceApiService.UpdatePersonAsync(personGroupId, currentPerson);
                }
            }

            await InitializeCameraAsync();

            await CreateFaceDetectionEffectAsync();
        }
Exemplo n.º 12
0
        async Task OnAddPersonToGroup()
        {
            if (!(await IsFormValid()))
            {
                return;
            }
            var wantUserContinue = await Application.Current.MainPage.DisplayAlert(
                string.Empty,
                "Vamos a guardar tu foto y aparecerá en algunos apartados de la aplicación móvil de l@s demás usuari@s que se encuentran en la sala. ¡Sólo es para entretenernos un rato! Tendrás la opción de borrar la foto después si quieres y no quedará ni rastro de ella. Si tú no la borras, se borrará al final del día de hoy. Si aún así no te convence, pulsa Cancelar.",
                "Ok",
                "Cancelar");

            if (!wantUserContinue)
            {
                return;
            }


            await ExecuteAsync(async() =>
            {
                var photoBytes = GetByteArray(_photo);
                //save photo in blob storage
                var photoUrl = await BlobStorageService.SaveBlockBlob(photoBytes, PersonName);

                //add face to largeList
                var persistedFaceIdInLargeList = await FaceApiService.AddLargeListFace(photoUrl);

                //add person to group
                var person = await FaceApiService.AddPersonToGroup(new PersonGroupRequest()
                {
                    Name     = PersonName,
                    UserData = JsonConvert.SerializeObject(new UserDataPerson()
                    {
                        LargeListTWPersistFaceId = persistedFaceIdInLargeList.PersistedFaceId,
                        UrlFaceImage             = photoUrl
                    })
                },
                                                                   _personGroup.PersonGroupId);

                //add face to person
                var res = await FaceApiService.AddFaceToPerson(person.PersonId, _personGroup.PersonGroupId, photoBytes);

                //trainmodel
                await FaceApiService.TrainModel(_personGroup.PersonGroupId);

                await Navigation.PopAsync();
            });
        }
Exemplo n.º 13
0
        async Task OnAddImage()
        {
            await ExecuteAsync(async() =>
            {
                await CheckPermissionAndAskIfIsNeeded(Permission.Camera);
                await CheckPermissionAndAskIfIsNeeded(Permission.Storage);

                _photo = await GetFacePhotoTaked();
                if (_photo == null)
                {
                    return;
                }
                PhotoTaked       = _photo.Path;
                byte[] byteArray = GetByteArray(_photo);

                var detectResults = await FaceApiService.DetectFaces(byteArray);

                if (!(await IsPhotoValid(detectResults)))
                {
                    return;
                }

                var identifyResults = await FaceApiService.Identify(new IdentifyFaceRequest()
                {
                    FaceIds = detectResults.Select(x => x.FaceId).ToList(),
                    MaxNumOfCandidatesReturned = 10,
                    PersonGroupId       = _personGroup.PersonGroupId,
                    ConfidenceThreshold = 0.5
                });

                var persons = await FaceApiService.GetPersons();

                if (detectResults == null || identifyResults == null || persons == null)
                {
                    return;
                }

                FacesDetected = identifyResults.Select(res => new FaceDetectedItemViewModel()
                {
                    FaceId         = res.FaceId,
                    FaceRectangle  = detectResults.FirstOrDefault(detResults => detResults.FaceId == res.FaceId).FaceRectangle,
                    FaceAttributes = detectResults.FirstOrDefault(detResults => detResults.FaceId == res.FaceId).FaceAttributes,
                    PersonName     = persons?.FirstOrDefault(p => p.PersonId == res.Candidates?.FirstOrDefault(cand => cand.PersonId == p.PersonId)?.PersonId)?.Name
                }).ToList();
            });
        }
Exemplo n.º 14
0
        public override async Task InitAsync()
        {
            if (Groups != null)
            {
                return;
            }
            await ExecuteAsync(async() =>
            {
                var group = await FaceApiService.GetMyGroup();


                Groups = new ObservableCollection <PersonGroupResponse>
                {
                    group
                };
            });
        }
Exemplo n.º 15
0
        async Task <bool> IsValidPhoto()
        {
            var facesDetected = await FaceApiService.DetectFaces(GetByteArray(_photo));

            if (facesDetected == null || facesDetected.Count == 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Foto no válida",
                    "No hemos detectado ninguna cara en la foto que has tomado. Saca una foto en la que salga tu cara.",
                    "Ok");

                return(false);
            }
            else if (facesDetected.Count == 1)
            {
                var persons = await FaceApiService.GetPersons();

                foreach (var face in facesDetected)
                {
                    bool existsFaceId = persons.FirstOrDefault(person => person.PersistedFaceIds.Contains(face.FaceId)) != null;
                    if (existsFaceId)
                    {
                        await Application.Current.MainPage.DisplayAlert(
                            "Foto no válida",
                            $"En el grupo {_personGroup.Name} ya hay una persona creada asociada a la cara a la que le has sacado la foto.",
                            "Ok");

                        return(false);
                    }
                }
            }
            else
            {
                foreach (var face in facesDetected)
                {
                    await Application.Current.MainPage.DisplayAlert(
                        "Foto no válida",
                        "En la foto sólo puede haber una cara",
                        "Ok");

                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 16
0
        async Task GetSimilarFaces()
        {
            var detectSelectedPersonRes = await FaceApiService.DetectFaces(PersonSelected.FaceImageUrl);

            var similarFaces = await FaceApiService.GetSimilarFaces(detectSelectedPersonRes.FirstOrDefault().FaceId);

            List <Person> similarPersons = new List <Person>();

            foreach (var similarFace in similarFaces)
            {
                var person = Persons.FirstOrDefault(p => p.LargeListFaceId == similarFace.PersistedFaceId);
                if (person != null)
                {
                    person.ConfidenceSimilarity = similarFace.Confidence.ToString();
                    similarPersons.Add(person);
                }
            }

            SimilarPersons = new ObservableCollection <Person>(similarPersons);
        }
Exemplo n.º 17
0
        private async void TakePicture_Click(object sender, RoutedEventArgs e)
        {
            if (currentPerson == null)
            {
                Debug.WriteLine("Creating person in group.");

                var userData = new UserData(App.Settings.Values["userID"].ToString(), App.Settings.Values["Token"].ToString());
                var id       = await FaceApiService.CreatePersonInGroupAsync(personGroupId, viewModel.UserName, userData);

                Debug.WriteLine("Person created with ID: " + id);

                currentPerson = new OxfordPerson()
                {
                    personId = id,
                    name     = viewModel.UserName,
                    userData = JsonConvert.SerializeObject(userData).EncodeBase64()
                };
            }

            var stream = await capturePhotoStreamAsync();

            if (stream != null)
            {
                var image = new BitmapImage();
                await image.SetSourceAsync(stream);

                photo.Source = image;

                var faceId = await FaceApiService.AddPersonFaceAsync(personGroupId, currentPerson.personId, stream);

                Debug.WriteLine($"Face added to person {currentPerson.personId} with ID: {faceId}");

                stream.Dispose();
            }
            else
            {
                viewModel.OxfordStatus = "Unable to capture the photo.";
            }
        }
Exemplo n.º 18
0
        public AccountsController(OpgContext db,
                                  FaceApiService faceApiService,
                                  NameIdentity service,
                                  NameIdentityFromServiceGroup nameIdentityFromServiceGroup,
                                  NameRegisterFromServiceGroup nameRegisterFromServiceGroup,
                                  PersonGroupCreateFromServiceGroup personGroupCreateFromServiceGroup,
                                  PersonGroupTrainFromServiceGroup personGroupTrainFromServiceGroup,
                                  BlobClient blobClient,
                                  TableClient tableClient
                                  )
        {
            _opgContext     = db;
            _faceApiService = faceApiService;
            _nameIdentity   = service;

            _nameIdentityFromServiceGroup      = nameIdentityFromServiceGroup;
            _nameRegisterFromServiceGroup      = nameRegisterFromServiceGroup;
            _personGroupCreateFromServiceGroup = personGroupCreateFromServiceGroup;
            _personGroupTrainFromServiceGroup  = personGroupTrainFromServiceGroup;

            _blobClient  = blobClient;
            _tableClient = tableClient;
        }
Exemplo n.º 19
0
        private async Task <bool> checkUserRegistrationAsync()
        {
            var userId = App.Settings.Values["userID"].ToString();
            var people = await FaceApiService.GetPeopleInGroupAsync(personGroupId);

            if (people.Count == 0)
            {
                return(false); // no users yet
            }

            var res = (from p in people
                       where JsonConvert.DeserializeObject <UserData>(p.userData.DecodeBase64()).UserId == userId
                       select p).FirstOrDefault();

            if (res == null)
            {
                return(false); // user not registered
            }
            else
            {
                currentPerson = res;
                return(true);
            }
        }
Exemplo n.º 20
0
        private async Task ResumeAfterPictureClarification(IDialogContext context, IAwaitable <IEnumerable <Attachment> > result)
        {
            await context.PostAsync(Response.Introduction_PictureSent);

            try
            {
                var attachments    = await result;
                var faceAttributes = await FaceApiService.DetectFaceAttributesAsync(attachments.First().ContentUrl);

                // Parse the result
                var gender = faceAttributes.Gender;
                var age    = (int)Math.Round(faceAttributes.Age);

                await context.PostAsync($"Now that's one good looking *{gender}*! I'm guessing you're around *{age}* years of age?");
            }
            catch (Exception ex)
            {
                await context.PostAsync($"ERROR: {ex.Message}");

                await context.PostAsync(Response.Error);
            }

            await RestartAsync(context);
        }