Search code examples
c#visual-studio-2015webcamemgucvlive-video

Exception In C# Code In Capturing Video From WebCam using Emgu CV 3.1 In Visual Studio 2015


I am new to OpenCV.

I am using Emgu CV 3.1 Library in C# and Visual Studio 2015.

I am facing a problem in reading a live video from a WebCam. I don't know why the exception occurred by Capture() Constructor. I wasted my 2 days upon it.

Plzzz help me and provide me a solution in Emgu CV 3.1 on Visual Studio 2015. I got TypeInitializationException. I aslo upload the picture of exception. TypeInitializationException Is here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.Structure;

namespace FaceRecognition_3._0
{
    public partial class Form1 : Form
    {
        private Capture _capture;
        private CascadeClassifier _cascadeClassifier;


        public Form1()
        {
            InitializeComponent();

            _capture = new Capture();
            imgCamUser.Image = _capture.QueryFrame();
            startProcess();
        }

        public void startProcess()
        {
            _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_alt_tree.xml");

            using (var imageFrame = _capture.QueryFrame().ToImage<Bgr, Byte>())
            {
                if (imageFrame != null)
                {
                    var grayframe = imageFrame.Convert<Gray, byte>();
                    var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty); //the actual face detection happens here
                    foreach (var face in faces)
                    {
                        imageFrame.Draw(face, new Bgr(Color.BurlyWood), 3); //the detected face(s) is highlighted here using a box that is drawn around it/them

                    }
                }

                imgCamUser.Image = imageFrame;
            }
        }
    }
}

`


Solution

  • This exception is thrown when Emgu.CV.CvInvoke cannot find the unmanaged dlls in the folder of your executable.

    According to EmguCV Wiki, you have to copy the OpenCV dlls from <EmguFolder>/bin to the execution directory (probably bin/Debug).