EmguCV HaarCascade issue

本文关键字:issue HaarCascade EmguCV | 更新日期: 2023-09-27 18:07:33

我使用EmguCV开发了一个工作的c#人脸识别程序。

然而,如果我加载"haarcascade_fullbody.xml"而不是"haarcascade_frontalface_alt_tree.xml"我得到全能的访问违规。

这是代码;

public Bitmap detection(Bitmap Source)
    {
        List<Image<Gray, byte>> TrainedImages = this.TrainedImages;
        List<String> Names = this.Names;
        Image<Bgr, byte> ImageFrame = new Image<Bgr, byte>(Source);
        Image<Gray, byte> grayFrame = ImageFrame.Convert<Gray, byte>();
        Image<Bgr, byte> overlay = new Image<Bgr, byte>(Source.Width, Source.Height);
        Graphics FaceCanvas;
        List<String> finimg = new List<String>();
        //HaarCascade haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        HaarCascade haar = new HaarCascade("haarcascade_fullbody.xml");
        var faces = grayFrame.DetectHaarCascade(haar, 1.1, 3, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new System.Drawing.Size(25, 25))[0];
        foreach (var face in faces)
        {                
            overlay.Draw(face.rect, new Bgr(System.Drawing.Color.Green), 3);
            tempbmp = new Bitmap(100, 100);
            FaceCanvas = Graphics.FromImage(tempbmp);
            FaceCanvas.DrawImage(grayFrame.ToBitmap(), 0, 0, face.rect, GraphicsUnit.Pixel);
            detected.Add(tempbmp);
            if (doit)
            {
                saveBitmap(tempbmp, trainpath, trainnamer.Text);
                doit = false;
            }
            if (doit10)
            {
                for (int k = 1; k <= 10; k++)
                    saveBitmap(tempbmp, trainpath, trainnamer.Text);
                doit10 = false;
            }

            try
            {
                MCvTermCriteria termCrit = new MCvTermCriteria(TrainedImages.ToArray().Length, 0.001);//????????????
                EigenObjectRecognizer recognizer = new EigenObjectRecognizer(TrainedImages.ToArray(), Names.ToArray(), 2500, ref termCrit);
                MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
                String name = recognizer.Recognize(new Image<Gray, byte>(tempbmp));
                if (Names.Contains(name) == false)
                    name = "Stranger";
                else
                    name = removeformat(name);
                overlay.Draw(name, ref font, new System.Drawing.Point(face.rect.Left, face.rect.Top - 5), new Bgr(System.Drawing.Color.Green));
                finimg.Add(name);
            }
            catch (IndexOutOfRangeException)
            {
                MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
                ImageFrame.Draw("Stranger", ref font, new System.Drawing.Point(face.rect.Left, face.rect.Top - 5), new Bgr(color));
                continue;
            }
        }

        detected.Clear();
        Bitmap supra = overlay.ToBitmap();
        supra.MakeTransparent(System.Drawing.Color.Black);
        return supra;
    }

EmguCV HaarCascade issue

显然xml有问题,因为任何其他haarcascade我尝试加载加载成功。我建议使用HOGDescriptor"haarcascade_mcs_upperbody.xml"用于行人检测。