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

前端 未结 3 546
情歌与酒
情歌与酒 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条回答
  •  旧时难觅i
    2020-12-20 04:56

    Check your fetchedBytes array to see if you are consistently getting just a stream of bytes representing a BMP image (starting with 0x42 0x4D), or if there may be "other stuff" in there, too.

    Depending on how the BMP data was inserted into the Access database it may contain an OLE "wrapper". For example, an 8x8 24-bit BMP image of pure red is saved by MSPAINT.EXE like this

    bmpDump.png

    If I copy that file and paste it into a Bound Object Frame in an Access form then Access wraps the BMP data in some "OLE stuff" before writing it to the table. Later, if I try to retrieve the BMP image via code, using something like this...

    Sub oleDumpTest()
        Dim rst As ADODB.Recordset, ads As ADODB.Stream
        Set rst = New ADODB.Recordset
        rst.Open "SELECT * FROM TrainingSet1 WHERE ID = 1", Application.CurrentProject.Connection
        Set ads = New ADODB.Stream
        ads.Type = adTypeBinary
        ads.Open
        ads.Write rst("FaceImage").Value
        rst.Close
        Set rst = Nothing
        ads.SaveToFile "C:\Users\Gord\Pictures\oleDump_red."
        ads.Close
        Set ads = Nothing
    End Sub
    

    ...then the resulting file also contains the OLE "wrapper"...

    ...and obviously is not a valid stand-alone BMP file. If I rename that file to give it a .bmp extension and try to open it in Paint, I get

    paintError.png

    So maybe (some of) the [FaceImage] objects in your database are not raw BMP data, and perhaps the other software is rejecting them (or simply not able to understand them).

    Edit

    Another possible issue is that when you get the images from files in a folder you hand the Image object a string containing the file path...

    trainingImages.Add(new Image(Application.StartupPath + "\\TrainedFaces\\" + LoadFaces));
    

    ...but when you try to retrieve the images from the database you hand the same object a Bitmap object

    MemoryStream stream = new MemoryStream(fetchedBytes);
    bmpImage = new Bitmap(stream);
    trainingImages.Add(new Emgu.CV.Image(bmpImage));
    

    I have no way of knowing whether the Emgu.CV.Image object might behave differently depending on the type of object it is given, but a quick+dirty workaround might be to write bmpImage to a temporary file, hand trainingImages.Add the path to that file, and then delete the file.

提交回复
热议问题