Search code examples
c#opencvexceptionemgucv

Emgu.CV.Util.CvException: 'OpenCV: cv::XMLParser::parse' Error C#


Hello,
In a face recognition program I wrote using c#, I get an error opening the program.
Forum1 Codes:

using AForge.Video;
using AForge.Video.DirectShow;
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace FaceApp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    FilterInfoCollection filter;
    VideoCaptureDevice device;

    CascadeClassifier cascadeClassifier = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");

    private void Form1_Load(object sender, EventArgs e)
    {
        filter = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo device in filter)
            comboBox1.Items.Add(device.Name);
        comboBox1.SelectedIndex = 0;
        device = new VideoCaptureDevice();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        device = new VideoCaptureDevice(filter[comboBox1.SelectedIndex].MonikerString);
        device.NewFrame += Device_NewFrame;
        device.Start();
    }

    private void Device_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
        Image<Bgr, byte> grayImage = new Image<Bgr, byte>(bitmap);
        Rectangle[] rectangles = cascadeClassifier.DetectMultiScale(grayImage, 1.2, 1);
        foreach (Rectangle rectangle in rectangles)
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                using (Pen pen = new Pen(Color.Red, 1))
                {
                    graphics.DrawRectangle(pen, rectangle);
                }
            }
        }
        pictureBox1.Image = bitmap;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (device.IsRunning)
            device.Stop();
    }
}
}

The line of code that the program failed:

CascadeClassifier cascadeClassifier = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");

Error: Exception Unhandled

Emgu.CV.Util.CvException: 'OpenCV: cv::XMLParser::parse'

I searched the error a bit on the internet but could not find a solution.
The code I wrote down below didn't work either.

private static readonly CascadeClassifier Classifier = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");
// or
private readonly CascadeClassifier Classifier = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");

How can I solve this problem?


Solution

  • I know it's too late, but for the benefit of other people who are looking for this problem

    You should use those files : https://github.com/opencv/opencv/raw/4.2.0/data/haarcascades/

    *opencv version 4.2.0 is used in Emgu.CV.Models v4.7.0.5276

    or use Emgu.CV.Models for example

    var newFaceCascade = new Emgu.CV.Models.CascadeFaceAndEyeDetector();
    newFaceCascade.Init() //in this method Emgu is downloads related files to Model from said link 
    newFaceCascade.Detect(image/*Emgu.CV.Mat*/, faces /*List<Rectangle>*/, eyes /*List<Rectangle>*/);
    

    It's work for me Like a Lamborghini in .Net 7.

    List item

    I found this method from decompiled source of Emgu.CV.Models.CascadeFaceAndEyeDetector