public string GetRecommendedUri(FaceViewModel face) { if (face != null && face.Age != null && face.Gender != null) { if (face.Age >= 0 && face.Age < 17) { return("http://www.microsoftstore.com/store/msusa/en_US/pdp/Xbox-One-+-Kinect-Bundle/productID.330348300"); } else if (face.Age >= 15 && face.Age < 23 && face.Gender == "female") { return("http://www.microsoftstore.com/store/msusa/en_US/pdp/Office-365-University/productID.275549300"); } else if (face.Age >= 15 && face.Age < 23 && face.Gender == "male") { return("http://www.microsoftstore.com/store/msusa/en_US/pdp/productID.324438600"); } else if (face.Age >= 23 && face.Age < 30) { return("http://www.microsoftstore.com/store/msusa/en_US/pdp/Microsoft-Lumia-950--Unlocked/productID.326602600"); } else if (face.Age >= 30 && face.Age < 40) { return("http://www.microsoftstore.com/store/msusa/en_US/pdp/productID.325711500"); } else if (face.Age >= 40) { return("http://www.microsoftstore.com/store/msusa/en_US/pdp/productID.325716000"); } } return("http://www.microsoftstore.com/store/msusa/en_US/cat/All-PCs-tablets/categoryID.69404700"); }
private async Task AddPersonFace(int idPerson) { foreach (string personFaceFilePath in Directory.EnumerateFiles( $"Img/Faces/{idPerson}", "*", SearchOption.AllDirectories) ) { using (var sc = new StreamContent(File.OpenRead(personFaceFilePath))) using (var ms = new MemoryStream()) { await sc.CopyToAsync(ms); var faceViewModel = new FaceViewModel() { IdPersons = idPerson, Image = ms.ToArray() }; using (var postContent = new StringContent(JsonConvert.SerializeObject(faceViewModel), Encoding.UTF8, "application/json")) using (var response = await Environment.ServerApiSecurity .CreateRequest("Person/AddPersonFaceAsync") .AddHeader("Authorization", "Bearer " + _login.access_token) .And(request => request.Content = postContent) .And(request => request.Method = HttpMethod.Post) .PostAsync()) { response.EnsureSuccessStatusCode(); } } } }
public async Task <IActionResult> DetectEmotion([FromBody] FaceViewModel model) { var faceAttributes = await _faceService.DetectFaceAttributesAsync(model.ImageData, FaceAttributeType.Emotion); return(Ok(faceAttributes?.Emotion)); }
public async Task PutFace(int id, FaceViewModel faceViewModel, int personId) { if (id != faceViewModel.Id) { throw new BadRequestException("Данные запроса не совпадают"); } var face = await _context.Faces.Where(f => f.Id == id).SingleOrDefaultAsync(); if (face == null) { throw new NotFoundException("Лица с такими параметрами не найдено"); } if (personId != face.PersonId) { throw new BadRequestException("Данные запроса не совпадают"); } face.PersonId = faceViewModel.PersonId; int photoHash = Hasher.CreateHash(faceViewModel.Photo); string oldName = null; // Если фото поменялось if (photoHash != face.PhotoHash) { oldName = face.PhotoName; face.PhotoName = DateTime.UtcNow.Ticks.ToString(); face.PhotoHash = photoHash; } _context.Entry(face).State = EntityState.Modified; try { await _context.SaveChangesAsync(); if (oldName != null) { await _googleStorageService.Delete(oldName); await _googleStorageService.Write(faceViewModel.Photo, face.PhotoName); } } catch (DbUpdateConcurrencyException) { if (!FaceExists(id)) { throw new NotFoundException("Лица с такими параметрами не найдено"); } else { throw; } } }
public ActionResult Add(int?personId = null, int?groupId = null) { var faceViewModel = new FaceViewModel() { PersonList = FillPersonList(personId) }; return(View(faceViewModel)); }
public ImagePage() { InitializeComponent(); BindingContext = _vm = new FaceViewModel(); MessagingCenter.Subscribe<FaceViewModel>(this, "", async (e) => { await DisplayAlert("Analyzing image", "Your image is being analyzed", "Ok"); }); }
private static Face MapToModel(FaceViewModel faceViewModel, string path) { return(new Face() { Id = faceViewModel.Id, PersonId = faceViewModel.PersonId, PhotoHash = Hasher.CreateHash(faceViewModel.Photo), PhotoName = path }); }
static void DrawPredictions(FaceViewModel vm, SKCanvas canvas, float left, float top, float scaleWidth, float scaleHeight, SKImageInfo info) { if (vm.FaceInfo == null) { return; } if (vm.FaceInfo.FaceRectangle != null) { LabelPrediction(canvas, vm.FaceInfo.MaxEmotion, vm.FaceInfo.FaceRectangle, left, top, scaleWidth, scaleHeight, vm, info); } }
public void AddFaceHistory(string Url, FaceViewModel faceViewModel) { Execute(@"insert into FaceHistory(Account,Url,FaceColor,Product_Id,FaceDate) values(@account,@url,@faceColor,@product_Id,@faceDate)", new { account = faceViewModel.Account, url = Url, faceColor = faceViewModel.FaceColor, product_id = faceViewModel.Product_Id, faceDate = DateTime.Now }); }
public async Task <IActionResult> DetectAge([FromBody] FaceViewModel model) { var faceAttributes = await _faceService.DetectFaceAttributesAsync(model.ImageData, FaceAttributeType.Age); var nowInTurkey = DateTime.UtcNow.AddHours(3); var curfewResult = new CurfewResult { Age = faceAttributes?.Age }; var curfewRequest = new CurfewRequest(curfewResult.Age ?? 0, nowInTurkey.DayOfWeek, nowInTurkey.Hour); curfewResult.IsFree = _curfewService.IsFreeToGoOut(curfewRequest); return(Ok(curfewResult)); }
public async Task <ActionResult> Add(FaceViewModel faceViewModel) { try { if (ModelState.IsValid) { await faceManager.AddAsync(faceViewModel.ToEntity()); return(new HttpStatusCodeResult(HttpStatusCode.Created)); } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Complete all the mandatory fields, please")); } catch (BusinessException ex) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, ex.Message)); } }
public async Task <FaceViewModel> PostFace(FaceViewModel faceViewModel, int personId) { if (personId != faceViewModel.PersonId) { throw new BadRequestException("Данные запроса не совпадают"); } string path = DateTime.UtcNow.Ticks.ToString(); await _googleStorageService.Write(faceViewModel.Photo, path); var face = MapToModel(faceViewModel, path); _context.Faces.Add(face); await _context.SaveChangesAsync(); faceViewModel.Id = face.Id; return(faceViewModel); }
public async Task <FaceViewModel> GetFace(int id, int personId) { var face = await _context.Faces.Where(f => f.PersonId == personId && f.Id == id).SingleOrDefaultAsync(); if (face == null) { throw new NotFoundException("Лица с такими параметрами не найдено"); } // Получение фото из облака var photo = await _googleStorageService.Read(face.PhotoName); var faceViewModel = new FaceViewModel() { Id = face.Id, PersonId = personId, Photo = photo }; return(faceViewModel); }
public async Task <ActionResult <FaceViewModel> > Face([FromForm] FaceAnalyzeRequest request) { var errorContent = ""; if (string.IsNullOrWhiteSpace(request.FaceSubscriptionKey)) { errorContent += $"Missing or invalid Face Subscription Key (see 'Azure Settings' tab){Environment.NewLine}"; } if (string.IsNullOrWhiteSpace(request.FaceEndpoint)) { errorContent += $"Missing or invalid Face Endpoint (see 'Azure Settings' tab){Environment.NewLine}"; } if (string.IsNullOrWhiteSpace(request.ImageUrl) && (request.File == null || !_allowedFileContentType.Contains(request.File.ContentType))) { errorContent += $"Missing or invalid ImageUrl / no file provided{Environment.NewLine}"; } if (request.EnableIdentification && string.IsNullOrWhiteSpace(request.IdentificationGroupId)) { errorContent += $"Missing or invalid Identification Group Id{Environment.NewLine}"; } if (!string.IsNullOrWhiteSpace(errorContent)) { return(View(FaceViewModel.Analyzed(request, new FaceAnalyzeResponse { OtherErrorMessage = "Request not processed due to the following error(s):", OtherErrorContent = errorContent }))); } Track("Vision_Face"); var imageAnalyzer = new ImageFaceAnalyzer(request.FaceSubscriptionKey, request.FaceEndpoint, _httpClientFactory); var analyzeResult = await imageAnalyzer.AnalyzeAsync(request.ImageUrl, request.File, request.DetectionModel, request.EnableIdentification, request.RecognitionModel, request.IdentificationGroupType, request.IdentificationGroupId); return(View(FaceViewModel.Analyzed(request, analyzeResult))); }
public async Task <IActionResult> Index(List <IFormFile> files) { var file = files.FirstOrDefault(); var filePath = Path.GetTempFileName(); if (file.Length > 0) { using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream).ConfigureAwait(false); stream.Flush(); stream.Position = 0; var faceClient = new FaceClient(new ApiKeyServiceClientCredentials(config.Value.Face.ApiKey)) { Endpoint = config.Value.Face.BaseUri }; var result = await faceClient.Face.DetectWithStreamAsync(stream, true, true, new[] { FaceAttributeType.Age, FaceAttributeType.Emotion, FaceAttributeType.FacialHair, FaceAttributeType.Gender, FaceAttributeType.Glasses, FaceAttributeType.Hair, FaceAttributeType.Smile }).ConfigureAwait(false); var model = new FaceViewModel { Faces = result, FileName = file.FileName, Image = System.IO.File.ReadAllBytes(filePath) }; return(View(model)); } } return(View(new FaceViewModel())); }
public async Task <ActionResult> Face(string img) { img = string.IsNullOrWhiteSpace(img) ? "/images/face/5.jpg" : img; string subscriptionKey = ConfigurationManager.AppSettings["FaceApiKey"]; string imageUrl = $"{_baseUrl}{img}"; // Using the SDK var faceService = new FaceServiceClient(subscriptionKey); var result = await faceService.DetectAsync(imageUrl, true, true, new[] { FaceAttributeType.Age, FaceAttributeType.FacialHair, FaceAttributeType.Gender, FaceAttributeType.Glasses, FaceAttributeType.Smile }); // Using the WebApi var viewmodel = new FaceViewModel { SdkResult = result, ImageUrl = imageUrl, }; return(View(viewmodel)); }
public async Task <FaceViewModel> SkinDetection() { var jwtObject = GetjwtToken(); var HttpRequest = HttpContext.Current.Request; FaceViewModel faceViewModel = new FaceViewModel(); if (HttpRequest.Files.Count > 0) { foreach (string name in HttpRequest.Files.Keys) { var file = HttpRequest.Files[name]; if ((!file.ContentType.Equals("image/jpg")) && (!file.ContentType.Equals("image/png")) && (!file.ContentType.Equals("image/jpeg"))) { //result = "檔案格式請選取jpg,png"; } else { string filename = jwtObject["Account"].ToString() + "_face_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"; string Url = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/FacePic/"), filename); file.SaveAs(Url); faceViewModel.Account = jwtObject["Account"].ToString(); faceViewModel.FaceUrl = "/FacePic/" + Path.GetFileNameWithoutExtension(Url) + Path.GetExtension(Url); var skin = await _faceService.GetSkin("http://localhost:58694/FacePic/" + Path.GetFileNameWithoutExtension(Url) + Path.GetExtension(Url)); string[] result = skin.Split(';'); faceViewModel.FaceColor = result[0]; var product = new ProductRepo().GetProductByTicket(result[1]); faceViewModel.Product_Id = product.Product_Id; faceViewModel.Brand = product.Brand; faceViewModel.Name = product.Name; faceViewModel.Color = product.Color; faceViewModel.ProductUrl = "/ProductPic/" + Path.GetFileNameWithoutExtension(product.Url) + Path.GetExtension(product.Url); faceViewModel.FaceDate = DateTime.Now; new FaceRepo().AddFaceHistory(Url, faceViewModel); } } } return(faceViewModel); }
//[Authorize(Policy = "Person.Add")] public async Task <IActionResult> AddPersonFaceAsync([FromBody] FaceViewModel viewModel) { if (!IsModelStateValid()) { return(Response()); } var person = await _personRepository.GetByIdAsync(viewModel.IdPersons); if (person == null) { NotifyError("", "Person not found"); return(Response()); } var faceId = await _personAdapter.AddPersonFaceAsync(person, viewModel.Image); if (faceId == null) { NotifyError("", "Could not add Person Face on Adapter"); return(Response()); } var userLoggedInDB = await GetUserDbByIdentityAsync(HttpContext); if (userLoggedInDB == null) { return(Response()); } var userLoggedIn = Mapper.Map <UserViewModel, User>(userLoggedInDB); var domainModel = Mapper.Map <FaceModel>(viewModel); domainModel.FaceId = new Guid(faceId); var cmd = new AddPersonFaceCommand(userLoggedIn, domainModel); await _mediator.SendCommand(cmd); return(Response(cmd)); }
private void BindFace(UniformGrid gridFace, FaceViewModel face) { foreach (var row in face.Rows) { foreach (var facelet in row.Facelets) { var faceletColorBinding = new Binding("CurrentColor") { Source = facelet, Mode = BindingMode.OneWay, Converter = new FaceletConverter() }; var faceletRectangle = new Rectangle() { Stroke = new SolidColorBrush(Colors.Black) }; faceletRectangle.SetBinding(Shape.FillProperty, faceletColorBinding); gridFace.Children.Add(faceletRectangle); } } }
public async Task <IActionResult> PutFace(int id, FaceViewModel faceViewModel, int personId) { await _faceService.PutFace(id, faceViewModel, personId); return(NoContent()); }
private void ThreadWork() { string imagePath; FaceRecognition faceRecognition = null; while (true) { lock (_searchStackLocker) { if (_searchQueue.Count > 0) { imagePath = _searchQueue.Dequeue(); } else { break; } } _progress.Report(new ProgressPartialResult() { Current = _current, Total = _progressMaximum, Text = imagePath }); lock (_progressLocker) { _current++; } FaceEncodingInfo founded; lock (_dbLocker) { founded = _db.GetFromDB(imagePath); } if (founded == null) { if (faceRecognition == null) { faceRecognition = FaceRecognition.Create(_configuration.ModelsDirectory); } FaceRecognitionDotNet.Image image; try { image = FaceRecognition.LoadImageFile(imagePath); } catch (Exception ex) { MessageBox.Show($"{ex.Message} \n {ex.StackTrace} \n {ex?.InnerException?.Message}", "Exception on LoadImageFile", MessageBoxButton.OK, MessageBoxImage.Error); continue; } using (image) { Debug.WriteLine($"Train on {imagePath}"); //find face locations var faceBoundingBoxes = faceRecognition.FaceLocations(image, 1, Model.Hog); var countOfFace = faceBoundingBoxes.Count(); if (countOfFace == 0) { Application.Current.Dispatcher.Invoke(() => { FaceViewModel vm = new FaceViewModel(imagePath); _windowService.ShowDialogWindow <FaceWindow>(vm); }); continue; //throw new Exception($"Not founded face in {imageFile}"); } if (countOfFace > 1) { Application.Current.Dispatcher.Invoke(() => { FaceViewModel vm = new FaceViewModel(faceBoundingBoxes, imagePath); _windowService.ShowDialogWindow <FaceWindow>(vm); }); continue; //If there are no people (or too many people) in a training image, skip the image. //throw new Exception($"Faces {countOfFace} > 1 in {imageFile}"); } else { // Add face encoding for current image to the training set var encodings = faceRecognition.FaceEncodings(image, faceBoundingBoxes); if (encodings == null) { continue; } foreach (var encoding in encodings) { var info = new SerializationInfo(typeof(double), _formatterConverter); encoding.GetObjectData(info, _context); double[] doubleInfo = (double[])info.GetValue("_Encoding", typeof(double[])); encoding.Dispose(); var dir = Path.GetDirectoryName(imagePath); string directory = new DirectoryInfo(dir).Name; lock (_trainedInfoLocker) { _trainedInfo.Add(new ClassInfo(directory, doubleInfo, imagePath)); } lock (_dbLocker) { _db.AddFaceInfo(imagePath, doubleInfo, faceBoundingBoxes.Single().Left, faceBoundingBoxes.Single().Right, faceBoundingBoxes.Single().Top, faceBoundingBoxes.Single().Bottom); } } } } } else { Debug.WriteLine($"File {imagePath} in db"); var dir = Path.GetDirectoryName(imagePath); string directory = new DirectoryInfo(dir).Name; lock (_trainedInfoLocker) { var fingerAndLocation = founded.FingerAndLocations.Single(); _trainedInfo.Add(new ClassInfo(directory, fingerAndLocation.FingerPrint, imagePath)); } } } if (faceRecognition != null) { faceRecognition.Dispose(); } }
public FaceView(FaceViewModel viewModel) { InitializeComponent(); DataContext = viewModel; }
static void LabelPrediction(SKCanvas canvas, string tag, FaceRectangle rectangle, float left, float top, float width, float height, FaceViewModel vm, SKImageInfo info, bool addBox = true) { var scale = Math.Min((float)info.Width / (float)vm.Image.Width, (float)info.Height / (float)vm.Image.Height); var scaleHeight = rectangle.Height * scale; var scaleWidth = rectangle.Width * scale; var scaleTop = rectangle.Top * scale; var scaleLeft = rectangle.Left * scale; if (addBox) { DrawBox(canvas, (float)scaleLeft, (float)scaleTop, (float)scaleWidth, (float)scaleHeight); } DrawText(canvas, tag, left, top, width, height); }
private async Task UploadFace(byte[] faceBytes, FaceViewModel face) { var blob = await _storageService.UploadStream(faceBytes, StorageContainer.Faces, $"{face.FaceId}.jpeg"); face.Url = blob.Uri; }
public async Task <ActionResult <FaceViewModel> > PostFace([FromBody] FaceViewModel faceViewModel, int personId) { var face = await _faceService.PostFace(faceViewModel, personId); return(face); }
public IActionResult Face() { return(View(FaceViewModel.NotAnalyzed())); }
public FaceViewModel Random() { var jwtObject = GetjwtToken(); var HttpRequest = HttpContext.Current.Request; FaceViewModel faceViewModel = new FaceViewModel(); if (HttpRequest.Files.Count > 0) { foreach (string name in HttpRequest.Files.Keys) { var file = HttpRequest.Files[name]; if ((!file.ContentType.Equals("image/jpg")) && (!file.ContentType.Equals("image/png")) && (!file.ContentType.Equals("image/jpeg"))) { //result = "檔案格式請選取jpg,png"; } else { string filename = jwtObject["Account"].ToString() + "_face_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"; string Url = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/FacePic/"), filename); file.SaveAs(Url); Dictionary <string, int> skinColor = new Dictionary <string, int>(); skinColor.Add("#fce0d5", 10); skinColor.Add("#fcdbbd", 38); skinColor.Add("#fddbb8", 23); skinColor.Add("#ebd4ae", 44); skinColor.Add("#e6c7a1", 28); skinColor.Add("#e3bf91", 3); skinColor.Add("#efd0bb", 7); skinColor.Add("#dbb88c", 2); skinColor.Add("#DABA93", 33); skinColor.Add("#DCB8A5", 37); skinColor.Add("#edd2a4", 5); skinColor.Add("#F8D0B3", 37); skinColor.Add("#edd4b7", 26); skinColor.Add("#f2cdb6", 31); skinColor.Add("#f0c999", 48); skinColor.Add("#ffc0a3", 40); skinColor.Add("#e0b7a6", 27); skinColor.Add("#DFC0AB", 15); skinColor.Add("#e6ba9a", 22); skinColor.Add("#f2c7a5", 46); Random random = new Random(); int r = random.Next(0, 10); int i = 0; string ticket = ""; int productid = 1; foreach (var sk in skinColor) { if (i == r) { ticket = sk.Key; productid = sk.Value; } i++; } var product = new ProductRepo().GetProductByRandom(productid); faceViewModel.Account = jwtObject["Account"].ToString(); faceViewModel.Account = jwtObject["Account"].ToString(); faceViewModel.FaceUrl = "/FacePic/" + Path.GetFileNameWithoutExtension(Url) + Path.GetExtension(Url); faceViewModel.FaceColor = ticket; faceViewModel.Product_Id = product.Product_Id; faceViewModel.Brand = product.Brand; faceViewModel.Name = product.Name; faceViewModel.Color = product.Color; faceViewModel.ProductUrl = "/ProductPic/" + Path.GetFileNameWithoutExtension(product.Url) + Path.GetExtension(product.Url); faceViewModel.FaceDate = DateTime.Now; new FaceRepo().AddFaceHistory(Url, faceViewModel); } } } return(faceViewModel); }