Resolved Person Detection with Webcam using OpenCV

ocnuybear

New member
Joined
Mar 30, 2023
Messages
4
Programming Experience
5-10
Can someone please help correct this code:

Need to track Full Body People, not the usual faces and it is very hard to find c# code for that specificly.

C#:
using System;
using System.Diagnostics.Tracing;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;


namespace Webcam
{
    public partial class Form1 : Form
    {

        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoDevice;
        private CascadeClassifier haarCascade;
        public Form1()
        {
            InitializeComponent();
            InitializeVideoDevices();

            // Initialize the Haar Cascade Classifier with the haarcascade_fullbody.xml file
            haarCascade = new CascadeClassifier("haarcascade_fullbody.xml");
        }

        private void InitializeVideoDevices()
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count == 0)
            {
                MessageBox.Show("No video devices found.");
                return;
            }

            videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
            videoDevice.NewFrame += VideoDevice_NewFrame;
        }

        private void VideoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap frame = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = frame;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnStartCamera_Click(object sender, EventArgs e)
        {

            using (Bitmap frame = (Bitmap)EventArgs.Frame.Clone())
            {
                // Convert the frame to grayscale
                using (Image<Gray, byte> grayFrame = new Image<Gray, byte>(frame))
                {
                    // Detect persons in the frame
                    Rectangle[] persons = haarCascade.DetectMultiScale(grayFrame, 1.1, 3, new Size(50, 50), Size.Empty);

                    // Draw rectangles around detected persons
                    using (Graphics g = Graphics.FromImage(frame))
                    {
                        foreach (Rectangle person in persons)
                        {
                            g.DrawRectangle(Pens.Red, person);
                        }
                    }
                }

                // Update PictureBox with the processed frame
                pictureBox1.Image = frame;
            }
        }

        private void btnStopCamera_Click(object sender, EventArgs e)
        {
            if (videoDevice != null && videoDevice.IsRunning)
            {
                videoDevice.SignalToStop();
                videoDevice.WaitForStop();
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            btnStopCamera_Click(null, null);
        }
    }
}

Visual Studio 2022 Community Edition complains about 2 problems:
1. 'VideoCaptureDevice' does not contain a definition for 'GetCurrentVideoFrame'
2. Argument 1: cannot convert from 'System.Drawing.Bitmap' to 'byte[*,*,*]'

Just need to get the FrameCode correct so that it displays the captured video feed using yhe webcam please assist?
 
2. Argument 1: cannot convert from 'System.Drawing.Bitmap' to 'byte[*,*,*]'

Yup, Bitmap doesn't do any implicit conversion of the the bitmap data 3 dimensional array of bytes. You'll need to create the array yourself and fill the array with data from the bitmap.
 
Your first error seems to be related to AForge rather than OpenCV. The second error seems to be just a matter of giving OpenCV the data in the form that it needs.
 
Got this code to work and just by changing the casacade file and little code it works 100%

C#:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

namespace WinFormsEyeDetection
{
    public partial class Form1 : Form
    {
        private VideoCapture _capture;
        private CascadeClassifier _eyesCascade;
        private static string _eyesCascadeName = "haarcascade_eye.xml";

        public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            _capture = new VideoCapture(0);
            string eyeCascadePath = "D:\\VS\\WinFormsEyeDetection\\haarcascade_eye.xml";
            _eyesCascade = new CascadeClassifier(eyeCascadePath);
            timer1.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            using (Mat frame = _capture.QueryFrame())
            {
                if (frame != null)
                {
                    var imageFrame = frame.ToImage<Bgr, byte>();
                    if (imageFrame != null)
                    {
                        var grayFrame = imageFrame.Convert<Gray, byte>();
                        var eyes = _eyesCascade.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty);

                        foreach (var eye in eyes)
                        {
                            imageFrame.Draw(eye, new Bgr(Color.Red), 2);
                        }

                        pictureBox1.Image = imageFrame.ToBitmap();
                    }
                }
            }
        }
    }
}
 
Back
Top Bottom