读取错误C#

问题描述:

  1. 我正在使用luxand Face SDK开发使用Visual Studio的人脸识别应用程序。我正在尝试修改示例应用程序。在应用程序中,示例被保存在计算机内存中,但我正在尝试将其写入文件并稍后从中读取。代码摘录如下。读取错误C#

  2. 该应用程序运行良好,并保存该文件。但是,当它试图读取文件时,应用程序停止工作,并且出现错误“LiveRecognition_VS2008.exe中发生未处理的异常'System.NullReferenceException'附加信息:未将对象引用设置为对象实例”在这部分代码被高亮显示 “br1.Read(t1.templateData,0,t1.templateData.Length)

  3. 请引导我什么是错误。我正在阅读文件错误?

 
     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"; 
      } 
     } 
    } 

+1

没有在代码做我看到'br1.Read',并且这个错误非常普遍,很难以任何方式帮助你。 – JonH 2012-08-15 13:39:23

+2

是你的发布代码中的序列键? – Mizipzor 2012-08-15 13:44:53

+0

@DavidB将该评论作为答案,您将得到一个赞成票。 – Mizipzor 2012-08-15 13:47:48

t1.templateDatanull。写入时,应首先将长度存储到文件中。然后在阅读时读取大小,创建数组,然后阅读其内容。

尝试是这样的:

using (BinaryWriter bw = new BinaryWriter(fs)) 
{ 
    bw.Write(template.templateData.Length); 
    bw.Write(template.templateData, 0, template.templateData.Length); 
} 



using(BinaryReader br1 = new BinaryReader(fs1)) 
{ 
    int length = br1.ReadInt32(); 
    t1.templateData = new byte[length]; 
    br1.Read(t1.templateData, 0, t1.templateData.Length); 
} 
+0

但我写入文件也使用相同的论点 – user1138880 2012-08-15 13:47:49

+0

可以举一些例子请 – user1138880 2012-08-15 13:53:40

+0

感谢它的工作! – user1138880 2012-08-15 14:34:42

你有一个空的参考 - 去br1.Read(t1.templateData, 0, t1.templateData.Length);,找到哪一个没有被设置(我打赌t1.templateData

+0

是的,没有数据被读入t1.templateData – user1138880 2012-08-15 13:51:29

+0

@ user1138880如果你的'templateData'或'templateData.Length'在创建对象't1'时没有被设置,那么它会给你这个错误。它基本上意味着你没有指向任何东西,程序不能在没有任何工作的情况下工作! – 2012-08-15 13:53:05