/* * Functions: * 1. Add Images * 2. Save to Database * 3. Train * 4.Predict */ public bool AddImagesToDb(Image <Gray, byte>[] images, string FirstName, string LastName) { if (!images.All(_ => _.Size.Width == 240 && _.Size.Height == 180)) { return(false); } var user = _context.Users.FirstOrDefault(_ => _.FirstName == FirstName && _.LastName == LastName); if (user == null) { return(false); } string path = Application.StartupPath + @"/../../Images/" + user.FirstName + "-" + user.LastName + "/"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } int totalLabels = 0; if (_context.Labels.Count() != 0) { totalLabels = _context.Labels.Max(_ => _.LabelNumber); } foreach (var image in images) { string imagePath = path + string.Concat(user.FirstName, "-", user.LastName, "-", "image", "-", (totalLabels + 1).ToString(), ".jpg"); DAL.Models.Label label = new DAL.Models.Label { LabelNumber = totalLabels + 1, UserId = user.Id }; _context.Labels.Add(label); _context.SaveChanges(); image.Save(imagePath); ++totalLabels; } _context.Update(user); _context.SaveChanges(); return(true); }
public string Predict(Image <Gray, byte> image) { faceRecognizer.Read(Application.StartupPath + @"/../../Images/faceRecognizer.yml"); var res = faceRecognizer.Predict(image); DAL.Models.Label label = null; //if(res.Distance > threshold) if (res.Distance < 3500) { label = _context.Labels.FirstOrDefault(_ => _.LabelNumber == res.Label); var user = _context.Users.FirstOrDefault(_ => _.Id == label.UserId); Log.Logger.Information("{@VisitDate} User{@username} with UserId {@UserId} {@VisitType} home and has access to House", DateTime.Now, user.FirstName + " " + user.LastName, user.Id); Log.CloseAndFlush(); return(user.FirstName + " " + user.LastName); } else { Log.Logger.Information("{@VisitDate} User{@username} with UserId {@UserId} {@VisitType} home and has access to House", DateTime.Now); } return(string.Empty); }