Exemplo n.º 1
0
        private FaceObject GetRecognizedFaceByID(int ID)
        {
            FaceObject fObj = null;

            foreach (FaceObject f in allRecogFaces)
            {
                if (f.ID == ID)
                {
                    fObj = f;
                    break;
                }
            }
            return(fObj);
        }
Exemplo n.º 2
0
        private void CheckFace(FaceObject fObj)
        {
            if (fObj.ID < 0)
            {
                return;
            }

            bool isNewFace = true;

            foreach (FaceObject f in allRecogFaces)
            {
                if (f.ID == fObj.ID)
                {
                    if (!f.isSameCellLocation(fObj))
                    {
                        Console.WriteLine("Recognized Face: " + fObj.ID + " is moving to another location");
                        GenerateAndSendReport(f.ID, f.gender, f.age, f.cellLocation.X, f.cellLocation.Y, fObj.distance.ToString(), "move");
                        f.UpdateLocation(fObj);
                    }

                    isNewFace = false;
                    break;
                }
            }

            if (isNewFace)
            {
                allRecogFaces.Add(fObj);
                lock (bitmapLock)
                {
                    int numOfPic = 3;
                    this.SaveFaceImage(fObj, numOfPic);
                    String request = this.AGCreateRequest(fObj.ID, numOfPic);
                    this.AGSendRequest(request);
                }

                Console.WriteLine("New Face: " + fObj.ID + " has been added.");
                GenerateAndSendReport(fObj.ID, fObj.gender, fObj.age, fObj.cellLocation.X, fObj.cellLocation.Y, fObj.distance.ToString(), "new");
            }
            else
            {
                if (fObj.isVisible == false)
                {
                    Console.WriteLine("Recognized Face: " + fObj.ID + " is visible.");
                    fObj.isVisible = true;
                    GenerateAndSendReport(fObj.ID, fObj.gender, fObj.age, fObj.cellLocation.X, fObj.cellLocation.Y, fObj.distance.ToString(), "old");
                }
            }
        }
Exemplo n.º 3
0
        private void CreateFaceObject(PXCMFaceData.Face face, ref FaceObject fObj)
        {
            PXCMFaceData.DetectionData detection = face.QueryDetection();
            if (detection != null)
            {
                PXCMRectI32 faceRect = new PXCMRectI32();
                detection.QueryBoundingRect(out faceRect);

                PXCMFaceData.RecognitionData recogData = face.QueryRecognition();
                int userID = -1;
                if (recogData != null)
                {
                    userID = recogData.QueryUserID();
                }

                //Registered Face
                if (userID > 0)
                {
                    //Get Face by USER-ID
                    fObj = this.GetRecognizedFaceByID(userID);

                    //Due to pre-loaded DB, FaceObject can be null
                    if (fObj == null)
                    {
                        fObj = new FaceObject();
                    }
                }

                float faceDistance;
                detection.QueryFaceAverageDepth(out faceDistance);
                faceDistance /= 1000;

                Rectangle rect = new Rectangle(faceRect.x, faceRect.y, faceRect.w, faceRect.h);
                //Point faceLoc = faceCamConfig.GetFaceLoc(rect);
                Point faceLoc = faceCamConfig.Location;

                fObj.ID           = userID;
                fObj.boundingBox  = rect;
                fObj.cellLocation = faceLoc;
                fObj.registerTime = DateTime.Now;
                fObj.lastSeen     = DateTime.Now;
                fObj.distance     = faceDistance;
            }
        }
Exemplo n.º 4
0
        private void DrawFace(FaceObject fObj)
        {
            if (bitmap == null)
            {
                return;
            }

            StringBuilder userLabel = new StringBuilder("ID:");
            Color         faceColor = Color.Red;

            if (fObj.ID > -1)
            {
                faceColor = GetFaceRectColor(fObj.ID);
                userLabel.Append(fObj.ID);
                //userLabel.Append(" " + fObj.gender + fObj.age.ToString());
                //userLabel.Append(" " + fObj.cellLocation.X + "," + fObj.cellLocation.Y);
            }
            else
            {
                userLabel.Append("-");
            }

            StringBuilder userInfo = new StringBuilder("G/A:");

            if (!String.IsNullOrEmpty(fObj.gender) && fObj.age > 0)
            {
                userInfo.Append(fObj.gender + fObj.age);
            }
            else
            {
                userInfo.Append("-");
            }
            userInfo.Append("A:" + fObj.isAligned.ToString());
            userInfo.Append("PYR: " + fObj.pitch.ToString("0.00") + ";" + fObj.yaw.ToString("0.00") + ";" + fObj.roll.ToString("0.00"));
            //userInfo.Append(" Loc:" + fObj.cellLocation.X + "," + fObj.cellLocation.Y);

            //if (fObj.distance > 0)
            //{
            //    userLabel.Append(" " + fObj.distance.ToString("0.00") + "m");
            //}
            //else
            //{
            //    userLabel.Append(" >1m");
            //}

            lock (bitmapLock)
            {
                using (Graphics graphics = Graphics.FromImage(bitmap))
                    using (Pen facePen = new Pen(faceColor, 3.0f))
                        using (Pen redPen = new Pen(Color.Red, 3.0f))
                            using (Brush faceBrush = new SolidBrush(faceColor))
                                using (Brush blackBrush = new SolidBrush(Color.Black))
                                    using (Font font = new Font(FontFamily.GenericMonospace, 12, FontStyle.Bold))
                                    {
                                        Rectangle idLabelRect = new Rectangle(fObj.boundingBox.X, fObj.boundingBox.Y - 25, fObj.boundingBox.Width, 25);
                                        graphics.FillRectangle(faceBrush, idLabelRect);
                                        PointF idLabelLoc = new PointF(fObj.boundingBox.X + 5, fObj.boundingBox.Y - 20);
                                        graphics.DrawString(userLabel.ToString(), font, blackBrush, idLabelLoc);

                                        Rectangle infoLabelRect = new Rectangle(fObj.boundingBox.X, fObj.boundingBox.Y + fObj.boundingBox.Height, fObj.boundingBox.Width, 25);
                                        graphics.FillRectangle(faceBrush, infoLabelRect);
                                        PointF infoLabelLoc = new PointF(infoLabelRect.X + 5, infoLabelRect.Y + 5);
                                        graphics.DrawString(userInfo.ToString(), font, blackBrush, infoLabelLoc);

                                        graphics.DrawRectangle(facePen, fObj.boundingBox);
                                    }
            }
        }
Exemplo n.º 5
0
        private void Analyze(PXCMFaceData data)
        {
            numFacesDetected = data.QueryNumberOfDetectedFaces();
            bool isRegisteringFace = false;

            for (int i = 0; i < numFacesDetected; i++)
            {
                PXCMFaceData.Face face = faceData.QueryFaceByIndex(i);
                if (face != null)
                {
                    FaceObject fObj = new FaceObject();
                    CreateFaceObject(face, ref fObj);

                    //Check landmarks alignment
                    //GetFaceLandmarks(face, out fObj.noseTip, out fObj.leftEye, out fObj.rightEye, out fObj.landmarkDetected);
                    GetFacePose(face, out fObj.pitch, out fObj.yaw, out fObj.roll);
                    CheckFaceAlignment(ref fObj);

                    if (fObj.ID < 0) //Face is NOT recognized
                    {
                        // TODO: Check if the face is registerable

                        if (doRegister && fObj.isAligned)
                        {
                            recogData = face.QueryRecognition();
                            if (recogData != null)
                            {
                                if (isAutoRegister)
                                {
                                    if (failedCount[i] >= RecogCompensation)
                                    {
                                        fObj.ID           = recogData.RegisterUser();
                                        isRegisteringFace = true;
                                        failedCount[i]    = 0;
                                        Console.WriteLine("Registrering Face: " + fObj.ID);
                                    }
                                    else
                                    {
                                        failedCount[i]++;
                                        Console.WriteLine("Registration Failed (" + i + "): " + failedCount[i] + "/" + RecogCompensation);
                                    }
                                }
                                else //Button Triggered Registration
                                {
                                    fObj.ID = recogData.RegisterUser();
                                }
                            }

                            fObj.registerTime = DateTime.Now;
                            fObj.lastSeen     = DateTime.Now;
                        }
                    }
                    else // Face is RECOGNIZED
                    {
                        failedCount[i] = 0;
                    }

                    DrawFace(fObj);
                    CheckFace(fObj);
                }
            }

            if (isRegisteringFace)
            {
                lastRegisterTime = DateTime.Now;
            }

            if (doRegister)
            {
                doRegister = false;
            }

            CheckMissingFace();
        }