Exemple #1
0
 /// <summary>
 /// 抓取影像Thread
 /// </summary>
 private void ThreadFunc()
 {
     while (!_faceService.IsFaceDetected() || _view.IsContinued())
     {
         if (!_view.EnableByCam())
         {
             using (var imageFrame = _capture.QueryFrame().ToImage <Bgr, Byte>())
             {
                 if (imageFrame != null)
                 {
                     try
                     {
                         imageFrame.Resize(640, 480, Emgu.CV.CvEnum.Inter.Cubic);//縮小圖像來源大小
                         _mainImageBox.Image     = _faceService.FaceDetection(imageFrame);
                         _cropFaceImageBox.Image = _faceService.GetFaceImage();
                     }
                     catch (Exception ex)
                     {
                         MessageBox.Show(ex.Message, "");
                         throw;
                     }
                 }
             }
         }
     }
 }
        public ActionResult Recognize(HttpPostedFileBase file)
        {
            if (file == null)
            {
                return(Content("null"));
            }
            _faceService = new FaceDP(Server.MapPath("~"));


            //傳入影像做臉部偵測
            Image <Bgr, byte> newImage = _faceService.FaceDetection(ConvertFileToImage(file));
            //偵測完的人臉做辨識
            int userId = _faceService.RecognizeFace();

            //var userNameLabel = _faceService.GetUserNameByUserId(userId);
            //PersonInfo personInfo = new PersonInfo();

            //查無此人
            if (userId == 0)
            {
                return(Content("null"));
            }

            ////將資料轉成陣列
            //personInfo = _faceService.GetPersonInfo(userId);
            //List<string> info = new List<string>();
            //info.Add(personInfo.userName);
            //info.Add(personInfo.gender);
            //info.Add(personInfo.birthDate.Date.ToString("yyyy/MM/dd"));
            //info.Add(personInfo.tag);
            var    info = _faceService.GetPersonInfo(userId);
            string json = JsonConvert.SerializeObject(info, Formatting.Indented);

            json = json.Substring(1, json.Length - 3);
            //var temp = Content(json, "application/json");
            //temp.ContentEncoding = Encoding.UTF8;

            return(Content(json));
        }
        public ActionResult Submit(HttpPostedFileBase file)
        {
            _faceService = new FaceDP(Server.MapPath("~"));

            ViewBag.NullFile      = false;
            ViewBag.WrongFileType = false;

            if (file == null)
            {
                ViewBag.NullFile = true;
                return(View());
            }
            if (file.ContentType != "image/jpeg")
            {
                ViewBag.WrongFileType = true;
                return(View());
            }

            //翻轉處理
            //Bitmap originImg = ConvertFileToImage(files).ToBitmap();
            var originImg = FileToImageRotate(file);


            //傳入影像做臉部偵測
            //Image<Bgr, byte> newImage = _faceService.FaceDetection(ConvertFileToImage(files));
            Bitmap bitmap   = _faceService.FaceDetection(originImg).Resize(450, 800, Emgu.CV.CvEnum.Inter.Linear).ToBitmap();
            Bitmap cropFace = _faceService.GetFaceImage().ToBitmap();

            //偵測完的人臉做辨識
            int       userId = _faceService.RecognizeFace();
            DataTable user   = _faceService.GetPersonInfo(userId);

            //轉換成bitmap格式,加上辨識後結果於左上角
            //Bitmap bitmap = newImage.ToBitmap();
            //Bitmap cropFace = _faceService.GetFaceImage().ToBitmap();

            //繪製辨識資訊
            //Graphics draw = Graphics.FromImage(bitmap);
            //draw.SmoothingMode = SmoothingMode.AntiAlias;
            //draw.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //draw.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //draw.FillRectangle(Brushes.Black, new Rectangle(0, 0, 150, 35));
            //draw.DrawString(userNameLabel, new Font("微軟正黑體", 18, FontStyle.Bold), Brushes.Red, new RectangleF(0, 0, 150, 35));
            //draw.Flush();

            //轉換影像成Stream
            var stream = new MemoryStream();

            bitmap.Save(stream, ImageFormat.Jpeg);
            var faceStream = new MemoryStream();

            cropFace.Save(faceStream, ImageFormat.Jpeg);

            //to base64
            // Convert byte[] to Base64 String
            var    imageBytes   = stream.ToArray();
            string base64String = Convert.ToBase64String(imageBytes);

            ViewBag.Image = base64String;

            imageBytes   = faceStream.ToArray();
            base64String = Convert.ToBase64String(imageBytes);

            ViewBag.FaceImage = base64String;

            //info
            ViewBag.UserName      = user.Rows[0]["userName"].ToString();
            ViewBag.UserGender    = user.Rows[0]["gender"].ToString();
            ViewBag.UserBirthDate = Convert.ToDateTime(user.Rows[0]["birthDate"]).ToString("yyyy/MM/dd");
            ViewBag.UserTag       = user.Rows[0]["tag"].ToString();
            DataTable dt = _faceService.GetGenderCode();

            ViewBag.GenderCode = _webModel.MapCodeData(dt);
            return(View());
        }