Exemplo n.º 1
0
        public Image drawFacialFlandmarks(long[] IDs, int tracker, Image frameImage)
        {
            Graphics gr  = Graphics.FromImage(frameImage);
            Pen      pen = new Pen(Brushes.Red);

            pen.Width    = 5.0F;
            pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
            for (int i = 0; i < IDs.Length; ++i)
            {
                FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                FSDK.GetTrackerFacePosition(tracker, 0, IDs[i], ref facePosition);
                FSDK.TPoint[] facialFeatures;
                FSDK.GetTrackerFacialFeatures(tracker, 0, IDs[i], out facialFeatures);
                int          left   = facePosition.xc - (int)(facePosition.w * 0.6);
                int          top    = facePosition.yc - (int)(facePosition.w * 0.5);
                StringFormat format = new StringFormat();

                for (int x = 0; x < facialFeatures.Length; x++)
                {
                    FSDK.TPoint point = facialFeatures[x];
                    gr.FillEllipse(Brushes.DarkBlue, point.x, point.y, 4, 4);

                    GuesGenderAndAge(IDs, i, tracker);
                    personRecognize(IDs, facePosition, left, top, facePosition.w, i, tracker, frameImage);
                }
            }
            return(frameImage);
        }
    public void Update(FSDK.CImage image, Image frameImage, bool recogniseFacialFeatures)
    {
        FSDK.FeedFrame(tracker, 0, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256);
        Array.Resize(ref IDs, (int)faceCount);

        Graphics graphics = Graphics.FromImage(frameImage);

        for (int i = 0; i < IDs.Length; ++i)
        {
            if (recogniseFacialFeatures == true)
            {
                FSDK.TPoint[] facialFeatures;
                FSDK.GetTrackerFacialFeatures(tracker, 0, IDs[i], out facialFeatures);
                foreach (FSDK.TPoint point in facialFeatures)
                {
                    graphics.FillEllipse(Brushes.Blue, point.x, point.y, 5, 5);
                }
            }
            else
            {
                FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                FSDK.GetTrackerFacePosition(tracker, 0, IDs[i], ref facePosition);

                int x = facePosition.xc - (int)(facePosition.w * 0.6);
                int y = facePosition.yc - (int)(facePosition.w * 0.6);
                int w = (int)(facePosition.w * 1.2);

                Pen pen = new Pen(Color.FromArgb(115, 115, 115, 115), 3);

                RectInfo currentRectInfo = new RectInfo(x, y, w);

                currentRectInfo = ProccessRectInfo(currentRectInfo, IDs[i]);

                /*String name;
                 * int res = FSDK.GetAllNames(tracker, IDs[i], out name, 65536);
                 *
                 * if (FSDK.FSDKE_OK == res && name.Length > 0)
                 * {
                 *  StringFormat format = new StringFormat();
                 *  format.Alignment = StringAlignment.Center;
                 *
                 *  graphics.DrawString(name, new System.Drawing.Font("Arial", 16),
                 *  new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                 *  currentRectInfo.x + currentRectInfo.w / 2, currentRectInfo.y + currentRectInfo.w + 5, format);
                 * }*/
                pen = new Pen(Color.FromArgb(115, 115, 115, 115), 3);

                if (!CheckIfOpen())
                {
                    MouseOnRect(currentRectInfo, ref pen, i);
                }

                graphics.DrawRectangle(pen, currentRectInfo.x, currentRectInfo.y, currentRectInfo.w, currentRectInfo.w);
            }
        }
    }
Exemplo n.º 3
0
        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();
            }

            int tracker = 0;

            FSDK.CreateTracker(ref tracker);

            int err = 0; // set realtime face detection parameters

            FSDK.SetTrackerMultipleParameters(tracker, "RecognizeFaces=false; DetectFacialFeatures=true; HandleArbitraryRotations=false; DetermineFaceRotationAngle=false; InternalResizeWidth=100; FaceDetectionThreshold=5;", ref err);

            this.button1.Hide();

            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);

                long[] IDs;
                long   faceCount = 0;
                FSDK.FeedFrame(tracker, 0, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256); // maximum of 256 faces detected
                Array.Resize(ref IDs, (int)faceCount);

                Image    frameImage = image.ToCLRImage();
                Graphics gr         = Graphics.FromImage(frameImage);

                for (int i = 0; i < IDs.Length; ++i)
                {
                    FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                    FSDK.GetTrackerFacePosition(tracker, 0, IDs[i], ref facePosition);

                    FSDK.TPoint[] facialFeatures;
                    FSDK.GetTrackerFacialFeatures(tracker, 0, IDs[i], out facialFeatures);

                    int left = facePosition.xc - (int)(facePosition.w * 0.6);
                    int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                    gr.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition.w * 1.2), (int)(facePosition.w * 1.2));

                    foreach (FSDK.TPoint point in facialFeatures)
                    {
                        gr.FillEllipse(Brushes.DarkBlue, point.x, point.y, 5, 5);
                    }
                }

                // display current frame
                pictureBox1.Image = frameImage;

                GC.Collect(); // collect the garbage after the deletion

                // make UI controls accessible
                Application.DoEvents();
            }
            FSDK.FreeTracker(tracker);

            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }