public IActionResult RecordPost(int classID, int roomID, int time) { List <StudentWithImageViewModel> students = null; if (time != 0) { Record rec = _context.Records.Where(r => r.ID == time).First(); List <Record> records = _context.Records.Where(record => record.Time.Year == rec.Time.Year && record.Time.Month == rec.Time.Month && record.Time.Day == rec.Time.Day && record.Time.Hour == rec.Time.Hour && record.Time.Minute == rec.Time.Minute && record.Time.Second == rec.Time.Second).ToList(); students = new List <StudentWithImageViewModel>(); foreach (var record in records) { User student = _context.Users.Where(s => s.ID == record.UserID).First(); StudentWithImageViewModel studentRecord = new StudentWithImageViewModel(); studentRecord.Student = student; studentRecord.Image = record.Image; students.Add(studentRecord); } } string loggedEmail = HttpContext.Session.GetString("email"); User loggedUser = _context.Users.Where(student => student.Email == loggedEmail).First(); RecordViewModel viewData = new RecordViewModel(); viewData.Rooms = GetRooms(); viewData.Classes = GetClasses(loggedUser.ID); List <Record> allRecords = _context.Records.Where(record => record.ClassID == classID && record.RoomID == roomID).ToList(); List <Record> viewRecords = allRecords.GroupBy(x => x.Time.Ticks).Select(x => x.FirstOrDefault()).OrderByDescending(y => y.Time).ToList(); viewData.Records = viewRecords; ViewBag.Students = students; return(View(viewData)); }
/** * Recognizes students from sent image path */ public List <StudentWithImageViewModel> RecognizeStudents(string imagePath) { List <StudentWithImageViewModel> recognizedStudents = new List <StudentWithImageViewModel>(); // get student encodings List <UserImages> students = _context.UserImages.ToList(); List <FaceEncoding> knownStudents = new List <FaceEncoding>(); foreach (UserImages student in students) { var bf = new BinaryFormatter(); using (var ms2 = new MemoryStream(student.Encoding)) { var loaded = bf.Deserialize(ms2) as FaceEncoding; knownStudents.Add(loaded); } } IEnumerable <FaceEncoding> knownEncodings = knownStudents; // match known encodings with loaded ones using (FaceRecognition fr = FaceRecognition.Create(modelDirectory)) { using (FaceRecognitionDotNet.Image image = FaceRecognition.LoadImageFile(imagePath)) { IEnumerable <Location> locations = fr.FaceLocations(image); List <FaceEncoding> encodings = fr.FaceEncodings(image, locations).ToList(); for (int j = 0; j < encodings.Count(); j++) { List <bool> matches = FaceRecognition.CompareFaces(knownEncodings, encodings[j], tolerance).ToList(); if (matches.Contains(true)) { for (int i = 0; i < matches.Count(); i++) { if (matches[i]) { StudentWithImageViewModel student = new StudentWithImageViewModel(); Bitmap face = Crop(imagePath, locations.ToList()[j].Bottom, locations.ToList()[j].Left, locations.ToList()[j].Right, locations.ToList()[j].Top); ImageConverter converter = new ImageConverter(); student.Image = (byte[])converter.ConvertTo(face, typeof(byte[])); student.Student = _context.Users.Where(student => student.ID == students[i].UserID).First(); recognizedStudents.Add(student); break; } } } } } } return(recognizedStudents); }