public JsonResult RecognizeConcatPhoto(HttpPostedFileBase photo) { DataPath = Server.MapPath("~/App_Data/Faces"); //var uploadedFile = new byte[photo.InputStream.Length]; //photo.InputStream.Read(uploadedFile, 0, uploadedFile.Length); FaceRecognizer.Load(DataPath); Bitmap b = new Bitmap(Image.FromStream(photo.InputStream)); List <int> facesList = new List <int>(); List <FaceRecognizer.PredictionResult> PredictionResult = new List <FaceRecognizer.PredictionResult>(); FaceClassifier = new CascadeClassifier(Server.MapPath("~/App_Data/") + "haarcascade_frontalface_default.xml"); var persons = Enumerable.Empty <object>() .Select(r => new { Id = 0, ShortName = "", FirstName = "", LastName = "" }) // prototype of anonymous type .ToList(); persons.Remove(new { Id = 0, ShortName = "", FirstName = "", LastName = "" }); for (int j = 0; j < b.Width / 100; j++) { var b1 = b.Clone(new Rectangle(j * 100, 0, 100, 100), PixelFormat.Format32bppArgb); var gray = new Image <Gray, byte>(b1); gray._EqualizeHist(); Rectangle[] faces; try { faces = FaceClassifier.DetectMultiScale(gray, 1.1, 1, new Size(50, 50), new Size(gray.Width, gray.Height)); } catch (Exception e) { return(Json("There is no Faces: " + e)); } var predict = FaceRecognizer.Predict(gray); if (predict.Distance < 110) { var s = PhotoContext.People.Where(x => x.Id == predict.Label).Select(x => new { x.Id, x.ShortName, x.FirstName, x.LastName }).ToList(); persons.AddRange(s); PredictionResult.Add(predict); facesList.Add(predict.Label); } else { persons.Add(new { Id = 0, ShortName = "Unknown", FirstName = "", LastName = "" }); } } FaceRecognizer.Dispose(); return(Json(persons, JsonRequestBehavior.AllowGet)); }
public void Dispose() { recognizer.Dispose(); faces = null; names = null; GC.Collect(); }
public bool Train() { lock (_sync) { if (_images.Count <= 1) { return(false); } if (_faceRecognizer != null) { _faceRecognizer.Dispose(); } _faceRecognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 100); _faceRecognizer.Train(_images.ToArray(), _images.Select((c, i) => i).ToArray()); _shouldTrain = false; return(true); } }