Example #1
0
        private void initSDK(Boolean isCore)
        {
            logger.Info("Init FR engine id = " + id);
            //ci = new CultureInfo("en-US");
            // calculate processing time
            Stopwatch watch = new Stopwatch();

            watch.Start();

            long aRam = Process.GetCurrentProcess().PrivateMemorySize64;

            fdre = null;
            //Tool.logDebug("Loading Analytic Engine");
            try
            {
                fdre = new BetafaceFDRE();
            }
            catch (Exception n)
            {
                logger.Error("Failed to load Analytic Engine, message = " + n.Message);
                Environment.FailFast("Failed to load Analytic Engine, message = " + n.Message, n);
                return;
            };

            if (null == fdre)
            {
                logger.Error("Failed to load Analytic Engine. Analytic Engine object is NULL, exiting!");
                Environment.Exit(-1);
                return;
            }
            ;

            try
            {
                if (fdre.Init())
                {
                    logger.Info("Analytic Engine initialized");
                }
                else
                {
                    //MessageBoxCloser.CloseAll();
                    logger.Error("Fail to initialize Analytic Engine. No license file exist or wrong license file, exiting!");
                    Environment.Exit(-1);
                    return;
                };
            }
            catch (Exception e)
            {
                //MessageBoxCloser.CloseAll();
                logger.Error("Fail to initialize Analytic Engine. No license file exists or wrong license file, exiting!" + e.Message);
                Environment.Exit(-1);
                return;
            }

            long bRam = Process.GetCurrentProcess().PrivateMemorySize64;

            logger.Info("FR engine RAM: " + (bRam - aRam));
            //WrapperAgent.LogInfo(className, "FR engine RAM: " + (bRam - aRam));

            // engine parameters
            dSettings.flags                  = iFlags;
            dSettings.iMaxImageWidthPix      = 640;  //BFFRProperties.MaxImageWidthPix;
            dSettings.iMaxImageHeightPix     = 480;  //BFFRProperties.MaxImageHeightPix;
            dSettings.dMinFaceSizeOnImage    = 0.05; //BFFRProperties.MinFaceSizeOnImage;
            dSettings.iMinFaceSizePix        = 20;   //BFFRProperties.MinFaceSizePix;
            dSettings.dAngleDegrees          = 0.0;  //BFFRProperties.AngleDegrees;
            dSettings.dAngleToleranceDegrees = 30.0; //BFFRProperties.AngleToleranceDegrees;
            dSettings.dMinDetectionScore     = 0.6;  //BFFRProperties.MinDetectionScore;
#if false
            if (isCore)
            {
                lock (lockThis)
                {
                    // init in-memory suspect database
                    blacklistKeysinMemory.Clear();
                }
                logger.Info("Load keys into engine [ " + id + " ] memory.");
                DatabaseHelper.getInstance().addUpdateHandler(id, updateKeys);
            }

            match_indexes = new int[numOfCandidate];
            match_scores  = new double[numOfCandidate];
            is_match      = new int[numOfCandidate];
#endif
            // calculate init engine time
            watch.Stop();
            logger.Info("Init engine time = [ " + watch.Elapsed.TotalSeconds + "sec ]");
            //WrapperAgent.LogInfo(className, "Init engine time = [ " + watch.Elapsed.TotalSeconds + "sec ]");
        }
Example #2
0
        private Tuple <byte[], double, string, double, string, string> parseResult(ref BetafaceFDRE fdre, Int64 srcImage, Int64 detectionResult, int index, string image_filename)
        {
            // 1. Get face info
            Int64 faceInfo = 0;
            //Tool.logDebug("[parseResult]");
            int returnValue = fdre.Betaface_GetFaceInfo(fdre.GetState(), detectionResult, index, out faceInfo);

            if (returnValue == BetafaceConsts.BETAFACE_OK)
            {
                // 2. Get face score
                double detectionScore = -1;
                returnValue = fdre.Betaface_GetFaceInfoDoubleParam(fdre.GetState(), faceInfo, BetafaceConsts.BETAFACE_FEATURE_FACE | BetafaceConsts.BETAFACE_PARAM_SCORE, out detectionScore);
                if (returnValue == BetafaceConsts.BETAFACE_OK)
                {
                    //TODO do something?
                }
                else
                {
                    logger.Error("Image: [ " + image_filename + " ], Get face score failed, message = " + returnValue);
                }

                // 3. Get face keyStopwatch watch = new Stopwatch();
                Stopwatch keyStopwatch = new Stopwatch();
                keyStopwatch.Start();

                int    KeyLen       = 0;
                IntPtr faceKey      = IntPtr.Zero;
                byte[] faceKeyBytes = null;
                returnValue = fdre.Betaface_GenerateFaceKey(fdre.GetState(), srcImage, faceInfo, BetafaceConsts.BETAFACE_RECKEY_DEFAULT, out faceKey, out KeyLen);
                if (returnValue == BetafaceConsts.BETAFACE_OK)
                {
                    faceKeyBytes = new byte[KeyLen];
                    Marshal.Copy(faceKey, faceKeyBytes, 0, KeyLen);
                    fdre.Betaface_ReleaseFaceKey(fdre.GetState(), ref faceKey); // release key
                }
                else
                {
                    logger.Error("Image: [ " + image_filename + " ], Get face key failed, message = " + returnValue);
                }
                //Tool.logDebug("[Betaface_GenerateFaceKey]");
                keyStopwatch.Stop();
                logger.Info("Image: [ " + image_filename + " ], Key index generated time: [ " + keyStopwatch.Elapsed.TotalSeconds + " sec ]");

                // 4. Get crop face image
                //Int64 faceImage = 0;
                //Int64 croppedfaceinfo = 0;
                //returnValue = fdre.Betaface_CropFaceImage(fdre.GetState(), srcImage, faceInfo, ruleProps.distanceBetweenEyes, ruleProps.distanceFromTopToEyeline, ruleProps.deRotate, ruleProps.outputFaceWidth, ruleProps.outputFaceHeight, ruleProps.noUpScale, ruleProps.blackBackgroundColor, out faceImage, out croppedfaceinfo);
                //if (returnValue == BetafaceConsts.BETAFACE_OK)
                //{
                //    fdre.Betaface_ReleaseFaceInfo(fdre.GetState(), ref croppedfaceinfo); // release cropped face info
                //}
                //else
                //{
                //    Tool.logError("Get crop face failed, message = " + returnValue);
                //}

                // 5. Do recognition
                Tuple <string, double, string, string> suspect = null;
#if false
                lock (lockThis)
                {
                    if (blacklistKeys.Count > 0)
                    {
                        suspect = recognition(ref fdre, faceKeyBytes);
                    }
                }
#endif
                // 6. Prepare result
                //Tuple<byte[], Int64, double, string, double, string, string> face = new Tuple<byte[], Int64, double, string, double, string, string>(faceKeyBytes, faceImage, detectionScore, suspect.Item1, suspect.Item2, suspect.Item3, suspect.Item4);
                Tuple <byte[], double, string, double, string, string> face = null;
                if (suspect != null)
                {
                    face = new Tuple <byte[], double, string, double, string, string>(faceKeyBytes, detectionScore, suspect.Item1, suspect.Item2, suspect.Item3, suspect.Item4);
                }
                else
                {
                    face = new Tuple <byte[], double, string, double, string, string>(faceKeyBytes, detectionScore, "", -1, null, "");
                }
                fdre.Betaface_ReleaseFaceInfo(fdre.GetState(), ref faceInfo); // release face info
                return(face);
            }
            else
            {
                logger.Warn("Image: [ " + image_filename + " ], Get face info failed, message = " + returnValue);
                return(null);
            }
        }
Example #3
0
        private Tuple <string, double, string, string> recognition(ref BetafaceFDRE fdre, byte[] candidateKey)
        {
            // calculate processing time
            //Stopwatch watch = new Stopwatch();
            //watch.Start();
            // load in-memory suspect database
            long state = 0;

            fdre.Betaface_SearchIndex_Init(blacklistKeys.Count, ref state, blacklistKeysinMemory[0]);
            //Tool.logDebug("[recognition] blacklistKeys.Count = " + blacklistKeys.Count);
            for (int index = 0; index < blacklistKeys.Count; index++)
            {
                //Tool.logDebug("[recognition] index = " + index);
                fdre.Betaface_SearchIndex_SetKey(state, index, blacklistKeysinMemory[index]);
            }

            // do recognition
            int nmatches = Math.Min(numOfCandidate, blacklistKeys.Count);

            //Tool.logDebug("[recognition] nmatches = " + nmatches);

            fdre.Betaface_SearchIndex_Search(state, candidateKey, 0, blacklistKeys.Count, nmatches, 0, default_false_alarm, match_indexes, match_scores, is_match);
            string suspectID = "";
            //string suspectName = "n/a";
            double suspectScore = -1;
            string suspectKey   = "n/a";
            string filename     = Path.GetFileNameWithoutExtension(blacklistKeys[match_indexes[0]]);

            string[] tokens = filename.Split('-');

            if (match_scores[0] > RuleProperties.ThresholdScore)
            {
                suspectID    = tokens[0];
                suspectScore = match_scores[0];
                suspectKey   = blacklistKeys[match_indexes[0]];
                //Tool.logDebug("Recognition : suspect found *******************************");
                //Tool.logDebug("Recognition : suspect found *****[ " + suspectID + " ]*****");
                //Tool.logDebug("Recognition : suspect found *******************************");
            }

            //Tool.logDebug("[recognition] -- 2");
            //Tool.logDebug("Recognition : candicate suspect found *******************************");
            // TODO, new a candicate json array
            JObject jobj = new JObject();

            for (int m = 0; m < nmatches; m++)
            {
                string   candicateFilename = Path.GetFileNameWithoutExtension(blacklistKeys[match_indexes[m]]);
                string[] candicateTokens   = candicateFilename.Split('-');

                string candicateSuspectID    = candicateTokens[0];
                double candicateSuspectScore = match_scores[m];

                //Tool.logDebug("Recognition : candicate suspect found *****[ " + candicateSuspectID + ", score: " + candicateSuspectScore + " ]*****");

                // TODO, add candidate json object
                JProperty jprop = new JProperty(candicateSuspectID, candicateSuspectScore);
                jobj.Add(jprop);
            }
            // TODO, translate candidate json array to string
            string candidates = JsonConvert.SerializeObject(jobj);

            //Tool.logDebug("[recognition] -- 3");
            //Tool.logDebug("Recognition : candicate suspect found *******************************");

            // release recognition engine
            fdre.Betaface_SearchIndex_Deinit(ref state);

            // calculate processing time
            //watch.Stop();
            //Tool.logDebug("[recognition] : suspectID [ " + suspectID + " ], suspectImg [ " + tokens[1] + " ], suspectScore [ " + match_scores[0] + "/" + ruleProps.recognitionScoreThreshold + " ], suspectKey [ " + blacklistKeys[match_indexes[0]] + " ], processing time = [ " + watch.Elapsed.TotalSeconds + "sec ], size [ " + blacklistKeys.Length + " ]");
            //Tool.logDebug("[recognition] -- 4");

            return(Tuple.Create(suspectID, suspectScore, suspectKey, candidates));
        }
Example #4
0
        private Collection <Tuple <byte[], double, string, double, string, string> > detection(ref BetafaceFDRE fdre, string image_filename)
        {
            // calculate processing time
            Stopwatch watch = new Stopwatch();

            watch.Start();

            Collection <Tuple <byte[], double, string, double, string, string> > faces = new Collection <Tuple <byte[], double, string, double, string, string> >();

            // 1. LOAD SOURCE IMAGE
            int   returnValue = 0;
            Int64 srcImage    = 0;

            returnValue = fdre.Betaface_LoadImage(fdre.GetState(), image_filename, out srcImage);
            //Tool.logDebug("[detection] : load image = [ " + image_filename + " ]");
            if (returnValue == BetafaceConsts.BETAFACE_OK)
            {
                // 2. DETECT FACE
                Int64 detectionResult = 0;
                int   facesCount      = 0;

                Stopwatch bfDetectFacesWatch = new Stopwatch();
                bfDetectFacesWatch.Start();

                returnValue = fdre.Betaface_DetectFaces(fdre.GetState(), srcImage, dSettings, out facesCount, out detectionResult);

                bfDetectFacesWatch.Stop();
                logger.Info("Image: [ " + image_filename + " ], Face detection time: [ " + bfDetectFacesWatch.Elapsed.TotalSeconds + " sec ]");

                if (returnValue == BetafaceConsts.BETAFACE_OK)
                {
                    // 3. HANDLE RESULT FACE BY FACE
                    for (int i = 0; i < facesCount; i++)
                    {
                        Tuple <byte[], double, string, double, string, string> face = parseResult(ref fdre, srcImage, detectionResult, 0, image_filename);
                        faces.Add(face);
                    }

                    // 2. RELEASE RESOURCE
                    fdre.Betaface_ReleaseDetectionResult(fdre.GetState(), ref detectionResult);//releas detection result
                }
                else
                {
                    logger.Warn("Image: [ " + image_filename + " ], DetectFaces failed, message = " + returnValue);
                }

                // 1. RELEASE RESOURCE
                fdre.Betaface_ReleaseImage(fdre.GetState(), ref srcImage); //releas srcImage (1)
            }
            else
            {
                logger.Warn("Image: [ " + image_filename + " ], LoadImage failed, message = " + returnValue);
                return(null);
            }

            // calculate processing time
            watch.Stop();
            logger.Info("Image: [ " + image_filename + " ], Detection and recognition time: [ " + watch.Elapsed.TotalSeconds + " sec ]");

            //Tool.logDebug("[detection] : face count [ " + faces.Count + " ]");
            return(faces);
        }