1

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?

3 Answers 3

0

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

0

I know my answer is late, but it would be helpful for those who face same issue. I faced same issue, it is due to xml file. I downloaded the haarcascades from the EmguCV github page.

1
  • can you explain this answer? I'm facing this same problem now... PLEASE HELP!
    – big boy
    Commented Jun 15, 2022 at 19:21
0

All the sample programs related to OpenCV, Accord.net, Emgu, etc. are all using .NET framework 4.7.2 or similar. If anyone tries to use .NET core or .NET 5/6/7/8 it doesn't work showing XML parser error stating that the CascadeClassifier could not get the "haarcascade_frontalface_alt_tree.xml" file loaded into it. All the .NET wrapper apps have stopped update for latest .NET framework.

Similarly the support for System.Drawing namespace (pure platform independent) in .NET 7> is not straightforward. You may choose to use Microsoft.Maui.Graphics to get Image(of Bgr, Byte) and compatible Bitmap types working over other tools like ImageSharp etc.

Having said that to get face OpenCV working in portable .NET we need to wait until someone updates OpenCV or Accord.NET or Emgu or similar to support latest .NET. Thanks.

Not the answer you're looking for? Browse other questions tagged or ask your own question.