Search code examples
c#webcamaforgebootcamp

Macbook camera won't turn off and a minor C# aforge issue


I made project that could track objects using Visual Studio C# using the Aforge framework on a Macbook pro with bootcamp. The problem is when I run the code and close it. Then run it again, the code won't display the images on my camera anymore. In addition, the green light on my webcam is still on after the code has been stopped.

My second problem is that when I click the "track object button" and applies the filter to the images, for some reasons it applies the filter on two picture boxes instead of only one. I nee help in debugging my code. I tried re arranging the lines that applies the filter but I can't seem to get it right.

here is my code

namespace VideoProcessing1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Graphics g;
        Bitmap video;
        bool OnOff;
        int mode;
        int thoigianddemnguoc;

        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;

        private void Form1_Load(object sender, EventArgs e)
        {
            CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo Device in CaptureDevice)
            {
                comboBox1.Items.Add(Device.Name);

            }
            comboBox1.SelectedIndex = 0;
            FinalFrame = new VideoCaptureDevice();
            //FinalFrame.Stop();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
            FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
            FinalFrame.Start();

        }

        void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
            g = Graphics.FromImage(video2);
            g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
            g.Dispose();

            if (mode == 1)
            {
                ColorFiltering colorfilter = new ColorFiltering();
                colorfilter.Red = new AForge.IntRange(0, 255);
                colorfilter.Green = new AForge.IntRange(0, 75);
                colorfilter.Green = new AForge.IntRange(0, 75);
                colorfilter.ApplyInPlace(video2);
                BlobCounter blobcounter = new BlobCounter();
                blobcounter.MinHeight = 20;
                blobcounter.MaxWidth = 20;
                blobcounter.ObjectsOrder = ObjectsOrder.Size;
                blobcounter.ProcessImage(video2);
                Rectangle[] rect = blobcounter.GetObjectsRectangles();

                if (rect.Length > 0)
                {
                    Rectangle objec = rect[0];
                    Graphics graphic = Graphics.FromImage(video2);
                    using (Pen pen = new Pen(Color.White, 3))
                    {
                        graphic.DrawRectangle(pen, objec);
                    }
                    graphic.Dispose();
                }
                pictureBox2.Image = video2;

            }
            pictureBox1.Image = video2;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          if(FinalFrame.IsRunning==true)
            {
                FinalFrame.Stop();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            mode = 1;
        }
    }
}

Solution

  • Calling Stop on your video capture device is not recommended. According to the docs: "The correct way of stopping camera is signaling it stop and then waiting for background thread's completion."

    You should try using something like this to stop:

    FinalFrame.SignalToStop();
    FinalFrame.WaitForStop();
    

    Thread behavior can get unpredictable if not properly used, I believe your issue lies here.

    As to your second question, the problem line is

    pictureBox1.Image = video2;
    

    You already store video2 in pictureBox2.Image so I assume you don't want to do that to the other pictureBox.