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); } }
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")); }
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); }
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(); }); }
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); }