Software Archive
Read-only legacy content
17061 Discussions

Face recognition - Database not working correct

ETren
Novice
216 Views

Hello!

I have a problem with my face recognition. It should save the face to the DB I created in an external file but it won't recognize me after a restart.

I hope you can help me.

using System;
using System.IO;
using System.Threading;

namespace Aibo
{
    class Program
    {

        #region vars
        static private PXCMSenseManager sm;
        static private PXCMFaceConfiguration.RecognitionConfiguration rcfg;
        static private PXCMFaceData dface;
        static private PXCMFaceData.RecognitionData rd;
        private const string dbn = "UserDB";
        private const string dbf = "database.bin";
        static String userId;
        static String facerecon;
        static Boolean known;
        #endregion
        
                static pxcmStatus OnModuleProcessedFrame(int mid, PXCMBase module, PXCMCapture.Sample sample)
        {
            if (mid == PXCMFaceModule.CUID)
            {
                PXCMFaceModule faces = module.QueryInstance<PXCMFaceModule>();
                if (dface != null)
                {
                    dface.Update();
                    PXCMFaceData.DetectionData ddata = dface.QueryFaceByID(0).QueryDetection();
                    PXCMRectI32 rect;
                    ddata.QueryBoundingRect(out rect);

                    Int32 nfaces = dface.QueryNumberOfDetectedFaces();
                    for (Int32 i = 0; i < nfaces; i++)
                    {
                        dface.Update();
                        PXCMFaceData.Face face = dface.QueryFaceByIndex(i);
                        if (face != null)
                        {
                            dface.Update();
                            rd = face.QueryRecognition();
                            if (rd.IsRegistered())
                            {
                                userId = Convert.ToString(rd.QueryUserID());
                                System.Diagnostics.Debug.WriteLine("Face recognized " + userId);
                                facerecon = "Face recognized " + userId;
                                known = true;
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("Face not recognized " + userId);
                                facerecon = "Face not recognized " + userId;
                                known = false;
                            }
                        }
                    }
                }
            }   
            return pxcmStatus.PXCM_STATUS_NO_ERROR;
        }


        static void Main(string[] args)
        {

            ConfigureRealSense();
        }

       static private void ConfigureRealSense()
        {
            sm = PXCMSenseManager.CreateInstance();
            sm.EnableFace();
            if (sm == null)
            {
                System.Diagnostics.Debug.WriteLine("Failed to create sense manager");
            }

            PXCMFaceModule face = sm.QueryFace();
            dface = face.CreateOutput();
            PXCMSenseManager.Handler handler = new PXCMSenseManager.Handler
            {
                onModuleProcessedFrame = OnModuleProcessedFrame
            };

            PXCMFaceConfiguration cfg = face.CreateActiveConfiguration();
            cfg.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR_PLUS_DEPTH);
            cfg.EnableAllAlerts();

            rcfg = cfg.QueryRecognition();
            rcfg.Enable();
            rcfg.SetRegistrationMode(PXCMFaceConfiguration.RecognitionConfiguration.RecognitionRegistrationMode.REGISTRATION_MODE_CONTINUOUS);
            cfg.ApplyChanges();
            LoadDatabaseFromFile();
            sm.Init(handler);
            sm.StreamFrames(true);

            cfg.Dispose();
            face.Dispose();
        }

        static private void LoadDatabaseFromFile()
        {
            dface.Update();
            if (File.Exists(dbf))
            {
                Byte[] buffer = File.ReadAllBytes(dbf);
                rcfg.SetDatabaseBuffer(buffer);
                System.Diagnostics.Debug.WriteLine("DB loaded");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("DB not found");
            }
        }

        static private void SaveDatabaseToFile()
        {
            dface.Update();
            PXCMFaceData.RecognitionModuleData rmd = dface.QueryRecognitionModule();
            Int32 nBytes = rmd.QueryDatabaseSize();
            Byte[] buffer = new Byte[nBytes];

            rmd.QueryDatabaseBuffer(buffer);

            File.WriteAllBytes(dbf, buffer);
            System.Diagnostics.Debug.WriteLine("DB saved");
        }

        private void DeleteDatabaseFile()
        {
            if (File.Exists(dbf))
            {
                File.Delete(dbf);
                System.Diagnostics.Debug.WriteLine("DB deleted");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("DB not found");
            }
        }

        static void OnProcessExit(object sender, EventArgs e)
        {
            dface.Dispose();
            sm.Dispose();
        }

    }
}

 

0 Kudos
1 Reply
Xusheng_L_Intel
Employee
216 Views

Eric, I will look at your code and be back to you soon. Thanks for supporting RealSense SDK.

0 Kudos
Reply