Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
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();
            }
        }