Beispiel #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            button2.Enabled      = true;
            int cameraHandle = 0;

            needClose = false;

            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;

            if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
            {
                FSDK.CreateTracker(ref tracker);
            }

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

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

            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();
                if (checkBox1.Checked)
                {
                    pictureBox1.Image = drawFacialFlandmarks(IDs, tracker, frameImage);
                }
                else
                {
                    pictureBox1.Image = frameImage;
                }
                GC.Collect();
                Application.DoEvents();
            }

            FSDK.SaveTrackerMemoryToFile(tracker, TrackerMemoryFile);
            FSDK.FreeTracker(tracker);
            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }
Beispiel #2
0
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            int image = 0;

            while (camIsGood)
            {
                if (progState == PROG_STATE.CAPTURED)
                {
                    Thread.Sleep(4);
                    continue;
                }

                FSDKCam.GrabFrame(cameraHandle, ref image);
                var cimg = new FSDK.CImage(image);
                worker.ReportProgress(0, cimg.Copy().ToCLRImage());
                if (progState == PROG_STATE.CAPTURING)
                {
                    var imgPath = Path.GetFullPath(Path.Combine("Captures", Guid.NewGuid().ToString() + ".jpg"));
                    cimg.Save(imgPath);
                    worker.ReportProgress(1, imgPath);
                }
                cimg.Dispose();
                FSDK.FreeImage(image);
                GC.Collect();
                Thread.Sleep(16);
            }

            FSDKCam.CloseVideoCamera(cameraHandle);
        }
        int Normalhandle()
        {
            try
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return(-1);
                }
                FSDK.CImage Image      = new FSDK.CImage(ImageHandle);
                Image       FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);
                pictureBox1.Image = FrameImage;

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();

                for (int person = 0; person < FacePosition.Length; person++)
                {
                    draw(FacePosition[person]);
                }

                GC.Collect(); // collect the garbage after the deletion
                return(0);
            }
            catch
            {
                return(-1);
            }
        }
Beispiel #4
0
    public void StartStreaming(bool recogniseFacialFeatures, Point location)
    {
        if (FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle) != FSDK.FSDKE_OK)
        {
            MessageBox.Show(Constants.CAMERA_OPEN_ERROR, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }

        FaceRecognitionController faceRecognitionController = new FaceRecognitionController();

        faceRecognitionController.Initialize(location);

        cameraWorking = true;

        while (cameraWorking)
        {
            Application.DoEvents();
            Int32 imageHandle = 0;
            FSDKCam.GrabFrame(cameraHandle, ref imageHandle);
            FSDK.CImage image      = new FSDK.CImage(imageHandle);
            Image       frameImage = image.ToCLRImage();

            faceRecognitionController.Update(image, frameImage, recogniseFacialFeatures);

            currentPictureBox.Image = frameImage;
            GC.Collect();
        }

        faceRecognitionController.Stop();

        FSDKCam.CloseVideoCamera(cameraHandle);
        FSDKCam.FinalizeCapturing();
    }
Beispiel #5
0
        public InputName(FSDK.CImage Photo)
        {
            InitializeComponent();

            facePhoto = Photo;
            Image frameImage = Photo.ToCLRImage();

            picBox.Image = 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);
            }
        }
    }
Beispiel #7
0
        public InputName(TFaceRecord Photo)
        {
            InitializeComponent();

            RealPhoto = Photo;
            facePhoto = Photo.faceImage;
            Image frameImage = Photo.faceImage.ToCLRImage();

            picBox.Image = frameImage;
        }
        private void Form1_Shown(object sender, EventArgs e)
        {
            int tracker = 0;

            FSDK.CreateTracker(ref tracker);

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

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

            while (!needClose)
            {
                if (CameraOpened)
                {
                    Int32 imageHandle = 0;
                    if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera
                    {
                        MessageBox.Show("Error in FSDK_GrabFrame", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        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);

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

                    // display current frame
                    pictureBox1.Image = frameImage;
                }
                GC.Collect(); // collect the garbage after the deletion

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

            if (CameraOpened)
            {
                FSDKCam.CloseVideoCamera(cameraHandle);
            }
        }
Beispiel #9
0
        private static FSDK.TPoint[] GetFeaturePoints(Image sourceImage)
        {
            FSDK.TPoint[] pointFeature = null;
            var           image        = new FSDK.CImage(new Bitmap(sourceImage));

            var faceRectangle = Rectangle.Empty;
            var facePosition  = image.DetectFace();

            if (0 == facePosition.w)
            {
                return(pointFeature);
            }

            pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition);
            return(pointFeature);
        }
        private void btnOpenPhoto_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    FSDK.CImage image = new FSDK.CImage(openFileDialog1.FileName);

                    // resize image to fit the window width
                    double ratio = System.Math.Min((pictureBox1.Width + 0.4) / image.Width,
                                                   (pictureBox1.Height + 0.4) / image.Height);
                    image = image.Resize(ratio);

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

                    FSDK.TFacePosition facePosition = image.DetectFace();
                    if (0 == facePosition.w)
                    {
                        MessageBox.Show("No faces detected", "Face Detection");
                    }
                    else
                    {
                        int left = facePosition.xc - (int)(facePosition.w * 0.6f);
                        int top  = facePosition.yc - (int)(facePosition.w * 0.5f);
                        gr.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition.w * 1.2), (int)(facePosition.w * 1.2));

                        FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition);
                        int           i = 0;
                        foreach (FSDK.TPoint point in facialFeatures)
                        {
                            gr.DrawEllipse((++i > 2) ? Pens.LightGreen : Pens.Blue, point.x, point.y, 3, 3);
                        }

                        gr.Flush();
                    }

                    // display image
                    pictureBox1.Image = frameImage;
                    pictureBox1.Refresh();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                }
            }
        }
Beispiel #11
0
    public bool Register(DataTable dt)
    {
        Int32 imageHandle = 0;

        FSDKCam.GrabFrame(cameraHandle, ref imageHandle);
        FSDK.CImage image      = new FSDK.CImage(imageHandle);
        Image       frameImage = image.ToCLRImage();

        Application.DoEvents();

        List <SqlParameter> sqlParams = new List <SqlParameter>();

        sqlParams.Add(new SqlParameter("Image", DataController.Instance().ConvertImageToByteArray(frameImage)));
        sqlParams.Add(new SqlParameter("Id", dt.Rows[0]["Id"]));
        DataController.Instance().ExecSP("StoreImage", sqlParams);

        return(faceRecognitionController.Register(image, dt));
    }
Beispiel #12
0
 void drawEyes(object objc)
 {
     try
     {
         object[]           obj          = (object[])objc;
         int                imageHandle  = (int)obj[0];
         FSDK.TFacePosition facePosition = (FSDK.TFacePosition)obj[1];
         Graphics           gr           = (Graphics)obj[2];
         FSDK.TPoint[]      eye          = new FSDK.CImage(imageHandle).DetectEyesInRegion(ref facePosition);
         byte[]             eyes         = new byte[0];
         FSDK.GetFaceTemplateUsingEyes(imageHandle, ref eye, out eyes);
         gr.DrawRectangle(Pens.Pink, eye[0].x, eye[0].y, 20f, 10f);
         gr.DrawRectangle(Pens.Pink, eye[1].x, eye[1].y, 20f, 10f);
     }
     catch (Exception)
     {
         //who sai
     }
 }
 public bool Register(FSDK.CImage image, DataTable dt)
 {
     FSDK.FeedFrame(tracker, 0, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256);
     Array.Resize(ref IDs, (int)faceCount);
     if (faceCount == 0)
     {
         MessageBox.Show(Constants.NO_FACE_ERROR);
         return(false);
     }
     else if (faceCount > 1)
     {
         MessageBox.Show(Constants.MULTIPLE_FACES_ERROR);
         return(false);
     }
     else
     {
         string id = Convert.ToString(dt.Rows[0]["Id"]);
         FSDK.SetName(tracker, IDs[0], id);
         return(true);
     }
 }
Beispiel #14
0
        public void nhanDien()
        {
            int CamXuLy = 0;
            int r       = FSDKCam.OpenVideoCamera(ref TenCamera, ref CamXuLy);

            if (r != FSDK.FSDKE_OK)
            {
                MessageBox.Show("Error opening the first camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            int tracker = 0;                                                                     //tạo tracker

            if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
            {
                FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
            }
            int err = 0;                                                                         // set realtime face detection parameters

            FSDK.SetTrackerMultipleParameters(tracker,
                                              "HandleArbitraryRotations=false; DetermineFaceRotationAngle=false; InternalResizeWidth=100; FaceDetectionThreshold=3;",
                                              ref err);

            while (!needClose)
            {
                Int32 xuLyAnh = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CamXuLy, ref xuLyAnh)) // grab the current frame from the camera
                {
                    Application.DoEvents();
                    continue;
                }

                FSDK.CImage image = new FSDK.CImage(xuLyAnh); //new FSDK.CImage(imageHandle);
                //Image img = Image.FromFile("E:\\10257275_557036397739997_2637418459313670467_o.jpg");
                //Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                //Graphics g = Graphics.FromImage((Image)b);

                //g.DrawImage(img, 0, 0, pictureBox1.Width, pictureBox1.Height);
                //g.Dispose();

                ///FSDK.CImage image = new FSDK.CImage(b);
                //FSDK.CImage image = new FSDK.CImage("E:\\10257275_557036397739997_2637418459313670467_o.jpg");

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

                // make UI controls accessible (to find if the user clicked on a face)
                Application.DoEvents();
                //Image.
                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);
                    Console.Write(IDs.Length.ToString());
                    int left = facePosition.xc - (int)(facePosition.w * 0.6);
                    int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                    int w    = (int)(facePosition.w * 1.2);

                    String name;
                    int    res = FSDK.GetAllNames(tracker, IDs[i], out name, 65536); // maximum of 65536 characters

                    if (FSDK.FSDKE_OK == res && name.Length > 0)
                    { // draw name
                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Center;

                        gr.DrawString(name, new System.Drawing.Font("Arial", 16),
                                      new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                      facePosition.xc, top + w + 5, format);
                    }

                    Pen pen = Pens.LightGreen;
                    if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                    {
                        pen = Pens.Blue;
                        if (ProgramState.psRemember == programState)
                        {
                            if (FSDK.FSDKE_OK == FSDK.LockID(tracker, IDs[i]))
                            {
                                // get the user name
                                NhapTen inputName = new NhapTen();
                                if (DialogResult.OK == inputName.ShowDialog())
                                {
                                    Ten = inputName.Ten;
                                    //if (Ten == null || Ten.Length == 0)
                                    //{
                                    //    String s = "";
                                    //    FSDK.SetName(tracker, IDs[i], "");
                                    //    FSDK.PurgeID(tracker, IDs[i]);
                                    //}

                                    FSDK.SetName(tracker, IDs[i], Ten);
                                    FSDK.UnlockID(tracker, IDs[i]);
                                }
                            }
                        }
                    }
                    gr.DrawRectangle(pen, left, top, w, w);
                }
                programState = ProgramState.psRecognize;

                // display current frame
                pictureBox1.Image = frameImage;
                GC.Collect(); // collect the garbage after the deletion
            }
            FSDK.SaveTrackerMemoryToFile(tracker, TrackerMemoryFile);
            FSDK.FreeTracker(tracker);
            FSDKCam.CloseVideoCamera(CamXuLy);
            FSDKCam.FinalizeCapturing();
        }
        public bool Recognize(ref string path, bool needCrop)
        {
            var AppDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Abalone", "TestRotator");

            FaceRectRelative = RectangleF.Empty;
            LeftEyeCenter    = RightEyeCenter = LeftMouth = LeftNose = RightNose = RightMouth = Vector2.Zero;

            var executablePath = Path.GetDirectoryName(Application.ExecutablePath);

            FSDK.TPoint[] pointFeature;
            var           image = new FSDK.CImage(path);

            var faceRectangle  = Rectangle.Empty;
            var mouthRectangle = Rectangle.Empty;

            FSDK.SetFaceDetectionThreshold(5);
            FSDK.SetFaceDetectionParameters(true, true, 512);
            var facePosition = image.DetectFace();

            if (0 == facePosition.w)
            {
                MessageBox.Show("No faces detected", "Face Detection");
                return(false);
            }

            if (needCrop)
            {
                RotatedAngle = facePosition.angle;      // угол, на который повернута голова.
            }
            pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition);

            var left = facePosition.xc - (int)(facePosition.w * 0.6f);

            left = left < 0 ? 0 : left;
            //   int top = facePosition.yc - (int)(facePosition.w * 0.5f);             // верхушку определяет неправильлно. поэтому просто не будем обрезать :)
            BottomFace = new Vector2(pointFeature[11].x, pointFeature[11].y);

            var distance = pointFeature[2].y - pointFeature[11].y;
            var top      = pointFeature[16].y + distance - 15;     // определение высоты по алгоритму старикана

            top = top < 0 ? 0 : top;

            var newWidth = (int)(facePosition.w * 1.2);

            newWidth = newWidth > image.Width || newWidth == 0 ? image.Width : newWidth;

            faceRectangle = new Rectangle(left, top, newWidth, BottomFace.Y + 15 < image.Height ? (int)(BottomFace.Y + 15) - top : image.Height - top - 1);
            if (needCrop)       // если это создание проекта - то нужно обрезать фотку и оставить только голову
            {
                var bmpImage = new Bitmap(path);

                using (var croppedImage = ImageEx.Crop(bmpImage, faceRectangle))
                {
                    path = AppDataDir;
                    FolderEx.CreateDirectory(path);
                    path = Path.Combine(path, "tempHaarImage.jpg");
                    croppedImage.Save(path, ImageFormat.Jpeg);

                    return(Recognize(ref path, false));
                }
            }

            LeftEyeCenter  = new Vector2(pointFeature[0].x, pointFeature[0].y);
            RightEyeCenter = new Vector2(pointFeature[1].x, pointFeature[1].y);

            LeftMouth  = new Vector2(pointFeature[3].x, pointFeature[3].y);
            RightMouth = new Vector2(pointFeature[4].x, pointFeature[4].y);

            LeftNose  = new Vector2(pointFeature[45].x, pointFeature[45].y);
            RightNose = new Vector2(pointFeature[46].x, pointFeature[46].y);

            TopFace     = new Vector2(pointFeature[66].x, pointFeature[66].y);
            MiddleFace1 = new Vector2(pointFeature[66].x, pointFeature[66].y);
            MiddleFace2 = new Vector2(pointFeature[5].x, pointFeature[5].y);


            RightMiddleFace1 = new Vector2(pointFeature[67].x, pointFeature[67].y);
            RightMiddleFace2 = new Vector2(pointFeature[6].x, pointFeature[6].y);

            #region Поворот фотки по глазам!

            var v = new Vector2(LeftEyeCenter.X - RightEyeCenter.X, LeftEyeCenter.Y - RightEyeCenter.Y);
            v.Normalize();      // ПД !
            var xVector = new Vector2(1, 0);

            var xDiff = xVector.X - v.X;
            var yDiff = xVector.Y - v.Y;
            var angle = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;

            if (Math.Abs(angle) > 1 && angleCount <= 5)                // поворачиваем наклоненные головы
            {
                ++angleCount;

                using (var ms = new MemoryStream(File.ReadAllBytes(path))) // Don't use using!!
                {
                    var originalImg = (Bitmap)Image.FromStream(ms);

                    path = AppDataDir;
                    FolderEx.CreateDirectory(path);
                    path = Path.Combine(path, "tempHaarImage.jpg");

                    using (var ii = ImageEx.RotateImage(new Bitmap(originalImg), (float)-angle))
                        ii.Save(path, ImageFormat.Jpeg);
                }

                return(Recognize(ref path, false));
            }

            #endregion

            var upperUpperLip = pointFeature[54];       // вехняя точка верхней губы
            var lowerUpperLip = pointFeature[61];       // нижняя точка верхней губы
            var lowerLip      = pointFeature[64];       // верхняя точка нижней губы

            var diff2 = Math.Abs(lowerUpperLip.y - upperUpperLip.y);
            var diffX = Math.Abs(lowerLip.y - lowerUpperLip.y);

            IsOpenSmile = diffX > diff2;            // экспериментальным путем выяснено, что улыбка на 90% открытая, если расстояние между верхней и нижней губой больше, чем толщина верхней губы

            #region Переводим в относительные координаты

            LeftMouth  = new Vector2(LeftMouth.X / (image.Width * 1f), LeftMouth.Y / (image.Height * 1f));
            RightMouth = new Vector2(RightMouth.X / (image.Width * 1f), RightMouth.Y / (image.Height * 1f));

            LeftEyeCenter  = new Vector2(LeftEyeCenter.X / (image.Width * 1f), LeftEyeCenter.Y / (image.Height * 1f));
            RightEyeCenter = new Vector2(RightEyeCenter.X / (image.Width * 1f), RightEyeCenter.Y / (image.Height * 1f));

            LeftNose  = new Vector2(LeftNose.X / (image.Width * 1f), LeftNose.Y / (image.Height * 1f));
            RightNose = new Vector2(RightNose.X / (image.Width * 1f), RightNose.Y / (image.Height * 1f));


            TopFace     = new Vector2(TopFace.X / (image.Width * 1f), TopFace.Y / (image.Height * 1f));
            MiddleFace1 = new Vector2(MiddleFace1.X / (image.Width * 1f), MiddleFace1.Y / (image.Height * 1f));
            MiddleFace2 = new Vector2(MiddleFace2.X / (image.Width * 1f), MiddleFace2.Y / (image.Height * 1f));
            BottomFace  = new Vector2(BottomFace.X / (image.Width * 1f), BottomFace.Y / (image.Height * 1f));

            RightMiddleFace1 = new Vector2(RightMiddleFace1.X / (image.Width * 1f), RightMiddleFace1.Y / (image.Height * 1f));
            RightMiddleFace2 = new Vector2(RightMiddleFace2.X / (image.Width * 1f), RightMiddleFace2.Y / (image.Height * 1f));

            FacialFeatures = new List <Vector3>();
            RealPoints     = new List <Vector2>();
            int index       = 0;
            var pointDepths = GetPointDepths();
            foreach (var point in pointFeature)
            {
                FacialFeatures.Add(new Vector3(point.x / (image.Width * 1f), point.y / (image.Height * 1f), pointDepths[index++]));
                RealPoints.Add(new Vector2(point.x, point.y));
            }

            ImageWidth  = image.Width;
            ImageHeight = image.Height;

            #endregion

            return(true);
        }
Beispiel #16
0
        void camera()
        {
            // activate face sdk before using it...
            FSDK.ActivateLibrary("ANj63QzeUGKbORKF7KmC+s5J0f8hF7moXNMr1QrCeFStmCw3DTYD55rPZOERChnfpSbr3TguoGSPOPdrTwOodvoDuCeE3Jp/18G1GSeyvZT/uqK6q9MtvgSHtNFpna2sHVTdb1Az2rXxy8mHOOBgZ/PT5olt1Tsu0Gv8Go+3rdU=");
            //initialize sdk to enable capture
            FSDK.InitializeLibrary();
            FSDKCam.InitializeCapturing();
            String[] cameralist = new String[] { };
            int      count;

            //get clist of connected cameras and select the first one
            FSDKCam.GetCameraList(out cameralist, out count);
            if (count == 0)
            {
                MessageBox.Show("Please attach a camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            FSDKCam.VideoFormatInfo[] formatList;
            FSDKCam.GetVideoFormatList(ref cameralist[0], out formatList, out count);
            String cameraName;

            cameraName = cameralist[0];
            if (FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle) != FSDK.FSDKE_OK)
            {
                MessageBox.Show("Error opening the first camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            //a camera is opened, so disable controls unitl a face is detected
            //button1.Enabled = false;
            needClose = false;
            int tracker = 0;

            if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile))
            {
                FSDK.CreateTracker(ref tracker);
            }
            int err = 0;

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

            FSDK.CImage image;
            Image       frameImage;

            while (!needClose)
            {
                int ImageHandle = new int();
                if (FSDKCam.GrabFrame(cameraHandle, ref ImageHandle) != FSDK.FSDKE_OK)
                {
                    Application.DoEvents();
                    continue;
                }
                image = new FSDK.CImage(ImageHandle);
                long[] IDs        = new long[256];
                long   faceCount  = new long();
                long   sizeOfLong = 8;
                FSDK.FeedFrame(tracker, 0, image.ImageHandle, ref faceCount, out IDs, sizeOfLong * 256);
                Array.Resize(ref IDs, (int)faceCount);
                frameImage = image.ToCLRImage();
                Graphics gr;
                gr = Graphics.FromImage(frameImage);
                int i;
                for (i = 0; i <= IDs.Length - 1;)
                {
                    if (pictureBox1.Image != null)
                    {
                        // a face has been detected, grab it and close our preview source
                        needClose = true;
                        //button1.Enabled = true;
                        String user;
                        user = "******";
                        try
                        {
                            if (System.IO.Directory.Exists(Application.StartupPath + "\\images"))
                            {
                                pictureBox1.Image.Save(Application.StartupPath + "\\images\\" + user + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                            else
                            {
                                System.IO.Directory.CreateDirectory(Application.StartupPath + "\\images");
                                pictureBox1.Image.Save(Application.StartupPath + "\\images\\" + user + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                        catch (Exception Ex)
                        {
                            Console.WriteLine(Ex.Message);
                        }
                        GC.Collect();
                        Application.DoEvents();
                        break;
                    }
                    //highlight face position in image
                    //    FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                    //    FSDK.GetTrackerFacePosition(tracker, 0, IDs[i], ref facePosition);
                    //    int left, top, w;
                    //    left = facePosition.xc = (int)(facePosition.w * 0.6);
                    //    top = facePosition.yc - (int)(facePosition.w * 0.5);
                    //    w = facePosition.w * (int)1.2;
                    //    string name;
                    //    int res;
                    //    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;
                    //        gr.DrawString(name, new System.Drawing.Font("Arial", 16), new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen), facePosition.xc, top + w + 5, format);
                    //    }
                    //    Pen pen = Pens.LightGreen;
                    //    //this block assigns a name to the image and saves it in our tracker file, but we don't need it since we are storing to our DB Server
                    //    if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                    //    {
                    //        pen = Pens.Blue;
                    //        if (programstate == ProgramStates.psRemember)
                    //        {
                    //            if (FSDK.FSDKE_OK == FSDK.LockID(tracker, IDs[i]))
                    //            {
                    //                //ibe == testdata
                    //                userName = "******";
                    //                if (userName == null)
                    //                {
                    //                    FSDK.SetName(tracker, IDs[i], "");
                    //                }
                    //                else
                    //                {
                    //                    FSDK.SetName(tracker, IDs[i], userName);
                    //                }
                    //                FSDK.UnlockID(tracker, IDs[i]);
                    //            }
                    //        }
                    //    }
                    //    gr.DrawRectangle(pen, left, top, w, w);
                }
                programstate      = ProgramStates.psRecognize;
                pictureBox1.Image = frameImage;
                //free captured resources to speed up program execution
                GC.Collect();
                Application.DoEvents();
            }
            //after capturing a valid face, tell the user and free resources
            //FSDK.SaveTrackerMemoryToFile(tracker, TrackerMemoryFile);
            //FSDK.FreeTracker(tracker);
            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
            MessageBox.Show("Face Image Successfully Captured!!!");
            loaddb();
            match_faces();
        }
        int AddFacehandle()
        {
#if TIME_DEBUG
            label2.Text = "Stopwatch REC start";
            stopwatch.Reset();
            stopwatch.Start();
#endif
            faceTemplates.Clear();
            int maxbear = 0;
            int minbear = 0;
            //label1.Text = "Adding new one";
            for (int count = 0; count < REPEAT_REM; count++)
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return(-1);
                }
                FSDK.CImage Image      = new FSDK.CImage(ImageHandle);
                Image       FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();
                //只允许有一个人在检测范围内
                if (FacePosition.Length == 0)
                {
                    minbear++;
                    //无人脸状态超出忍受值
                    if (minbear > MAX_MINBEAR)
                    {
#if TIME_DEBUG
                        stopwatch.Stop();
                        TimeSpan timespan2     = stopwatch.Elapsed;
                        double   milliseconds2 = timespan2.TotalMilliseconds; //  总毫秒数
                        label2.Text = milliseconds2.ToString() + " .REM";
#endif
                        faceTemplates.Clear();
                        username = "";
                        //label1.Text = "no people";
                        recperson = -2;
                        return(-1);
                    }
                    count--;
                    continue;
                }
                if (FacePosition.Length != 1)
                {
                    maxbear++;
                    //多人脸状态超出忍受值
                    if (maxbear > MAX_MAXBEAR)
                    {
                        faceTemplates.Clear();
                        username = "";
                        //label1.Text = "Too many people";
                        recperson = -3;
                        return(-1);
                    }
                    count--;
                    continue;
                }


                //draw(FacePosition[0]);
                FaceTemplate  Template = new FaceTemplate();
                FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]);
                Template.templateData = Image.GetFaceTemplateUsingEyes(ref features);

                faceTemplates.Add(Template);
                Application.DoEvents();
            }
            if (AddNewOne)
            {
                if (btn1Click)
                {
                    btn1Click = false;
                    int i = 0;
                    for (i = 0; i < REPEAT_REM; i++)
                    {
                        int          fnum  = btn1Count * REPEAT_REM + i;
                        string       tpath = ".\\Users\\" + btn1UserName + fnum.ToString() + ".dat";
                        MemoryStream m     = new MemoryStream(faceTemplates[i].templateData);
                        FileStream   fs    = new FileStream(tpath, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                if (btn2Click)
                {
                    btn2Click = false;
                    int i = 0;
                    for (i = 0; i < REPEAT_REM; i++)
                    {
                        int          fnum  = btn2Count * REPEAT_REM + i;
                        string       tpath = ".\\Users\\" + btn2UserName + fnum.ToString() + ".dat";
                        MemoryStream m     = new MemoryStream(faceTemplates[i].templateData);
                        FileStream   fs    = new FileStream(tpath, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                if (true)
                {
                    AddNewOne = false;
                    string name = username;
                    username = "";
                    if (namexist(name) == -1)
                    {
                        userName.Add(name);
                        List <FaceTemplate> temp = new List <FaceTemplate>(faceTemplates.ToArray());
                        UserTemplates.Add(temp);
                    }
                    else
                    {
                        List <FaceTemplate> temp = new List <FaceTemplate>(faceTemplates.ToArray());
                        int existnum             = namexist(name);
                        int i;
                        for (i = 0; i < temp.Count; i++)
                        {
                            UserTemplates[existnum].Add(temp[i]);
                        }
                    }
                }
            }
            faceTemplates.Clear();
            username = "";
            //label1.Text = "Saved the man";

#if TIME_DEBUG
            stopwatch.Stop();
            TimeSpan timespan     = stopwatch.Elapsed;
            double   milliseconds = timespan.TotalMilliseconds; //  总毫秒数
            label2.Text = milliseconds.ToString() + " .REM";
#endif
            recperson = -1;
            return(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; DetectGender=true; HandleArbitraryRotations=false; DetermineFaceRotationAngle=false; InternalResizeWidth=100; FaceDetectionThreshold=5;", ref err);

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

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

                    String AttributeValues;
                    if (0 == FSDK.GetTrackerFacialAttribute(tracker, 0, IDs[i], "Gender", out AttributeValues, 1024))
                    {
                        float ConfidenceMale   = 0.0f;
                        float ConfidenceFemale = 0.0f;
                        FSDK.GetValueConfidence(AttributeValues, "Male", ref ConfidenceMale);
                        FSDK.GetValueConfidence(AttributeValues, "Female", ref ConfidenceFemale);

                        String str = (ConfidenceMale > ConfidenceFemale ? "Male" : "Female") + ", " +
                                     (ConfidenceMale > ConfidenceFemale ? (int)(ConfidenceMale * 100) : (int)(ConfidenceFemale * 100)).ToString() + "%";

                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Center;

                        gr.DrawString(str, new System.Drawing.Font("Arial", 16),
                                      new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                      facePosition.xc, top + w + 5, format);
                    }
                }

                // display current frame
                pictureBox1.Image = frameImage;

                GC.Collect(); // collect the garbage

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

            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }
Beispiel #19
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Hide();
            this.panel2.Show();
            this.pictureBox1.Show();
            this.webBrowser1.Show();



            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;                                                                     // creating a Tracker

            if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
            {
                FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
            }
            int err = 0;                                                                         // set realtime face detection parameters

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

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

                // make UI controls accessible (to find if the user clicked on a face)
                Application.DoEvents();

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

                    int left = facePosition.xc - (int)(facePosition.w * 0.6);
                    int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                    int w    = (int)(facePosition.w * 1.2);

                    String name    = null;
                    String address = "sunrinwiki.layer7.kr/index.php/";
                    String new_address;

                    int res = FSDK.GetAllNames(tracker, IDs[i], out name, 65536); // maximum of 65536 characters

                    if (FSDK.FSDKE_OK == res && name.Length > 0)
                    { // draw name
                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Center;

                        gr.DrawString(name, new System.Drawing.Font("Arial", 16),

                                      new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                      facePosition.xc, top + w + 5, format);

                        if ((saved_name != name) && (name != null))
                        {
                            new_address = address + name;
                            webBrowser1.Navigate(new_address);
                            saved_name = name;
                        }
                    }

                    Pen pen = Pens.LightGreen;
                    if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                    {
                        pen = Pens.Blue;
                        if (ProgramState.psRemember == programState)
                        {
                            if (FSDK.FSDKE_OK == FSDK.LockID(tracker, IDs[i]))
                            {
                                // get the user name
                                InputName inputName = new InputName();
                                if (DialogResult.OK == inputName.ShowDialog())
                                {
                                    userName = inputName.userName;
                                    if (userName == null || userName.Length <= 0)
                                    {
                                        String s = "";
                                        FSDK.SetName(tracker, IDs[i], "");
                                        FSDK.PurgeID(tracker, IDs[i]);
                                    }
                                    else
                                    {
                                        FSDK.SetName(tracker, IDs[i], userName);
                                    }
                                    FSDK.UnlockID(tracker, IDs[i]);
                                }
                            }
                        }
                    }
                    gr.DrawRectangle(pen, left, top, w, w);
                }
                programState = ProgramState.psRecognize;

                // display current frame
                pictureBox1.Image = frameImage;
                GC.Collect(); // collect the garbage after the deletion
            }
            FSDK.SaveTrackerMemoryToFile(tracker, TrackerMemoryFile);
            FSDK.FreeTracker(tracker);

            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }
        int Recongnizehandle()
        {
#if TIME_DEBUG
            label2.Text = "Stopwatch REC start";
            stopwatch.Reset();
            stopwatch.Start();
#endif
            faceTemplates.Clear();
            //label1.Text = "Recongnize start";
            int maxbear = 0;
            int minbear = 0;
            reset();
            recperson = -1;
            for (int count = 0; count < REPEAT_REC; count++)
            {
                Int32 ImageHandle = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle))
                {
                    return(-1);
                }
                FSDK.CImage Image      = new FSDK.CImage(ImageHandle);
                Image       FrameImage = Image.ToCLRImage();
                gr = Graphics.FromImage(FrameImage);

                FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces();
                if (FacePosition.Length == 0)
                {
                    minbear++;
                    //无人脸状态超出忍受值
                    if (minbear > MAX_MINBEAR)
                    {
                        recperson = -2;
#if TIME_DEBUG
                        stopwatch.Stop();
                        TimeSpan timespan2     = stopwatch.Elapsed;
                        double   milliseconds2 = timespan2.TotalMilliseconds; //  总毫秒数
                        label2.Text = milliseconds2.ToString() + " .REM";
#endif
                        return(1);
                    }
                    count--;
                    continue;
                }
                if (FacePosition.Length != 1)
                {
                    maxbear++;
                    //多人脸状态超出忍受值
                    if (maxbear > MAX_MAXBEAR)
                    {
                        recperson = -3;
                        return(1);
                    }
                    count--;
                    continue;
                }

                //draw(FacePosition[0]);

                FaceTemplate  Template = new FaceTemplate();
                FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]);

                Template.templateData = Image.GetFaceTemplateUsingEyes(ref features);

                int recnum = recongnize(Template);
                faceTemplates.Add(Template);

                who[count] = recnum;

                if (recnum != -1)
                {
                    StringFormat format = new StringFormat();
                    format.Alignment = StringAlignment.Center;
                    gr.DrawString(userName[recnum], new System.Drawing.Font("Arial", 16),
                                  new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                  FacePosition[0].xc, FacePosition[0].yc + FacePosition[0].w * 0.55f, format);
                }
                Application.DoEvents();
            }

            recperson = vote();
            //label1.Text = "Recongnize the man";

            if (recperson > -1)
            {
                List <FaceTemplate> temp = new List <FaceTemplate>(faceTemplates.ToArray());
                int i;
                for (i = 0; i < temp.Count; i++)
                {
                    UserTemplates[recperson].Add(temp[i]);
                }
            }

#if TIME_DEBUG
            stopwatch.Stop();
            TimeSpan timespan     = stopwatch.Elapsed;
            double   milliseconds = timespan.TotalMilliseconds; //  总毫秒数
            label2.Text = milliseconds.ToString() + " .REC";
#endif
            return(0);
        }
Beispiel #21
0
        //处理三个摄像头图像
        private void Bg1_DoWork(object sender, DoWorkEventArgs e)
        {
            string        ConnString = Tool.connstr;//连接字符串
            SqlConnection conn       = new SqlConnection(ConnString);

            int[]    countID1   = new int[CountingSize];
            byte[][] templates1 = new byte[CountingSize][];
            int[]    countID2   = new int[CountingSize];
            byte[][] templates2 = new byte[CountingSize][];

            //存储所有已经出现的id
            List <long> allIDs1 = new List <long>();

            //新建1个Tracker
            int tracker1 = 0;

            FSDK.CreateTracker(ref tracker1);
            int tracker2 = 0;

            FSDK.CreateTracker(ref tracker2);

            //设置Tracker参数
            int err1 = 0;

            FSDK.SetTrackerMultipleParameters(tracker1,
                                              "RecognizeFaces=" + RecognizeFaces.ToString().ToLower() +
                                              "; HandleArbitraryRotations=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; DetermineFaceRotationAngle=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; InternalResizeWidth=" + InternalResizeWidth.ToString().ToLower() +
                                              "; FaceDetectionThreshold=" + FaceDetectionThreshold.ToString().ToLower() + ";",
                                              ref err1);
            int err2 = 0;

            FSDK.SetTrackerMultipleParameters(tracker2,
                                              "RecognizeFaces=" + RecognizeFaces.ToString().ToLower() +
                                              "; HandleArbitraryRotations=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; DetermineFaceRotationAngle=" + HandleArbitraryRotations.ToString().ToLower() +
                                              "; InternalResizeWidth=" + InternalResizeWidth.ToString().ToLower() +
                                              "; FaceDetectionThreshold=" + FaceDetectionThreshold.ToString().ToLower() + ";",
                                              ref err2);

            DateTime t1 = DateTime.Now;
            DateTime t2 = DateTime.Now;

            //记录总帧数
            int loops = 0;

            while (!needClose)
            {
                //声明定义三个图片句柄,来源于三个摄像头句柄
                Int32 imageHandle1 = 0;
                Int32 imageHandle2 = 0;
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle1, ref imageHandle1))
                {
                    continue;
                }
                if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle2, ref imageHandle2))
                {
                    continue;
                }

                FSDK.CImage image1      = new FSDK.CImage(imageHandle1);
                Image       frameImage1 = image1.ToCLRImage();
                FSDK.CImage image2      = new FSDK.CImage(imageHandle2);
                Image       frameImage2 = image2.ToCLRImage();

                long[] IDs1;
                long[] IDs2;
                long   faceCount1 = 0;
                long   faceCount2 = 0;
                FSDK.FeedFrame(tracker1, 0, image1.ImageHandle, ref faceCount1, out IDs1, sizeof(long) * 256); // maximum 256 faces detected
                FSDK.FeedFrame(tracker2, 0, image2.ImageHandle, ref faceCount2, out IDs2, sizeof(long) * 256); // maximum 256 faces detected
                Array.Resize(ref IDs1, (int)faceCount1);
                Array.Resize(ref IDs2, (int)faceCount2);
                Graphics g1 = Graphics.FromImage(frameImage1);
                Graphics g2 = Graphics.FromImage(frameImage2);

                conn.Open();

                Application.DoEvents(); //使控件可用

                //处理1号相机当前帧
                for (int i = 0; i < IDs1.Length; ++i)
                {
                    try
                    {
                        //计算人脸特征模板
                        FSDK.TFacePosition facePosition1 = new FSDK.TFacePosition();
                        FSDK.GetTrackerFacePosition(tracker1, 0, IDs1[i], ref facePosition1);

                        //绘制人脸矩形框
                        int left = facePosition1.xc - (int)(facePosition1.w * 0.5);
                        int top  = facePosition1.yc - (int)(facePosition1.w * 0.5);
                        g1.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition1.w * 1.0), (int)(facePosition1.w * 1.0));

                        //计算当前ID对应人脸特征串
                        byte[] temp1 = image1.GetFaceTemplateInRegion(ref facePosition1);

                        //记录当前ID
                        if (allIDs1.Count == 0)//如果是第一个ID则直接加入
                        {
                            //记录当前ID出现次数
                            countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;

                            //更新当前ID的特征串
                            templates1[IDs1[i]] = temp1;

                            //无重复添加新的ID
                            AddIDIntoAllIDList(allIDs1, IDs1[i]);
                        }
                        else if (allIDs1.Exists(x => x == IDs1[i]))//如果已存在当前ID,则只更新特征串数据
                        {
                            //更新当前ID的特征串
                            templates1[IDs1[i]] = temp1;

                            //记录当前ID出现次数
                            countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;
                        }
                        else//如果是新的ID,则与原来的每个ID对应的模板进行比对,看相似度
                        {
                            float simlarity = 0;
                            //foreach (long id in allIDs1)
                            //{
                            //    int r = FSDK.MatchFaces(ref templates1[id], ref temp1, ref simlarity);
                            //    if (simlarity > 0.8f)//发现相似
                            //    {
                            //        //FSDK.PurgeID(tracker1, IDs[i]);
                            //        countID1[id] = countID1[id] >= 100 ? 100 : countID1[id] + 1;
                            //        break;
                            //    }
                            //    else//没有相似
                            //    {
                            //        //记录当前ID出现次数
                            //        countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;

                            //        //更新当前ID的特征串
                            //        templates1[IDs1[i]] = temp1;

                            //        //无重复添加新的ID
                            //        AddIDIntoAllIDList(allIDs1, IDs1[i]);
                            //    }
                            //}

                            int sim1_count = 0;
                            foreach (long id in allIDs1)
                            {
                                int r = FSDK.MatchFaces(ref templates1[id], ref temp1, ref simlarity);
                                if (simlarity > 0.8f)//发现相似
                                {
                                    countID1[id] = countID1[id] >= 100 ? 100 : countID1[id] + 1;
                                    sim1_count   = sim1_count + 1;
                                    break;
                                }
                            }

                            if (sim1_count == 0)
                            {
                                //记录当前ID出现次数
                                countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1;

                                //更新当前ID的特征串
                                templates1[IDs1[i]] = temp1;

                                //无重复添加新的ID
                                AddIDIntoAllIDList(allIDs1, IDs1[i]);
                            }
                        }
                    }
                    catch { }
                }

                //处理2号相机当前帧
                for (int i = 0; i < IDs2.Length; ++i)
                {
                    try
                    {
                        //计算人脸特征模板
                        FSDK.TFacePosition facePosition2 = new FSDK.TFacePosition();
                        FSDK.GetTrackerFacePosition(tracker2, 0, IDs2[i], ref facePosition2);

                        //绘制人脸矩形框
                        int left = facePosition2.xc - (int)(facePosition2.w * 0.5);
                        int top  = facePosition2.yc - (int)(facePosition2.w * 0.5);
                        g2.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition2.w * 1.0), (int)(facePosition2.w * 1.0));

                        //计算当前ID对应人脸特征串
                        byte[] temp2 = image2.GetFaceTemplateInRegion(ref facePosition2);

                        //如果是新的ID,则与原来的每个ID对应的模板进行比对,看相似度
                        float simlarity = 0;
                        foreach (long id in allIDs1)
                        {
                            int r = FSDK.MatchFaces(ref templates1[id], ref temp2, ref simlarity);
                            if (simlarity > 0.8f)//发现相似
                            {
                                countID1[id] = 0;

                                int        a             = Convert.ToInt32(id);
                                DateTime   XiaCheShiJian = DateTime.Now.ToLocalTime();
                                SqlCommand sqlCmd        = new SqlCommand("UPDATE [SafeSystem].[dbo].[FaceRecognition] SET [XiaCheShiJian] = @XiaCheShiJian WHERE Template = " + a + "and XiaCheShiJian is null", conn);
                                sqlCmd.Parameters.Add("@XiaCheShiJian", System.Data.SqlDbType.DateTime);
                                sqlCmd.Parameters["@XiaCheShiJian"].Value = XiaCheShiJian;
                                sqlCmd.ExecuteNonQuery();
                                RemoveIDFromAllIDList(allIDs1, IDs1[i]);
                                break;
                            }
                        }
                    }
                    catch { }
                }

                //组织当前全部ID列表字符串
                string strAll1 = "";
                int    index1  = 0;
                foreach (int id in allIDs1)
                {
                    if (index1 == 0)
                    {
                        strAll1 += "(" + id + ":" + countID1[id] + ")";
                    }
                    else
                    {
                        strAll1 += ",(" + id + ":" + countID1[id] + ")";
                    }
                    index1++;
                }

                //显示当前实时数据到当前帧
                t2 = DateTime.Now;
                TimeSpan ts = t2 - t1;
                t1 = t2;
                g1.DrawString("Frames Count:" + loops, new Font("微软雅黑", 20f), Brushes.Red, 10, 0);
                g1.DrawString("All Person:" + allIDs1.Count + "[" + strAll1 + "]", new Font("微软雅黑", 20f), Brushes.Red, 10, 50);

                g1.DrawString("Frame Time:" + ts.TotalMilliseconds.ToString("####.#") + "ms", new Font("微软雅黑", 20f), Brushes.Red, 10, 100);
                g1.DrawString("Frame Rate:" + (1000.0 / ts.TotalMilliseconds).ToString("####.#") + "fps", new Font("微软雅黑", 20f), Brushes.Red, 10, 150);
                g1.Dispose();
                g1 = null;
                Helper.SetImage(box1, frameImage1);
                Thread.Sleep(SleepTime);
                g2.Dispose();
                g2 = null;
                Helper.SetImage(box2, frameImage2);
                Thread.Sleep(SleepTime);

                //每处理100帧图像就清理一次出现次数很少的ID,减少计数错误
                if (loops == 100)
                {
                    List <long> bak1 = new List <long>();
                    foreach (long id in allIDs1)
                    {
                        if (countID1[id] > MiniAppear)
                        {
                            bak1.Add(id);
                            int        a = Convert.ToInt32(id);
                            DateTime   ShangCheShiJian = DateTime.Now.ToLocalTime();
                            SqlCommand sqlCmd          = new SqlCommand("if not exists (SELECT [Template] FROM [SafeSystem].[dbo].[FaceRecognition] Where Template = '" + a + "' and XiaCheShiJian is null) INSERT INTO [SafeSystem].[dbo].[FaceRecognition] ([Template],[ShangCheShiJian]) " + " values(@Template, @ShangCheShiJian)", conn);
                            sqlCmd.Parameters.Add("@Template", System.Data.SqlDbType.Int);
                            sqlCmd.Parameters.Add("@ShangCheShiJian", System.Data.SqlDbType.DateTime);
                            sqlCmd.Parameters["@Template"].Value        = a;
                            sqlCmd.Parameters["@ShangCheShiJian"].Value = ShangCheShiJian;
                            sqlCmd.ExecuteNonQuery();
                        }
                        else
                        {
                            countID1[id] = 0;
                        }
                    }
                    allIDs1 = bak1;
                    loops   = 0;
                }
                loops++;

                GC.Collect(); // collect the garbage after the deletion
                conn.Close();
            }
            FSDK.SaveTrackerMemoryToFile(tracker1, TrackerMemoryFile);
            FSDK.FreeTracker(tracker1);
            FSDK.FreeTracker(tracker2);

            FSDKCam.CloseVideoCamera(cameraHandle1);
            FSDKCam.CloseVideoCamera(cameraHandle2);
            FSDKCam.FinalizeCapturing();
        }
Beispiel #22
0
        private void picImg_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do You Want To Change This Pciture?", "Ïntrusion Detection", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return;
            }

            try
            {
                openFileDialog.InitialDirectory = @"C:\";
                openFileDialog.RestoreDirectory = false;
                openFileDialog.Title            = "Select Passport Photo";
                openFileDialog.Filter           = "Images Files (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF";

                openFileDialog.CheckFileExists = true;
                openFileDialog.CheckPathExists = true;


//                openFileDialog.ShowDialog();

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        FSDK.CImage image = new FSDK.CImage(openFileDialog.FileName);

                        // resize image to fit the window width
                        double ratio = System.Math.Min((picImg.Width + 0.4) / image.Width,
                                                       (picImg.Height + 0.4) / image.Height);
                        image = image.Resize(ratio);

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

                        FSDK.TFacePosition facePosition = image.DetectFace();
                        if (0 == facePosition.w)
                        {
                            MessageBox.Show("No faces detected", "Face Detection");
                        }
                        else
                        {
                            int left = facePosition.xc - (int)(facePosition.w * 0.6f);
                            int top  = facePosition.yc - (int)(facePosition.w * 0.5f);
                            gr.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition.w * 1.2), (int)(facePosition.w * 1.2));


                            faceTemplate = new byte[FSDK.TemplateSize];
                            FSDK.GetFaceTemplateInRegion(image.ImageHandle, ref facePosition, out faceTemplate);
                            //GetFaceTemplate(image, out templateData);


                            FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition);
                            int           i = 0;
                            foreach (FSDK.TPoint point in facialFeatures)
                            {
                                gr.DrawEllipse((++i > 2) ? Pens.LightGreen : Pens.Blue, point.x, point.y, 3, 3);
                            }

                            gr.Flush();
                        }

                        // display image
                        picImg.Image = frameImage;
                        picImg.Refresh();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Exception");
                    }
                }

                //picImg.Image = Image.FromFile(openFileDialog.FileName);
            }
            catch (Exception q)
            {
            }
        }
Beispiel #23
0
        private void visorCamaraLuxand_Shown(object sender, EventArgs e)
        {
            #region liveRecognition
            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;                                                                     // creating a Tracker
            if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
            {
                FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
            }
            int err = 0;                                                                         // set realtime face detection parameters
            FSDK.SetTrackerMultipleParameters(tracker, "HandleArbitraryRotations=false; DetermineFaceRotationAngle=false; InternalResizeWidth=100; FaceDetectionThreshold=5;", ref err);
            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);
                // make UI controls accessible (to find if the user clicked on a face)
                Application.DoEvents();
                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);
                    int    left = facePosition.xc - (int)(facePosition.w * 0.6);
                    int    top  = facePosition.yc - (int)(facePosition.w * 0.5);
                    int    w    = (int)(facePosition.w * 1.2);
                    String name = this.nombre + " Edad: " + this.edad + " Puesto: " + this.puesto;
                    //String name;
                    //String nombre;
                    int res = FSDK.GetAllNames(tracker, IDs[i], out nombre, 65536); // maximum of 65536 characters
                    if (FSDK.FSDKE_OK == res && name.Length > 0)
                    {                                                               // draw name
                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Center;
                        gr.DrawString(name, new System.Drawing.Font("Arial", 16),
                                      new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                      facePosition.xc, top + w + 5, format);
                    }
                    Pen pen = Pens.LightGreen;
                    if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                    {
                        pen = Pens.Blue;
                        if (ProgramState.psRemember == programState)
                        {
                            if (FSDK.FSDKE_OK == FSDK.LockID(tracker, IDs[i]))
                            {
                                // get the user name
                                InputName inputName = new InputName();
                                if (DialogResult.OK == inputName.ShowDialog())
                                {
                                    userName = inputName.userName;

                                    /**********************************
                                    *  extraemos data del form input name
                                    **********************************/
                                    string nombre    = inputName.userName;
                                    string a_paterno = inputName.userPaterno;
                                    string a_materno = inputName.userMaterno;
                                    string genero    = inputName.userGenero;
                                    string edad      = inputName.userEdad;
                                    string rol       = inputName.userRol;
                                    string puesto    = inputName.userPuesto;
                                    string correo    = inputName.userCorreo;

                                    /*******************************************
                                    *  guardamos los datos en la tabla de usuarios
                                    *******************************************/
                                    fnRegistraUsuarioBD(nombre, a_paterno, a_materno, genero, edad, rol, puesto, correo);
                                    if (userName == null || userName.Length <= 0)
                                    {
                                        String s = "";
                                        FSDK.SetName(tracker, IDs[i], "");
                                        FSDK.PurgeID(tracker, IDs[i]);
                                    }
                                    else
                                    {
                                        FSDK.SetName(tracker, IDs[i], userName);
                                    }
                                    FSDK.UnlockID(tracker, IDs[i]);
                                }
                            }
                        }
                    }
                    gr.DrawRectangle(pen, left, top, w, w);
                }
                programState = ProgramState.psRecognize;
                // display current frame
                pictureBox1.Image = frameImage;
                GC.Collect(); // collect the garbage after the de
            }
            FSDK.SaveTrackerMemoryToFile(tracker, TrackerMemoryFile);
            FSDK.FreeTracker(tracker);
            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
            #endregion
        }
Beispiel #24
0
        public void proses()
        {
            Pen            p1   = new Pen(Color.LightGreen, 2);
            OpenFileDialog open = new OpenFileDialog();

            open.Filter = "Image Files(*.jpg; *bmp; *.png; *.tiff; *.gif)| *.jpg; *.bmp; *.png; *.tiff; *.gif";
            if (open.ShowDialog() == DialogResult.OK)
            {
            }

            FSDK.CImage image = new FSDK.CImage(open.FileName);
            double      ratio = System.Math.Min((pictureBox.Width + 0.4) / image.Width, pictureBox.Height + 0.4 / image.Height);

            image = image.Resize(ratio);
            Image    frameImage       = image.ToCLRImage();
            Graphics graphicFromImage = Graphics.FromImage(frameImage);

            pictureBox.Image = new Bitmap(frameImage);


            FSDK.TFacePosition facePosition   = image.DetectFace();
            FSDK.TPoint[]      facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition);

            if (0 == facePosition.w)
            {
                MessageBox.Show("No Face detected", "Face Detected");
            }
            else
            {
                left   = facePosition.xc - facePosition.w / 2;
                top    = facePosition.yc - facePosition.w / 2;
                width  = facePosition.w;
                height = facePosition.w + 50;

                graphicFromImage.DrawRectangle(p1, left, top, width, height);

                int i = 0;
                foreach (FSDK.TPoint point in facialFeatures)
                {
                    {
                        graphicFromImage.DrawEllipse((++i > 3) ? Pens.Yellow : Pens.Blue, point.x, point.y, 3, 3);
                    }
                    // richTextBoxForPicture.Text += i.ToString() + "." + point.x.ToString() + ", " + point.y.ToString() + Environment.NewLine;
                }
                graphicFromImage.Flush();
                pictureBox.Image = frameImage;
                pictureBox.Refresh();



                /// ==========================================================================================
                ///

                /*
                 * FSDK.CImage croppedImage = image.CopyRect(left, top, left + width - 1, top + height - 1);
                 * pictureBoxCropped.Image = croppedImage.ToCLRImage();
                 * pictureBoxCropped.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBoxCropped.Refresh();
                 *
                 * int a = 30;
                 *
                 * FSDK.CImage eyeImage = image.CopyRect(facialFeatures[0].x-a, facialFeatures[0].y - a, facialFeatures[0].x + a, facialFeatures[0].y + a);
                 * pictureBox1.Image = eyeImage.ToCLRImage();
                 * pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox1.Refresh();
                 *
                 * FSDK.CImage eyeRImage = image.CopyRect(facialFeatures[1].x - a, facialFeatures[1].y - a, facialFeatures[1].x + a, facialFeatures[1].y + a);
                 * pictureBox2.Image = eyeRImage.ToCLRImage();
                 * pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox2.Refresh();
                 *
                 * FSDK.CImage noseImage = image.CopyRect(facialFeatures[2].x - a, facialFeatures[2].y - a, facialFeatures[2].x + a, facialFeatures[2].y + a);
                 * pictureBox3.Image = noseImage.ToCLRImage();
                 * pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox3.Refresh();
                 *
                 * FSDK.CImage uppermouthImage = image.CopyRect(facialFeatures[64].x - a, facialFeatures[64].y - a, facialFeatures[64].x + a, facialFeatures[64].y);
                 * pictureBox6.Image = uppermouthImage.ToCLRImage();
                 * pictureBox6.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox6.Refresh();
                 *
                 * FSDK.CImage lowmouthImage = image.CopyRect(facialFeatures[64].x - a, facialFeatures[64].y , facialFeatures[64].x + a, facialFeatures[64].y + a);
                 * pictureBox4.Image = lowmouthImage.ToCLRImage();
                 * pictureBox4.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox4.Refresh();
                 *
                 * FSDK.CImage dagu = image.CopyRect(facialFeatures[11].x - (a+20), facialFeatures[11].y - a, facialFeatures[11].x + a + 20, facialFeatures[11].y + a);
                 * pictureBox5.Image = dagu.ToCLRImage();
                 * pictureBox5.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox5.Refresh();
                 *
                 * FSDK.CImage nasolabialright = image.CopyRect(facialFeatures[53].x , facialFeatures[53].y - a, facialFeatures[53].x + a, facialFeatures[53].y + a);
                 * pictureBox7.Image = nasolabialright.ToCLRImage();
                 * pictureBox7.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox7.Refresh();
                 *
                 * FSDK.CImage nasolabialleft = image.CopyRect(facialFeatures[52].x - (a), facialFeatures[52].y - a, facialFeatures[52].x, facialFeatures[52].y + a);
                 * pictureBox8.Image = nasolabialleft.ToCLRImage();
                 * pictureBox8.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox8.Refresh();
                 *
                 * FSDK.CImage eyebrowleft = image.CopyRect(facialFeatures[19].x - a, facialFeatures[19].y - a, facialFeatures[19].x + a, facialFeatures[19].y+10);
                 * pictureBox9.Image = eyebrowleft.ToCLRImage();
                 * pictureBox9.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox9.Refresh();
                 *
                 * FSDK.CImage eyebrowright = image.CopyRect(facialFeatures[20].x - a, facialFeatures[20].y - a, facialFeatures[20].x + a, facialFeatures[20].y+10);
                 * pictureBox10.Image = eyebrowright.ToCLRImage();
                 * pictureBox10.SizeMode = PictureBoxSizeMode.Zoom;
                 * pictureBox10.Refresh();
                 */
            }
            pictureBox.SizeMode  = PictureBoxSizeMode.Normal;
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;


            /// Gambar 2
            /// =========================================================================================



            if (open.ShowDialog() == DialogResult.OK)
            {
                FSDK.CImage img = new FSDK.CImage(open.FileName);

                double rat = System.Math.Min((pictureBox1.Width + 0.4) / img.Width, pictureBox1.Height + 0.4 / img.Height);
                img = img.Resize(rat);


                Image    frameI  = img.ToCLRImage();
                Graphics graphic = Graphics.FromImage(frameI);
                pictureBox1.Image = new Bitmap(frameI);
                FSDK.TFacePosition facePos = img.DetectFace();
                FSDK.TPoint[]      facialF = img.DetectFacialFeaturesInRegion(ref facePos);

                if (0 == facePos.w)
                {
                    MessageBox.Show("No Face detected", "Face Detected");
                }
                else
                {
                    left   = facePos.xc - facePos.w / 2;
                    top    = facePos.yc - facePos.w / 2;
                    width  = facePos.w;
                    height = facePos.w + 50;

                    graphic.DrawRectangle(p1, left, top, width, height);

                    int      i    = 0;
                    double[] temp = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    foreach (FSDK.TPoint point in facialF)
                    {
                        {
                            graphic.DrawEllipse((++i > 3) ? Pens.Yellow : Pens.Blue, point.x, point.y, 3, 3);
                        }
                        if (i == 12 || i == 18 || i == 16 || i == 19 || i == 13)// Left eyebrow
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[0] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 14 || i == 20 || i == 17 || i == 21 || i == 15)// Right eyebrow
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[1] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 23 || i == 35 || i == 28 || i == 36 || i == 24 || i == 38 || i == 27 || i == 37)// Left eye
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[2] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 30 || i == 32 || i == 40 || i == 26 || i == 42 || i == 31 || i == 41 || i == 25)// Right eye
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[3] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 22 || i == 43 || i == 45 || i == 47 || i == 49 || i == 48 || i == 46 || i == 44)// Nose
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[4] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 50 || i == 52) //Left Cheek
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[5] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 51 || i == 53)//Right Cheek
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[9] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 56 || i == 54 || i == 57 || i == 61 || i == 60 || i == 62)// upper mouth 61 60 62
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[6] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 58 || i == 55 || i == 59 || i == 64 || i == 63 || i == 65)// lower mouth  64   63 65
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[7] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 3 || i == 4)// mouth
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[6] += Math.Sqrt((x * x) + (y * y));
                            temp[7] += Math.Sqrt((x * x) + (y * y));
                        }
                        else if (i == 5 || i == 7 || i == 9 || i == 11 || i == 10 || i == 8 || i == 6)// chin
                        {
                            int x = point.x - facialFeatures[i - 1].x;
                            int y = point.y - facialFeatures[i - 1].y;
                            temp[8] += Math.Sqrt((x * x) + (y * y));
                        }
                        // richTextBoxForPicture.Text += i.ToString() + "." + point.x.ToString() + ", " + point.y.ToString() + Environment.NewLine;
                    }
                    graphic.Flush();
                    eucLEyeB.Text  = temp[0].ToString();
                    eucREyeB.Text  = temp[1].ToString();
                    eucLEye.Text   = temp[2].ToString();
                    eucREye.Text   = temp[3].ToString();
                    eucnose.Text   = temp[4].ToString();
                    eucpipi.Text   = temp[5].ToString();
                    eucUmouth.Text = temp[6].ToString();
                    eucLmouth.Text = temp[7].ToString();
                    eucchin.Text   = temp[8].ToString();
                    Rcheck.Text    = temp[9].ToString();

                    double avg = 0;
                    avg         = (temp[0] + temp[1] + temp[2] + temp[3] + temp[4] + temp[5] + temp[6] + temp[7] + temp[8] + temp[9]) / 10;
                    avgbox.Text = avg.ToString();
                }
                pictureBox1.Image = frameI;
                pictureBox1.Refresh();
            }
        }
        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();
        }
Beispiel #26
0
        private static void CropImage(Image sourceImage, string imageName, bool needCrop = true, int angleCount = 0)
        {
            FSDK.TPoint[] pointFeature;

            var image = new FSDK.CImage(new Bitmap(sourceImage));



            var faceRectangle = Rectangle.Empty;

            var facePosition = image.DetectFace();

            if (0 == facePosition.w)
            {
                SaveToFTP(sourceImage, imageName);
                return;
            }

            pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition);

            var left = facePosition.xc - (int)(facePosition.w * 0.6f);

            left = left < 0 ? 0 : left;
            //   int top = facePosition.yc - (int)(facePosition.w * 0.5f);             // верхушку определяет неправильлно. поэтому просто не будем обрезать :)
            var BottomFace = new Vector2(pointFeature[11].x, pointFeature[11].y);

            var distance = pointFeature[2].y - pointFeature[11].y;
            var top      = pointFeature[16].y + distance - 15; // определение высоты по алгоритму старикана

            top = top < 0 ? 0 : top;

            var newWidth = (int)(facePosition.w * 1.2);

            newWidth = newWidth > image.Width || newWidth == 0 ? image.Width : newWidth;

            faceRectangle = new Rectangle(left, top, newWidth,
                                          BottomFace.Y + 15 < image.Height ? (int)(BottomFace.Y + 15) - top : image.Height - top - 1);

            if (needCrop)
            {
                sourceImage = ImageEx.Crop(new Bitmap(sourceImage), faceRectangle);
            }


            // по новой картинке еще раз распознаемм все
            image        = new FSDK.CImage(new Bitmap(sourceImage));
            facePosition = image.DetectFace();
            if (0 == facePosition.w)
            {
                SaveToFTP(sourceImage, imageName);
                return;
            }

            pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition);


            var LeftEyeCenter  = new Vector2(pointFeature[0].x, pointFeature[0].y);
            var RightEyeCenter = new Vector2(pointFeature[1].x, pointFeature[1].y);

            #region Поворот фотки по глазам!



            var v = new Vector2(LeftEyeCenter.X - RightEyeCenter.X, LeftEyeCenter.Y - RightEyeCenter.Y);
            v.Normalize(); // ПД !
            var xVector = new Vector2(1, 0);

            var xDiff = xVector.X - v.X;
            var yDiff = xVector.Y - v.Y;
            var angle = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;

            if (Math.Abs(angle) > 1 && angleCount <= 5) // поворачиваем наклоненные головы
            {
                ++angleCount;
                sourceImage = ImageEx.RotateImage(new Bitmap(sourceImage), (float)-angle);
                CropImage(sourceImage, imageName, false, angleCount);
                return;
            }

            #endregion

            #region Корректируем размер фотки

            const int selectedSize = 1024;              // вызывается уже при создании проекта
            var       max          = (float)Math.Max(sourceImage.Width, sourceImage.Height);
            if (max != selectedSize)
            {
                var k = selectedSize / max;
                sourceImage = ImageEx.ResizeImage(sourceImage, new Size((int)Math.Round(sourceImage.Width * k), (int)Math.Round((sourceImage.Height * k))));
            }

            #endregion

            SaveToFTP(sourceImage, imageName);
        }
Beispiel #27
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();
            }


            // set realtime face detection parameters
            FSDK.SetFaceDetectionParameters(false, false, 100);
            FSDK.SetFaceDetectionThreshold(3);

            // list where we store face templates
            // faceTemplates = new List();
            faceTemplates = new List <FaceTemplate>();


            while (!needClose)
            {
                // faceTemplates.Clear();

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

                    template.templateData = new byte[FSDK.TemplateSize];
                    FaceTemplate template1 = 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

                        label1.Text = "Templates stored: " + faceTemplates.Count.ToString();
                        faceTemplates.Add(template);
                        if (faceTemplates.Count > 9)
                        {
                            // get the user name
                            InputName inputName = new InputName();
                            inputName.ShowDialog();
                            userName = inputName.userName;



                            cmd = new SqlCommand("insert into facetb values(@Name,@face)", con);
                            cmd.Parameters.AddWithValue("@Name", userName);

                            cmd.Parameters.AddWithValue("@face", template.templateData);

                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                            MessageBox.Show("Record Save!");

                            programState = ProgramState.psRecognize;
                        }
                        break;

                    case ProgramState.psRecognize:     // recognize the user
                        bool match = false;


                        con.Open();
                        cmd = new SqlCommand("select * from facetb ORDER BY id ASC ", con);
                        SqlDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            template1.templateData = (byte[])dr["face"];
                            faceTemplates.Add(template1);


                            strList.Add(dr["Name"].ToString());
                        }
                        con.Close();



                        int ii = 0;

                        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)
                            {
                                userName = strList[ii].ToString();

                                label3.Text = strList[ii].ToString();
                                match       = true;
                                break;
                            }

                            ii++;
                        }

                        con.Close();



                        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);
                            // abc = 0;
                            send();
                        }

                        else
                        {
                            abc         = 0;
                            label3.Text = "UnKnow FACE";
                        }
                        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();
        }
Beispiel #28
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = false;

            //int FSDK.SetHTTPProxy("192.168.43.1:8080", 8080, "", "");
            //int r = FSDKCam.OpenIPVideoCamera(FSDKCam.FSDK_VIDEOCOMPRESSIONTYPE.FSDK_MJPEG, "192.168.43.1", "", "", 60, ref cameraHandle);
            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();
            }

            try
            {
                int tracker = 0;                                                                     // creating a Tracker
                if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
                {
                    FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
                }
                int err = 0;                                                                         // set realtime face detection parameters
                FSDK.SetTrackerMultipleParameters(tracker, "HandleArbitraryRotations=false; DetermineFaceRotationAngle=true; InternalResizeWidth=100; FaceDetectionThreshold=1;", ref err);
                FSDK.SetFaceDetectionParameters(false, true, 384);

                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, cameraHandle, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256); // maximum of 256 faces detected
                    Array.Resize(ref IDs, (int)faceCount);


                    // make UI controls accessible (to find if the user clicked on a face)
                    Application.DoEvents();

                    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, cameraHandle, IDs[i], ref facePosition);

                        int left = facePosition.xc - (int)(facePosition.w * 0.6);
                        int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                        int w    = (int)(facePosition.w * 1.2);

                        string name;

                        TFaceRecord fIn = new TFaceRecord();

                        fIn.FacePosition   = new FSDK.TFacePosition();
                        fIn.FacialFeatures = new FSDK.TPoint[2];
                        fIn.Template       = new byte[FSDK.TemplateSize];

                        FSDK.CImage img = new FSDK.CImage(image.ImageHandle);
                        img = image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));

                        fIn.image        = img;
                        fIn.FacePosition = fIn.image.DetectFace();

                        fIn.faceImage      = fIn.image.CopyRect((int)(fIn.FacePosition.xc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.xc + Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc + Math.Round(fIn.FacePosition.w * 0.5)));
                        fIn.FacialFeatures = fIn.image.DetectEyesInRegion(ref fIn.FacePosition);
                        fIn.Template       = fIn.image.GetFaceTemplateInRegion(ref fIn.FacePosition); // get template with higher precision


                        TFaceRecord fOut = new TFaceRecord();
                        //if (Fx.GetPerson(fIn, out fOut))
                        //{
                        //name = fOut.Fullname;

                        // draw name
                        //StringFormat format = new StringFormat();
                        //format.Alignment = StringAlignment.Center;

                        //lblPersons.Text = name;
                        // gr.DrawString(name, new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);

                        //}
                        //else
                        //{
                        //lblPersons.Text = "";

                        if (0 != fIn.FacePosition.w)
                        {
                            //img = new FSDK.CImage(image.ImageHandle);
                            //img = fIn.image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));
                            if (Global.AutomaticTaining)
                            {
                                InputName inputName = new InputName(fIn);
                                inputName.AutoSave();
                                userName = inputName.userName;
                            }
                            else
                            {
                                InputName inputName = new InputName(fIn);
                                if (DialogResult.OK == inputName.ShowDialog(this))
                                {
                                    userName = inputName.userName;
                                    if (userName == null || userName.Length <= 0)
                                    {
                                        String s = "";
                                    }
                                    else
                                    {
                                    }
                                }
                            }
                            //}
                        }

                        //int res = FSDK.GetAllNames(tracker, IDs[i], out name, 65536); // maximum of 65536 characters

                        /*/if (FSDK.FSDKE_OK == res && name.Length > 0)
                         * if (name.Length > 0)
                         * {
                         *  // draw name
                         *  //name = "Prince Daniel";
                         *  StringFormat format = new StringFormat();
                         *  format.Alignment = StringAlignment.Center;
                         *
                         *  gr.DrawString(name, new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);
                         * }
                         *
                         */

                        Pen pen = Pens.LightGreen;
                        if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                        {
                            pen = Pens.Blue;
                            //programState = ProgramState.psRemember;
                            if (ProgramState.psRemember == programState)
                            {
                                //FSDK.CImage img = new FSDK.CImage(image.ImageHandle);
                                img = new FSDK.CImage(image.ImageHandle);
                                img = image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));

                                if (Global.AutomaticTaining)
                                {
                                    InputName inputName = new InputName(img);
                                    inputName.AutoSave();
                                    userName = inputName.userName;
                                }
                                else
                                {
                                    InputName inputName = new InputName(img);
                                    if (DialogResult.OK == inputName.ShowDialog(this))
                                    {
                                        userName = inputName.userName;
                                        if (userName == null || userName.Length <= 0)
                                        {
                                            String s = "";
                                        }
                                        else
                                        {
                                        }
                                    }
                                }
                            }
                        }


                        gr.DrawRectangle(pen, left, top, w, w);
                    }

                    programState = ProgramState.psRecognize;

                    // display current frame
                    imageBox.Image = frameImage;
                    GC.Collect(); // collect the garbage after the deletion
                }

                FSDKCam.CloseVideoCamera(cameraHandle);
                FSDKCam.FinalizeCapturing();
            }
            catch (Exception n)
            {
                Application.DoEvents();
            }
        }
Beispiel #29
0
 public Face(byte[] face_template, FSDK.TFacePosition face_pos, FSDK.TPoint[] facial_features, FSDK.CImage face_image)
 {
     this.face_template   = face_template;
     this.face_position   = FacePosition.FromFSDK(face_pos);
     this.facial_features = facial_features;
     this.image           = face_image;
     this.face_image      = face_image;
 }
Beispiel #30
0
        private void StartStreamingAndDetecting()
        {
            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();
            }

            try
            {
                int tracker = 0;                                                                     // creating a Tracker
                if (FSDK.FSDKE_OK != FSDK.LoadTrackerMemoryFromFile(ref tracker, TrackerMemoryFile)) // try to load saved tracker state
                {
                    FSDK.CreateTracker(ref tracker);                                                 // if could not be loaded, create a new tracker
                }
                int err = 0;                                                                         // set realtime face detection parameters
                FSDK.SetTrackerMultipleParameters(tracker, "HandleArbitraryRotations=false; DetermineFaceRotationAngle=true; InternalResizeWidth=100; FaceDetectionThreshold=1;", ref err);
                FSDK.SetFaceDetectionParameters(false, true, 384);

                int FrameCounter = 0;
                Pen P            = new Pen(Color.AliceBlue);
                int L            = 0;
                int T            = 0;
                int W            = 0;

                string[] Names = new string[10];

                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, cameraHandle, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256); // maximum of 256 faces detected
                    Array.Resize(ref IDs, (int)faceCount);


                    // make UI controls accessible (to find if the user clicked on a face)
                    Application.DoEvents();

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


                    for (int i = 0; i < IDs.Length; ++i)
                    {
                        string FaceFullName = "Unknown Face";
//                        Name = "";
                        if (FaceFullName == "Unknown Face")
                        {
                            FSDK.TFacePosition facePosition = new FSDK.TFacePosition();
                            FSDK.GetTrackerFacePosition(tracker, cameraHandle, IDs[i], ref facePosition);

                            int left = facePosition.xc - (int)(facePosition.w * 0.6);
                            int top  = facePosition.yc - (int)(facePosition.w * 0.5);
                            int w    = (int)(facePosition.w * 1.2);

                            string name;

                            TFaceRecord fIn = new TFaceRecord();

                            fIn.FacePosition   = new FSDK.TFacePosition();
                            fIn.FacialFeatures = new FSDK.TPoint[2];
                            fIn.Template       = new byte[FSDK.TemplateSize];

                            FSDK.CImage img = new FSDK.CImage(image.ImageHandle);
                            img = image.CopyRect((int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));

                            fIn.image        = img;
                            fIn.FacePosition = fIn.image.DetectFace();

                            fIn.faceImage      = fIn.image.CopyRect((int)(fIn.FacePosition.xc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc - Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.xc + Math.Round(fIn.FacePosition.w * 0.5)), (int)(fIn.FacePosition.yc + Math.Round(fIn.FacePosition.w * 0.5)));
                            fIn.FacialFeatures = fIn.image.DetectEyesInRegion(ref fIn.FacePosition);
                            fIn.Template       = fIn.image.GetFaceTemplateInRegion(ref fIn.FacePosition); // get template with higher precision


                            //TFaceRecord fOut = new TFaceRecord();
                            //string FaceFullName = "Unknown Face";

                            //if (FrameCounter < 9)
                            //{
                            //    FrameCounter++;

                            //    StringFormat format = new StringFormat();
                            //    format.Alignment = StringAlignment.Center;

                            //    gr.DrawString("Searching ...", new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);

                            //    Fx.GetPerson(fIn, out FaceFullName);
                            //    Names[FrameCounter] = FaceFullName;
                            //    name = FaceFullName;

                            //}
                            //else
                            //{

                            //FrameCounter++;
                            Fx.GetPerson(fIn, out FaceFullName);
                            Names[FrameCounter] = FaceFullName;
                            name = FaceFullName;
                            if (FaceFullName != "")
                            {
                                IntruderFullName = FaceFullName;
                            }

                            FrameCounter = 0;


                            /*/ draw name
                             * name = (from n in Names
                             *      group n by n into g
                             *      select new
                             *      {
                             *          Key = g.Key,
                             *          Count = g.Count()
                             *      }).OrderByDescending(g => g.Count).ToArray()[0].ToString();
                             */

                            StringFormat format = new StringFormat();
                            format.Alignment = StringAlignment.Center;

                            gr.DrawString(name, new Font("Arial", 16), new SolidBrush(Color.LightGreen), facePosition.xc, top + w + 5, format);

                            Pen pen = Pens.LightGreen;
                            gr.DrawRectangle(pen, left, top, w, w);


                            if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                            {
                                pen = Pens.Blue;
                            }

                            P = pen;
                            L = left;
                            T = top;
                            W = w;

                            gr.DrawRectangle(pen, left, top, w, w);
                            if (FaceFullName == "Unknown Face")
                            {
                                un++;
                                if (un >= 20)
                                {
                                    //MessageBox.Show("I don't kno him");
                                }
                            }
                            else
                            {
                                kno++;
                                if (kno >= 5)
                                {
                                    //MessageBox.Show("i know him");

                                    //Get detected student details
                                    var d_student = db.Students.Where(p => p.fullname == Name).FirstOrDefault();
                                    var att       = db.Attendances.Where(p => p.fullname == name && p.insertdate > DateTime.Now.AddDays(-1));
                                    if (att.Count() == 0)
                                    {
                                        //save to attedance
                                        db.Attendances.Add(
                                            new Attendance
                                        {
                                            // regno =
                                            regno      = d_student.regno,
                                            fullname   = d_student.fullname,
                                            gender     = d_student.gender,
                                            level      = d_student.level,
                                            insertdate = DateTime.Now
                                        });
                                        db.SaveChanges();
                                    }
                                }
                            }

                            if (MakingCall == false)
                            {
                                MakingCall = true;
                                // timer.Tick += timer_Tick;
                                //timer.Interval = 30000000;
                                // timer.Start();
                                //MessageBox.Show("UNKNOWN");
                                Thread.Sleep(5000);
                                TwilioClass call = new TwilioClass();
                                if (call.MakeCall(IntruderFullName))
                                {
                                    MakingCall = false;
                                }
                            }
                            //}
                            //gr.DrawRectangle(P, L, T, W, W);
                            name             = "";
                            IntruderFullName = "";
                        }
                    }

                    //programState = ProgramState.psRecognize;

                    // display current frame
                    imageBox.Image = frameImage;
                    GC.Collect(); // collect the garbage after the deletion
                }

                FSDKCam.CloseVideoCamera(cameraHandle);
                FSDKCam.FinalizeCapturing();
            }
            catch (Exception n)
            {
                StartStreamingAndDetecting();
            }
        }