private void CameraCapture() { frame = new Mat(); //Matrix to save the picture capture.Retrieve(frame, 0); //retrieve the picture to the matrinx using (Image <Bgr, byte> image = frame.ToImage <Bgr, byte>()) { if (frame != null) { Image <Gray, byte> grayFrame = frame.ToImage <Gray, byte>(); // display the image in the imageBox faces = cascade.DetectMultiScale(grayFrame, 1.1, 2, new Size(30, 30)); Bitmap BitmapInput = grayFrame.ToBitmap(); Bitmap ExtractedFace; Graphics FaceCanvas; if (faces.Count() > 0) { foreach (var face in faces) { image.Draw(face, new Bgr(Color.Blue), 1); // draw rectangles in the picture ExtractedFace = new Bitmap(face.Width, face.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BitmapInput, 0, 0, face, GraphicsUnit.Pixel); } } imageBox1.Image = image; // display the image in the imageBox } } }
public Rectangle[] Detect(Image <Bgr, Byte> image, double scaleFactor, int minNeighbors, int windowSizeX, int windowSizeY) { Image <Gray, byte> grayImage = image.Convert <Gray, byte>(); Graphics FaceCanvas; Bitmap BmpInput = grayImage.ToBitmap(); this.scaleFactor = scaleFactor; this.minNeighbors = minNeighbors; this.windowSizeX = windowSizeX; this.windowSizeY = windowSizeY; faces = csClassifier.DetectMultiScale(grayImage, this.scaleFactor, this.minNeighbors, new Size(this.windowSizeX, this.windowSizeY), new Size(this.windowSizeX, this.windowSizeY)); if (faces.Length > 0) { this.numberOFFaces = faces.Length; ExtractedFaces = new Bitmap[numberOFFaces]; for (int i = 0; i < numberOFFaces; i++) { ExtractedFaces[i] = new Bitmap(faces[i].Width, faces[i].Height); FaceCanvas = Graphics.FromImage(ExtractedFaces[i]); FaceCanvas.DrawImage(BmpInput, 0, 0, faces[i], GraphicsUnit.Pixel); } } return(faces); }
private void ProcessFrame(object sender, EventArgs arg) { Mat frame = new Mat(); //Matrix to save the picture capture.Retrieve(frame, 0); //retrieve the picture to the matrinx Image <Bgr, byte> image = frame.ToImage <Bgr, byte>(); FaceNo = 0; if (frame != null) { Image <Gray, byte> grayFrame = frame.ToImage <Gray, byte>(); // display the image in the imageBox faces = cascade.DetectMultiScale(grayFrame, 1.1, 2, new Size(30, 30)); Bitmap BitmapInput = grayFrame.ToBitmap(); Bitmap ExtractedFace; Graphics FaceCanvas; //countTable.Text = faces.Count().ToString(); if (faces.Count() > 0) { foreach (var face in faces) { image.Draw(face, new Bgr(Color.Blue), 1); // draw rectangles in the picture ExtractedFace = new Bitmap(face.Width, face.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BitmapInput, 0, 0, face, GraphicsUnit.Pixel); ExtFaces.Add(ExtractedFace); FaceNo++; } } imageBox1.Image = image; // display the image in the imageBox } }
private void VideoCapture() { using (frame = FrameCapture.QueryFrame()) { if (frame != null) { using (Image <Gray, byte> grayFrame = frame.ToImage <Gray, byte>()) { faces = cascade.DetectMultiScale(grayFrame, 1.1, 2, new Size(100, 100)); Bitmap BitmapInput = grayFrame.ToBitmap(); Graphics FaceCanvas; if (faces.Count() > 0) { foreach (var face in faces) { grayFrame.Draw(face, new Gray(), 2); // draw rectangles in the picture ExtractedFace = new Bitmap(face.Width, face.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BitmapInput, 0, 0, face, GraphicsUnit.Pixel); } imageBox1.Image = grayFrame; } } } } }
public void DetectFaceEyeHaar() { try { int personNumber = 0; string eyePath = Path.GetFullPath(@"../../data/haarcascade_eye.xml"); string facePath = Path.GetFullPath(@"../../data/haarcascade_frontalface_default.xml"); CascadeClassifier classifierEye = new CascadeClassifier(eyePath); CascadeClassifier classifierFace = new CascadeClassifier(facePath); var imgGray = imgInput.Convert <Gray, byte>().Clone(); double scale = Convert.ToDouble(textBoxScale.Text); int neig = Convert.ToInt32(numericNeig.Value); int border = Convert.ToInt32(numericBorder.Value); double r, g, b; r = Convert.ToDouble(panelBorderColor.BackColor.R); g = Convert.ToDouble(panelBorderColor.BackColor.G); b = Convert.ToDouble(panelBorderColor.BackColor.B); Rectangle[] faces = classifierFace.DetectMultiScale(imgGray, scale, neig); if (faces.Length > 0) { Bitmap bmpInput = imgGray.ToBitmap(); Graphics FaceCanvas; ExtFaces = new Bitmap[faces.Length]; Bitmap ExtractedFace; // empty faceNo = 0; foreach (var face in faces) { personNumber += 1; imgInput.Draw(face, new Bgr(0, 0, 255), border); ExtractedFace = new Bitmap(face.Width, face.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(bmpInput, 0, 0, face, GraphicsUnit.Pixel); ExtFaces[faceNo] = ExtractedFace; faceNo++; imgGray.ROI = face; Rectangle[] eyes = classifierEye.DetectMultiScale(imgGray, scale, neig); foreach (var eye in eyes) { var e = eye; e.X += face.X; e.Y += face.Y; imgInput.Draw(e, new Bgr(0, 255, 0), border); } } pictureBox.Image = imgInput.Bitmap; labelPersonNumber.Text = Convert.ToString(personNumber); } } catch (Exception ex) { throw new Exception(ex.Message); } }
public void SpawnGuest(int fixFloor = 99) { int currentFloor = Random.Range(0, HotelController.Instance.hotel.floors.Count); int targetFloor = currentFloor; float maxWaitingTime = Random.Range(50f, 120f); // maxWaitingTime = Random.Range(1, 2f); while (targetFloor == currentFloor) { targetFloor = Random.Range(0, HotelController.Instance.hotel.floors.Count); } if (fixFloor != 99) { currentFloor = fixFloor; targetFloor = 1; } Guest guest = new Guest(currentFloor, targetFloor, maxWaitingTime); GameObject guestGO = SimplePool.Spawn(guestPrefab, Vector3.zero, Quaternion.identity); guestGO.transform.SetParent(worldTransform); guestGO.transform.position = new Vector3(4.5f, (float)currentFloor, 0f); guestGO.transform.localScale = new Vector3(0.8f, 0.8f, 0.8f); guestGO.transform.eulerAngles = new Vector3(0f, 90f, 0f); guestGO.GetComponent <FaceCanvas>().hud.gameObject.SetActive(true); guestGO.SetActive(true); guests.Add(guest); guestGameObjects.Add(guestGO); guestToGameObjectMap.Add(guest, guestGO); gameObjectToGuestMap.Add(guestGO, guest); guestGO.transform.DOMove(new Vector3(Random.Range(-1.8f, 1.2f), guestGO.transform.position.y, Random.Range(-0.5f, 1.5f)), Random.Range(2f, 3f)) .OnComplete(() => GuestReadyForLift(guest, guestGO)); guestGO.name = "Guest"; FaceCanvas hud = guestGO.GetComponent <FaceCanvas>(); hud.targetFloorText.text = HotelController.Instance.GetFloor(targetFloor).shortName; guest.hud = hud; UpdatePatienceBar(guest); guestCount[currentFloor]++; UpdateLeftPanel(currentFloor); spawnGap = Mathf.Max(minSpawnGap, spawnGap - spawnReduce); }
//face detection function using viola-jones method private void detectFaces() { if (ImageFrame != null) // confirm that image is valid { //convert the image to gray scale Image <Gray, byte> grayframe = ImageFrame.Convert <Gray, byte>(); windowsSize = int.Parse(textBoxDetScale.Text); ScaleIncreaseRate = Double.Parse(comboBoxScaleRate.Text); minNeighbors = int.Parse(comboBoxMinNeigh.Text); //Detect faces from the gray-scale image var faces = grayframe.DetectHaarCascade(haar_face, ScaleIncreaseRate, minNeighbors, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(windowsSize, windowsSize))[0]; int TotalFaces = faces.Length; textBox1.Text = TotalFaces.ToString(); if (faces.Length > 0) { Bitmap BmpInput = grayframe.ToBitmap(); Bitmap ExtractedFace; //empty Graphics FaceCanvas; ExtFaces = new Bitmap[faces.Length]; faceNo = 0; foreach (var face in faces) { ImageFrame.Draw(face.rect, new Bgr(Color.Red), 3); //set the size of the empty box(ExtractedFace) which will later contain the detected face ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height); //set empty image as FaceCanvas, for painting FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BmpInput, 0, 0, face.rect, GraphicsUnit.Pixel); ExtFaces[faceNo] = ExtractedFace; faceNo++; } pbExtractedFaces.Image = ExtFaces[0]; //Display the detected faces in imagebox CamImageBox.Image = ImageFrame; btnNext.Enabled = true; btnPrev.Enabled = true; } } }
//////////////////////////////////////////////////////////////////////////////////////// // FUNCTIONS USED TO DETECT FACES IN INPUT IMAGE //////////////////////////////////////////////////////////////////////////////////////// private void DetectFaces() { Image <Gray, byte> grayframe = ImageFrame.Convert <Gray, byte>(); //Assign user-defined Values to parameter variables: MinNeighbors = int.Parse(comboBoxMinNeigh.Text); // the 3rd parameter WindowsSize = int.Parse(textBoxWinSiz.Text); // the 5th parameter ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd parameter //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]' var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate, MinNeighbors, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(WindowsSize, WindowsSize))[0]; if (faces.Length > 0) { MessageBox.Show("Total Faces Detected: " + faces.Length.ToString()); Bitmap BmpInput = grayframe.ToBitmap(); Bitmap ExtractedFace; //empty Graphics FaceCanvas; ExtFaces = new Bitmap[faces.Length]; faceNo = 0; //draw a green rectangle on each detected face in image foreach (var face in faces) { ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3); //set the size of the empty box(ExtractedFace) which will later contain the detected face ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height); //set empty image as FaceCanvas, for painting FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BmpInput, 0, 0, face.rect, GraphicsUnit.Pixel); ExtFaces[faceNo] = ExtractedFace; faceNo++; } pbExtractedFaces.Image = ExtFaces[0]; //Display the detected faces in imagebox CamImageBox.Image = ImageFrame; btnNext.Enabled = true; btnPrev.Enabled = true; } }
private void CameraCapture() { Mat frame = new Mat(); //Matrix to save the picture Camera.Retrieve(frame, 0); //retrieve the picture to the matrinx Image <Bgr, byte> image = frame.ToImage <Bgr, byte>(); if (frame != null) { Image <Gray, byte> grayframe = frame.ToImage <Gray, byte>(); // display the image in the imageBox faces = cascade.DetectMultiScale(grayframe, 1.1, 2, new Size(30, 30)); Bitmap BitmapInput = grayframe.ToBitmap(); Bitmap ExtractedFace; Graphics FaceCanvas; ExtFaces = new List <Bitmap>(); if (faces.Count() > 0) { foreach (var face in faces) { image.Draw(face, new Bgr(Color.Green), 1); // draw rectangles in the picture ExtractedFace = new Bitmap(face.Width, face.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BitmapInput, 0, 0, face, GraphicsUnit.Pixel); ExtFaces.Add(ExtractedFace); Image <Gray, byte> ExtracreFaceAsImage = new Image <Gray, byte>(ExtractedFace).Resize(100, 100, Inter.Cubic); /*FaceRecognizer.PredictionResult result = new FaceRecognizer.PredictionResult(); * result = fr1.Predict(ExtracreFaceAsImage); * * if (attendanceResult.ContainsKey(result.Label)) * attendanceResult.Add(result.Label, 0); * else * attendanceResult.Add(result.Label, ++attendanceResult[result.Label]); * * result = fr3.Predict(ExtracreFaceAsImage);//receiving the result * if (attendanceResult.ContainsKey(result.Label)) * attendanceResult.Add(result.Label, 0); * else * attendanceResult.Add(result.Label, ++attendanceResult[result.Label]); * /*result = fr2.Predict(ExtracreFaceAsImage);//receiving the result * attendanceResult.Add(result.Label, ++attendanceResult[result.Label]);*/ } } imageBox1.Image = image; } }
//----------------------------------------------------------------------------// //<<<<<<<<------FUNCTION USED TO DETECT AND EXTRACT FACES--------------->>>>>>>> //----------------------------------------------------------------------------// private void DetectFaces() { Image <Gray, byte> grayframe = TestImage.Convert <Gray, byte>(); //Assign user-defined Values to parameter variables: MinNeighbors = int.Parse(comboBoxMinNeigh.Text); // the 3rd parameter WindowsSize = int.Parse(textBoxWinSiz.Text); // the 5th parameter ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd parameter //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]' var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate, MinNeighbors, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(WindowsSize, WindowsSize))[0]; if (faces.Length > 0) { MessageBox.Show("Total Faces Detected: " + faces.Length.ToString()); Bitmap BmpInput = grayframe.ToBitmap(); Bitmap ExtractedFace; // an empty "box"/"image" to hold the extracted face. Graphics FaceCanvas; //Set The Face Number //FaceCollection = Directory.GetFiles(@"Face Collection\", "*.bmp"); //int FaceNo = FaceCollection.Length; //A Bitmap Array to hold the extracted faces EXfaces = new Bitmap[faces.Length]; int i = 0; //draw a green rectangle on each detected face in image foreach (var face in faces) { //locate the detected face & mark with a rectangle TestImage.Draw(face.rect, new Bgr(Color.Green), 3); //set the size of the empty box(ExtractedFace) which will later contain the detected face ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height); //set empty image as FaceCanvas, for painting FaceCanvas = Graphics.FromImage(ExtractedFace); //graphics draws the located face on the faceCancas, thus filling the empty ExtractedFace //image with exact pixels of the face to be extracted from input image FaceCanvas.DrawImage(BmpInput, 0, 0, face.rect, GraphicsUnit.Pixel); //save this extracted face to hard disk //ExtractedFace.Save(@"Face Collection\" + "Face_" + (FaceNo++) + ".bmp");//save images in folder //Save this extracted face to array EXfaces[i] = ExtractedFace; i++; } //Display the detected faces in imagebox CamImageBox.Image = TestImage; MessageBox.Show(faces.Length.ToString() + " Face(s) Extracted sucessfully!"); pbCollectedFaces.Image = EXfaces[0]; btnAddtoTS.Enabled = true; txtBoxFaceName.Enabled = true; if (faces.Length > 1) { btnNext.Enabled = true; btnPrev.Enabled = true; } else { btnNext.Enabled = false; btnPrev.Enabled = false; } } else { MessageBox.Show("NO faces Detected!"); } }
private void DetectFaces() { Image <Gray, byte> grayframe = ImageFrame.Convert <Gray, byte>(); //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]' var faces = grayframe.DetectHaarCascade( haar, 1.2, 2, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0]; Bitmap BmpInput = grayframe.ToBitmap(); Bitmap ExtractedFace; //empty Graphics FaceCanvas; //empty ExtFaces = new Bitmap[faces.Length]; FaceRecc = new Bitmap[faces.Length]; //draw a green rectangle on each detected face in image foreach (var face in faces) { ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3); //set the size of the empty box(ExtractedFace) for image to store ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height); //set empty image as face canvas FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BmpInput, 0, 0, face.rect, GraphicsUnit.Pixel); try { ExtFaces[faceNo] = ExtractedFace; FaceRecc[faceNo] = ExtractedFace; faceNo++; } catch (System.IndexOutOfRangeException ew) { MessageBox.Show("Invalid Input"); } } try { faceNo = 0; pbDetectedFaces.Image = ExtFaces[faceNo]; //Display the detected faces in imagebox InputImageBox.Image = ImageFrame; Next.Enabled = true; Previous.Enabled = true; DFName.Enabled = true; DFRollNo.Enabled = true; btnAddToTrainingSet.Enabled = true; btnRecognize.Enabled = true; } catch (Exception e) { MessageBox.Show("No faces found. Please provide valid input"); } }
private void VideoCapture() { using (frame = VideoPlayer.QueryFrame()) { if (frame != null) { using (Image <Gray, byte> grayFrame = frame.ToImage <Gray, byte>()) // display the image in the imageBox { faces = cascade.DetectMultiScale(grayFrame, 1.1, 2, new Size(100, 100)); Bitmap BitmapInput = grayFrame.ToBitmap(); Bitmap ExtractedFace; Graphics FaceCanvas; String s = ""; if (faces.Count() > 0) { int i = 0; foreach (var face in faces) { grayFrame.Draw(face, new Gray(), 2); // draw rectangles in the picture ExtractedFace = new Bitmap(face.Width, face.Height); FaceCanvas = Graphics.FromImage(ExtractedFace); FaceCanvas.DrawImage(BitmapInput, 0, 0, face, GraphicsUnit.Pixel); Image <Gray, byte> ExtracreFaceAsImage = new Image <Gray, byte>(ExtractedFace).Resize(200, 200, Inter.Cubic); ExtracreFaceAsImage._EqualizeHist(); FaceRecognizer.PredictionResult result = new FaceRecognizer.PredictionResult(); result = fr1.Predict(ExtracreFaceAsImage); if (result.Distance <= 8000) { //s += i + " " + result.Label + " (" + result.Distance + ") "; if (!attendanceResult.ContainsKey(result.Label)) { attendanceResult.Add(result.Label, 0); } else { attendanceResult[result.Label]++; } } result = fr2.Predict(ExtracreFaceAsImage);//receiving the result if (result.Distance <= 3100) { //s += i + " " + result.Label + " (" + result.Distance + ") "; if (!attendanceResult.ContainsKey(result.Label)) { attendanceResult.Add(result.Label, 0); } else { attendanceResult[result.Label]++; } } result = fr3.Predict(ExtracreFaceAsImage);//receiving the result if (result.Distance <= 100) { //s += i + " " + result.Label + " (" + result.Distance + ") "; if (!attendanceResult.ContainsKey(result.Label)) { attendanceResult.Add(result.Label, 0); } else { attendanceResult[result.Label]++; } } //ExtracreFaceAsImage.Save("E:\\Result\\extfaces\\" + i + ".bmp"); s += "\n"; i++; } imageBox1.Image = grayFrame.Resize(800, 600, Inter.Cubic); } } } } }