I am using luxand Face SDK to develop a Face recognition application using Visual Studio. I am trying to modify a sample application. In the application the samples are kept in computer memory, but I am trying to write it to a file and later read from it. The code extract is given below.
The application runs fine and it save the file. But when it tries to read the file the application stops working and I get error "An unhandled exception of type 'System.NullReferenceException' occurred in LiveRecognition_VS2008.exe Additional information: Object reference not set to an instance of an object" and the this part of the code is highlighted " br1.Read(t1.templateData, 0, t1.templateData.Length)"
Please guide to me what is the error. I am reading the file wrong?
struct FaceTemplate { // single template public byte [] templateData; } List faceTemplates; // set of face templates (we store 10) String cameraName; bool needClose = false; string userName; // WinAPI procedure to release HBITMAP handles returned by FSDKCam.GrabFrame [DllImport("gdi32.dll")] static extern bool DeleteObject(IntPtr hObject); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { if (FSDK.FSDKE_OK != FSDK.ActivateLibrary("# snip serial key #")) { MessageBox.Show("Please run the License Key Wizard (Start - Luxand - FaceSDK - License Key Wizard)", "Error activating FaceSDK", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } FSDK.InitializeLibrary(); FSDKCam.InitializeCapturing(); string [] cameraList; int count; FSDKCam.GetCameraList(out cameraList, out count); if (0 == count) { MessageBox.Show("Please attach a camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } FSDKCam.VideoFormatInfo [] formatList; FSDKCam.GetVideoFormatList(ref cameraList[0], out formatList, out count); pictureBox1.Width = formatList[0].Width; pictureBox1.Height = formatList[0].Height; this.Width = formatList[0].Width + 48; this.Height = formatList[0].Height + 116; cameraName = cameraList[0]; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { needClose = true; } private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; int cameraHandle = 0; int r = FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle); if (r != FSDK.FSDKE_OK) { MessageBox.Show("Error opening the first camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } btnRemember.Enabled = true; // set realtime face detection parameters FSDK.SetFaceDetectionParameters(false, false, 100); FSDK.SetFaceDetectionThreshold(3); // list where we store face templates faceTemplates = new List(); while (!needClose) { Int32 imageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera { Application.DoEvents(); continue; } FSDK.CImage image = new FSDK.CImage(imageHandle); Image frameImage = image.ToCLRImage(); Graphics gr = Graphics.FromImage(frameImage); FSDK.TFacePosition facePosition = image.DetectFace(); // if a face is detected, we can recognize it if (facePosition.w != 0) { gr.DrawRectangle(Pens.LightGreen, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); // create a new face template FaceTemplate template = new FaceTemplate(); if (programState == ProgramState.psRemember || programState == ProgramState.psRecognize) template.templateData = image.GetFaceTemplateInRegion(ref facePosition); switch (programState) { case ProgramState.psNormal: // normal state - do nothing break; case ProgramState.psRemember: // Remember Me state - store facial templates faceTemplates.Add(template); label1.Text = "Templates stored: " + faceTemplates.Count.ToString(); if (faceTemplates.Count > 0) { // get the user name InputName inputName = new InputName(); inputName.ShowDialog(); userName = inputName.userName; FileStream fs = File.Open(userName + ".bin", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); //Opens a binary writer (writes to file stream) bw.Write(template.templateData, 0, template.templateData.Length); bw.Close(); fs.Close(); programState = ProgramState.psRecognize; } break; case ProgramState.psRecognize: // recognize the user bool match = false; /* foreach (FaceTemplate t in faceTemplates) { float similarity = 0.0f; FaceTemplate t1 = t; FSDK.MatchFaces(ref template.templateData, ref t1.templateData, ref similarity); float threshold = 0.0f; FSDK.GetMatchingThresholdAtFAR(0.01f, ref threshold); // set FAR to 1% if (similarity > threshold) { match = true; break; } } */ FaceTemplate t1 = new FaceTemplate(); FileStream fs1 = File.Open(userName + ".bin", FileMode.Open,FileAccess.Read); BinaryReader br1 = new BinaryReader(fs1); br1.Read(t1.templateData, 0, t1.templateData.Length); float similarity = 0.0f; FSDK.MatchFaces(ref template.templateData, ref t1.templateData, ref similarity); float threshold = 0.0f; FSDK.GetMatchingThresholdAtFAR(0.01f, ref threshold); // set FAR to 1% if (similarity > threshold) { match = true; } if (match) { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(userName, new System.Drawing.Font("Arial", 16), new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen), facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format); } break; } } // display current frame pictureBox1.Image = frameImage; GC.Collect(); // collect the garbage after the deletion // make UI controls accessible Application.DoEvents(); } FSDKCam.CloseVideoCamera(cameraHandle); FSDKCam.FinalizeCapturing(); } private void btnRemember_Click(object sender, EventArgs e) { faceTemplates.Clear(); programState = ProgramState.psRemember; label1.Text = "Look at the camera"; } } }
br1.Read
and that error is so so general its hard to help you in any way. – JonH