Ejemplo n.º 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();
        }
Ejemplo n.º 2
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();
    }
Ejemplo n.º 3
0
        //初始化摄像头
        private void cameralInit()
        {
            //激活FaceSDK
            if (FSDK.FSDKE_OK != FSDK.ActivateLibrary(@"O5vT1Fmf2KYH3v4rxuEFhXL55hZPjcl/6Y412kbp1WBv9yy5S13/GtYZfTHKlXV+1a4c03NOYEeIzTsRWgVDvf6hHuyR/bD45k1kMH6dzlFsPilgR3UAcZRm01e6fbeQaomfqcVnWyMSKuXqQgCF07onN0xTKUKQKEd7cqSpuPs="))
            {
                MessageBox.Show("Please run the License Key Wizard (Start - Luxand - FaceSDK - License Key Wizard)", "Error activating FaceSDK", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            FSDK.InitializeLibrary();

            FSDKCam.InitializeCapturing();

            string[] cameraList;                              //声明摄像头数组列表
            int      count;                                   //摄像头数量

            FSDKCam.GetCameraList(out cameraList, out count); //获取各个摄像头分布及其总数
            int countCam = 0;

            foreach (string cam in cameraList)
            {
                //if (cam == @"Logitech HD Webcam C270")
                if (cam == @"USB2.0 PC CAMERA1" || cam == @"USB2.0 PC CAMERA2")
                {
                    countCam++;
                }
            }

            if (count < 2)
            {
                MessageBox.Show("系统需要连接2个摄像头!", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            string cameraName1 = cameraList[0];//读取摄像头,下同
            string cameraName2 = cameraList[1];

            //backgroundWorker1.RunWorkerAsync();//自动工作函数,一打开就自动开启摄像头进行检测


            FSDKCam.VideoFormatInfo[] formatList;//摄像头展现出来的视频格式数组
            FSDKCam.GetVideoFormatList(ref cameraName1, out formatList, out count);
            FSDKCam.GetVideoFormatList(ref cameraName2, out formatList, out count);
            FSDKCam.SetVideoFormat(ref cameraName1, formatList[0]);
            FSDKCam.SetVideoFormat(ref cameraName2, formatList[0]);

            int r1 = FSDKCam.OpenVideoCamera(ref cameraName1, ref cameraHandle1);
            int r2 = FSDKCam.OpenVideoCamera(ref cameraName2, ref cameraHandle2);

            if (r1 != FSDK.FSDKE_OK || r2 != FSDK.FSDKE_OK)
            {
                MessageBox.Show("开启摄像头失败!", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }
Ejemplo n.º 4
0
 public void StartCamera()
 {
     if (cameraCount > 0)
     {
         camIsGood = FSDK.FSDKE_OK == FSDKCam.OpenVideoCamera(ref cameraList[0], ref cameraHandle);
         if (camIsGood)
         {
             progState = PROG_STATE.RUNNING;
             worker.RunWorkerAsync();
         }
     }
 }
Ejemplo n.º 5
0
        private int CameraStart()
        {
            //摄像头启动
            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();
            }
            FSDKCam.SetVideoFormat(ref cameraName, FormatList[choosenFormat]);
            return(CameraHandle);
        }
Ejemplo n.º 6
0
        private void camera_select(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            int s = camera.SelectedIndex;

            if (s != Fitems.cameraN)
            {
                Fitems.init = false;
            }
            if ((Fitems.cameraN == s) && Fitems.init == true)
            {
                doneLoadingCamera(); return;
            }

            //   System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(fsdk_async_funcs));
            //  t.IsBackground = true;
            //  t.SetApartmentState(System.Threading.ApartmentState.STA);
            // t.Priority = System.Threading.ThreadPriority.BelowNormal;
            //  t.Start();
            backgroundWorker1.RunWorkerAsync();

            string cameraName = (string)camera.SelectedItem;


            int r = FSDKCam.OpenVideoCamera(ref cameraName, ref s);

            if (r != FSDK.FSDKE_OK)
            {
                MessageBox.Show("Error opening camera\nCamera Busy", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            int count;

            String[] cameraList;
            FSDKCam.GetCameraList(out cameraList, out count);

            FSDK.SetFaceDetectionParameters(Fitems.facerotationB, Fitems.faceAngle, Fitems.faceWidth);
            FSDK.SetFaceDetectionThreshold(Fitems.facethreshold);

            FSDKCam.VideoFormatInfo[] formatList;

            FSDKCam.GetVideoFormatList(ref cameraName, out formatList, out count);
            Fitems.CamX    = formatList[0].Width;
            Fitems.CamY    = formatList[0].Height;
            Fitems.cameraN = s;
            Application.DoEvents();
            Fitems.init = true;
        }
Ejemplo n.º 7
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();
        }
        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();
        }
Ejemplo n.º 9
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
        }
Ejemplo n.º 10
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;                                                                     // 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;
                    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("", new System.Drawing.Font("Arial", 16),
                                      new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen),
                                      facePosition.xc, top + w + 5, format);
                        label3.Text = name;
                    }
                    else
                    {
                        label3.Text = "UnKnow FACE";
                    }



                    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();
                                inputName.bmp = new Bitmap(pictureBox1.Image);


                                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.SaveTrackerMemoryToFile(tracker, TrackerMemoryFile);

            if (label3.Text == label4.Text)
            {
                //con.Open();
                //cmd = new SqlCommand("select * from facetb where NAme='" + label3.Text + "'", con);
                //SqlDataReader dr = cmd.ExecuteReader();
                //if (dr.Read())
                //{
                //}
                //else
                //{
                //    dr.Close();


                //    FileStream fs = new FileStream(TrackerMemoryFile, FileMode.Open, FileAccess.Read);
                //    BinaryReader br = new BinaryReader(fs);
                //    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                //    br.Close();
                //    fs.Close();

                //    cmd = new SqlCommand("insert into facetb values('" + label3.Text + "',@Image)", con);
                //    conv_photo();
                //    cmd.Parameters.AddWithValue("@Image", bytes);
                //    // con.Open();
                //    cmd.ExecuteNonQuery();
                //    ///con.Close();
                //}
                //con.Close();
            }


            FSDK.FreeTracker(tracker);

            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }
Ejemplo n.º 11
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();
            }
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
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();
        }
Ejemplo n.º 14
0
        private void startDetectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            label1.Visible = true;
            bFirstRun      = true;
            needClose      = true;
            if (mThread != null)
            {
                mThread.Abort();
            }

            if (CameraOpened)
            {
                CameraOpened = false;

                if (FSDK.FSDKE_OK != FSDKCam.CloseVideoCamera(cameraHandle))
                {
                    MessageBox.Show("Error closing camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
            }

            if (usbCamera)
            {
                FSDKCam.InitializeCapturing();

                string[] cameraList;
                int      count;
                FSDKCam.GetCameraList(out cameraList, out count);

                if (0 >= count)
                {
                    MessageBox.Show("Please attach a camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    label1.Visible = false;
                    Application.Exit();
                }
                cameraName = cameraList[0];

                FSDKCam.VideoFormatInfo[] formatList;
                FSDKCam.GetVideoFormatList(ref cameraName, out formatList, out count);

                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);
                    label1.Visible = false;
                    //Application.Exit();
                }
                else
                {
                    CameraOpened = true;
                }
            }
            else
            {
                if (FSDK.FSDKE_OK != FSDKCam.OpenIPVideoCamera(FSDKCam.FSDK_VIDEOCOMPRESSIONTYPE.FSDK_MJPEG, ipcam_addr, ipcam_username, ipcam_password, 50, ref cameraHandle))
                {
                    MessageBox.Show("Error opening IP camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    label1.Visible = false;
                    //Application.Exit();
                }
                else
                {
                    CameraOpened = true;
                }
            }
            needClose = false;

            mThread = new Thread(new ThreadStart(MainLoop));
            mThread.Start();
        }
Ejemplo n.º 15
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();
        }
Ejemplo n.º 16
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();
            }
        }
Ejemplo n.º 17
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; 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);
                    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

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

            FSDKCam.CloseVideoCamera(cameraHandle);
            FSDKCam.FinalizeCapturing();
        }
Ejemplo n.º 18
0
        private void Capturar()
        {
            this.button1.Enabled = false;
            int cameraHandle = 0;

            int r = FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle);

            Debug.WriteLine("cameraName: " + cameraName);
            Debug.WriteLine("cameraHandle: " + cameraHandle);
            Debug.WriteLine("r: " + r);


            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);
                //pictureBox2.Image = frameImage;

                if (IDs.Length == 0)
                {
                    LimpiarCampos();
                }

                for (int i = 0; i < IDs.Length; ++i)
                {
                    //Debug.WriteLine("Cant: " + IDs.Length);
                    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;
                    int    res = FSDK.GetAllNames(tracker, IDs[i], out name, 65536); // maximum of 65536 characters

                    //DETECCION

                    if (FSDK.FSDKE_OK == res)
                    { // draw name
                        if (name.Length <= 0)
                        {
                            name    = "Desconocido";
                            usuario = new Usuario();
                            LimpiarCampos();
                        }
                        else
                        {
                            usuario = BuscarUsuario(name);
                            CarcarCampos(usuario);
                            name = usuario.Nombre + " " + usuario.Apellido;
                            if (textBoxP.Text == "")
                            {
                                return;
                            }
                            BuscarTiporId();
                        }

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

                        gr.DrawString(name, new Font("Century Gothic", 16, FontStyle.Bold),
                                      new SolidBrush(Color.LightGreen),
                                      facePosition.xc, top + w + 5, format);
                    }


                    //REGISTRO
                    Pen pen = Pens.DarkGreen;
                    //if (mouseX >= left && mouseX <= left + w && mouseY >= top && mouseY <= top + w)
                    //{
                    //    pen = Pens.DarkBlue;
                    //    if (ProgramState.psRemember == programState)
                    //    {
                    //        if (FSDK.FSDKE_OK == FSDK.LockID(tracker, IDs[i]))
                    //        {
                    //            // get the user name
                    //            ////CamposBloqueados(false);
                    //            //Debug.WriteLine("desbloqueo campos");
                    //            //Debug.WriteLine("estaGuar: " + estaGuardado);

                    //            inputDatos inputDatos = new inputDatos();
                    //            bool nuevoUsuario = false;

                    //            if (usuario.Id <= 0)
                    //            {
                    //                Debug.WriteLine("Nuevo usuario");
                    //                nuevoUsuario = true;
                    //            }
                    //            else
                    //            {
                    //                Debug.WriteLine("Nombre: " + usuario.Nombre + "\tCedula: " + usuario.Cedula);
                    //                inputDatos.setDatos(usuario);
                    //                nuevoUsuario = false;
                    //            }



                    //            if (DialogResult.OK == inputDatos.ShowDialog())
                    //            //if (estaGuardado)
                    //            {
                    //                Debug.WriteLine("entró if");
                    //                //userName = textBox_cedula.Text;
                    //                usuario = inputDatos.usuario;

                    //                //userName = inputName.userName;
                    //                userName = usuario.Cedula;

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

                    //                usuario.Imagen = frameImage;

                    //                if (nuevoUsuario)
                    //                {
                    //                    usuario = Usuario_Datos.insertUsuario(usuario);
                    //                    MessageBox.Show("Registro guardado correctamente");
                    //                }
                    //                else
                    //                {
                    //                    Debug.WriteLine("id: " + usuario.Id);
                    //                    Debug.WriteLine("ced: " + usuario.Cedula);
                    //                    usuario = Usuario_Datos.updateUsuario(usuario);
                    //                    MessageBox.Show("Registro actualizado correctamente");
                    //                }

                    //                CargarUsuarioDB();

                    //                //CamposBloqueados(true);
                    //            }
                    //            estaGuardado = false;
                    //        }
                    //    }
                    //}

                    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();
            // BuscarTiporId();
        }