2012-08-15 137 views
1
  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

回答

2

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

1

你有一個空的參考 - 去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