public ApiResult <RollCallReponseModel> RollCallByImageUpload()
        {
            var           r             = new ApiResult <RollCallReponseModel>();
            RollCallModel rollCallModel = new RollCallModel();
            var           httpRequest   = System.Web.HttpContext.Current.Request;

            if (httpRequest.Files.Count < 1)
            {
                r.Messages.Add("Vui lòng upload ảnh");
            }
            else if (httpRequest.Files.Count > 1)
            {
                r.Messages.Add("Vui lòng chỉ uplaod 1 ảnh duy nhât");
            }
            else
            {
                HttpPostedFile file = httpRequest.Files[0];
                byte[]         thePictureAsBytes = new byte[file.ContentLength];
                using (BinaryReader theReader = new BinaryReader(file.InputStream))
                {
                    thePictureAsBytes = theReader.ReadBytes(file.ContentLength);
                }
                rollCallModel.ClassScheduleId = Int32.Parse(httpRequest.Files.AllKeys[0]);
                rollCallModel.Base64Image     = Convert.ToBase64String(thePictureAsBytes);
                r = RCSService.RollCall(rollCallModel);
            }
            return(r);
        }
 public ApiResult <RollCallReponseModel> RollCall(RollCallModel model)
 {
     return(RCSService.RollCall(model));
 }
Пример #3
0
        public ApiResult <RollCallReponseModel> RollCall(RollCallModel model)
        {
            var r = new ApiResult <RollCallReponseModel>();

            r.Data             = new RollCallReponseModel();
            r.Data.Base64Image = model.Base64Image;
            var rollCallData  = new ApiResult <List <RollCall> >();
            var classSchedule = RCSContext.ClassSchedules.Where(cs => cs.Id == model.ClassScheduleId).FirstOrDefault();

            //check thông tin lớp học
            if (classSchedule == null)
            {
                r.Messages.Add("Lớp học không tồn tại.");
                return(r);
            }
            if (classSchedule.Status == (int)EClassStatus.Schedule)
            {
                r.Messages.Add("Lớp học chưa mở.");
                return(r);
            }
            if (classSchedule.Status == (int)EClassStatus.Closed)
            {
                r.Messages.Add("Lớp học đã kết thúc");
                return(r);
            }
            var imageGray      = model.Base64Image.base64ToImageGray();
            var image          = model.Base64Image.base64ToImage();
            var bitmap         = new Bitmap(image);
            var bitmapResponse = new Bitmap(bitmap);

            imageGray._EqualizeHist();
            var rectangles = DetectService.DetectFaceRollCall(imageGray);

            if (rectangles.Count() == 0)
            {
                r.Messages.Add("Không có sinh viên nào trong ảnh.");
                return(r);
            }
            var classId      = RCSContext.ClassSchedules.Where(cs => cs.Id == model.ClassScheduleId).FirstOrDefault().ClassId;
            var studyings    = RCSContext.Studyings.Where(st => st.ClassId == classId).ToList();
            var newRollCalls = new List <RollCall>();
            var oldRollCalls = new List <RollCall>();

            //Detect khuôn mặt
            rectangles.ForEach(rec =>
            {
                var face      = bitmap.cropAtRect(rec);
                var faceGray  = new Image <Gray, byte>(face);
                var studentId = DetectService.RecognizeFace(faceGray);
                if (studentId == -1)
                {
                    var faceGrayFlip = faceGray.Flip(FlipType.Horizontal);
                    studentId        = DetectService.RecognizeFace(faceGrayFlip);
                }
                if (studentId != -1)
                {
                    if (studyings.Any(st => st.StudentId == studentId))
                    {
                        bitmapResponse = drawFaceAndNoteOnBitmap(bitmapResponse, rec, studentId.ToString(), Color.Green);
                        var rc         = RCSContext.RollCalls.Where(i => i.StudentId == studentId && i.ClassScheduleId == model.ClassScheduleId).FirstOrDefault();
                        var fileData   = new FileData
                        {
                            Data = face.bitmapToByteArr(),
                        };
                        var fileAttachment = new FileAttachment
                        {
                            CreateDate = DateTime.Now,
                            Height     = face.Height,
                            Width      = face.Width,
                            Extension  = "jpg",
                            Type       = (int)EFileType.ImageJPG,
                            Name       = Guid.NewGuid().ToString(),
                            FileData   = fileData
                        };
                        if (rc == null)
                        {
                            var rollCall = new RollCall();
                            rollCall     = new RollCall
                            {
                                ClassScheduleId = model.ClassScheduleId,
                                StudentId       = studentId,
                                CreatedDate     = DateTime.Now,
                                Type            = (int)ERollCallType.Auto,
                                IsActive        = true
                            };
                            newRollCalls.Add(rollCall);
                        }
                        else
                        {
                            rc.IsActive       = true;
                            rc.CreatedDate    = DateTime.Now;
                            rc.FileAttachment = fileAttachment;
                            oldRollCalls.Add(rc);
                        }
                    }
                }
                else
                {
                    bitmapResponse = drawFaceAndNoteOnBitmap(bitmapResponse, rec, "unknown", Color.Red);
                }
            });
            if (newRollCalls.Count == 0 && oldRollCalls.Count == 0)
            {
                r.Messages.Add("Các sinh viên không thuộc lớp này.");
                r.Data.Base64Image = Convert.ToBase64String(bitmapResponse.bitmapToByteArr());
                return(r);
            }
            RCSContext.RollCalls.AddRange(newRollCalls);
            try
            {
                RCSContext.SaveChanges();
            }
            catch (Exception e) { r.Messages.Add(e.Message); return(r); }
            newRollCalls.AddRange(oldRollCalls);
            newRollCalls.ForEach(i =>
            {
                i.ClassSchedule.RollCalls = null;
                i.FileAttachment          = null;
                i.Student = null;
                i.Image   = null;
            });
            r.Messages.Add("Điểm danh thành công.");
            r.IsSuccess        = true;
            r.Data.Base64Image = Convert.ToBase64String(bitmapResponse.bitmapToByteArr());
            r.Data.RollCalls   = newRollCalls;
            return(r);
        }