EmguCV - Face Recognition - 'Object reference not set' exception when using training set from Microsoft Access Database

前端 未结 3 542
情歌与酒
情歌与酒 2020-12-20 04:25

I\'ve been developing a face recognition application using EmguCV (C#). I got the whole thing working okay if I store the face images (training set) in simple windows folder

3条回答
  •  自闭症患者
    2020-12-20 04:44

    Finally made it!! just one more day of coding helped me to got the problem solved:

    public void ProcessRequest(HttpContext context)
    {
        _httpContext = context;
        var imageid = context.Request.QueryString["Image"];
        if (imageid == null || imageid == "")
        {
            imageid = "1";
        }
    
    
        using (WebClient wc = new WebClient())
        {
            // Handler retrieves the image from database and load it on the stream
            using (Stream s = wc.OpenRead("http://mypageurl/Image.ashx?Image=" + imageid))
            {
                using (Bitmap bmp = new Bitmap(s))
                {
                    AddFace(bmp);
                }
            }
        }
    
    }
    
    public void AddFace(Bitmap image)
    {
        var faceImage = DetectFace(image);
        if (faceImage != null)
        {
            var stream = new MemoryStream();
            faceImage.Save(stream, ImageFormat.Bmp);
            stream.Position = 0;
            byte[] data = new byte[stream.Length];
            stream.Read(data, 0, (int)stream.Length);
    
            _httpContext.Response.Clear();
            _httpContext.Response.ContentType = "image/jpeg";
            _httpContext.Response.BinaryWrite(data);
        }
    }
    
    private Bitmap DetectFace(Bitmap faceImage)
    {
        var image = new Image(faceImage);
        var gray = image.Convert();
        string filePath = HttpContext.Current.Server.MapPath("haarcascade_frontalface_default.xml");
        var face = new HaarCascade(filePath);
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.1, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
        Image result = null;
    
        foreach (MCvAvgComp f in facesDetected[0])
        {
            //draw the face detected in the 0th (gray) channel with blue color
            image.Draw(f.rect, new Bgr(Color.Blue), 2);
            result = image.Copy(f.rect).Convert();
            break;
        }
    
        if (result != null)
        {
            result = result.Resize(200, 200, INTER.CV_INTER_CUBIC);
            return result.Bitmap;
        }
    
    
       return null;
    }
    
    public bool IsReusable
    {
        get { return false; }
    }
    

提交回复
热议问题