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(); }
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(); }
public void SaveFaceData(int tracker) { FSDK.SaveTrackerMemoryToFile(tracker, trackerFile); FSDK.FreeTracker(tracker); }
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(); }
/// <summary> /// Image capture and face detection /// Using Luxand Face SDK /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { // 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 = f.get_staffname(); 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!!!"); }
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(); }
//处理三个摄像头图像 private void Bg1_DoWork(object sender, DoWorkEventArgs e) { string ConnString = Tool.connstr;//连接字符串 SqlConnection conn = new SqlConnection(ConnString); int[] countID1 = new int[CountingSize]; byte[][] templates1 = new byte[CountingSize][]; int[] countID2 = new int[CountingSize]; byte[][] templates2 = new byte[CountingSize][]; //存储所有已经出现的id List <long> allIDs1 = new List <long>(); //新建1个Tracker int tracker1 = 0; FSDK.CreateTracker(ref tracker1); int tracker2 = 0; FSDK.CreateTracker(ref tracker2); //设置Tracker参数 int err1 = 0; FSDK.SetTrackerMultipleParameters(tracker1, "RecognizeFaces=" + RecognizeFaces.ToString().ToLower() + "; HandleArbitraryRotations=" + HandleArbitraryRotations.ToString().ToLower() + "; DetermineFaceRotationAngle=" + HandleArbitraryRotations.ToString().ToLower() + "; InternalResizeWidth=" + InternalResizeWidth.ToString().ToLower() + "; FaceDetectionThreshold=" + FaceDetectionThreshold.ToString().ToLower() + ";", ref err1); int err2 = 0; FSDK.SetTrackerMultipleParameters(tracker2, "RecognizeFaces=" + RecognizeFaces.ToString().ToLower() + "; HandleArbitraryRotations=" + HandleArbitraryRotations.ToString().ToLower() + "; DetermineFaceRotationAngle=" + HandleArbitraryRotations.ToString().ToLower() + "; InternalResizeWidth=" + InternalResizeWidth.ToString().ToLower() + "; FaceDetectionThreshold=" + FaceDetectionThreshold.ToString().ToLower() + ";", ref err2); DateTime t1 = DateTime.Now; DateTime t2 = DateTime.Now; //记录总帧数 int loops = 0; while (!needClose) { //声明定义三个图片句柄,来源于三个摄像头句柄 Int32 imageHandle1 = 0; Int32 imageHandle2 = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle1, ref imageHandle1)) { continue; } if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle2, ref imageHandle2)) { continue; } FSDK.CImage image1 = new FSDK.CImage(imageHandle1); Image frameImage1 = image1.ToCLRImage(); FSDK.CImage image2 = new FSDK.CImage(imageHandle2); Image frameImage2 = image2.ToCLRImage(); long[] IDs1; long[] IDs2; long faceCount1 = 0; long faceCount2 = 0; FSDK.FeedFrame(tracker1, 0, image1.ImageHandle, ref faceCount1, out IDs1, sizeof(long) * 256); // maximum 256 faces detected FSDK.FeedFrame(tracker2, 0, image2.ImageHandle, ref faceCount2, out IDs2, sizeof(long) * 256); // maximum 256 faces detected Array.Resize(ref IDs1, (int)faceCount1); Array.Resize(ref IDs2, (int)faceCount2); Graphics g1 = Graphics.FromImage(frameImage1); Graphics g2 = Graphics.FromImage(frameImage2); conn.Open(); Application.DoEvents(); //使控件可用 //处理1号相机当前帧 for (int i = 0; i < IDs1.Length; ++i) { try { //计算人脸特征模板 FSDK.TFacePosition facePosition1 = new FSDK.TFacePosition(); FSDK.GetTrackerFacePosition(tracker1, 0, IDs1[i], ref facePosition1); //绘制人脸矩形框 int left = facePosition1.xc - (int)(facePosition1.w * 0.5); int top = facePosition1.yc - (int)(facePosition1.w * 0.5); g1.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition1.w * 1.0), (int)(facePosition1.w * 1.0)); //计算当前ID对应人脸特征串 byte[] temp1 = image1.GetFaceTemplateInRegion(ref facePosition1); //记录当前ID if (allIDs1.Count == 0)//如果是第一个ID则直接加入 { //记录当前ID出现次数 countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1; //更新当前ID的特征串 templates1[IDs1[i]] = temp1; //无重复添加新的ID AddIDIntoAllIDList(allIDs1, IDs1[i]); } else if (allIDs1.Exists(x => x == IDs1[i]))//如果已存在当前ID,则只更新特征串数据 { //更新当前ID的特征串 templates1[IDs1[i]] = temp1; //记录当前ID出现次数 countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1; } else//如果是新的ID,则与原来的每个ID对应的模板进行比对,看相似度 { float simlarity = 0; //foreach (long id in allIDs1) //{ // int r = FSDK.MatchFaces(ref templates1[id], ref temp1, ref simlarity); // if (simlarity > 0.8f)//发现相似 // { // //FSDK.PurgeID(tracker1, IDs[i]); // countID1[id] = countID1[id] >= 100 ? 100 : countID1[id] + 1; // break; // } // else//没有相似 // { // //记录当前ID出现次数 // countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1; // //更新当前ID的特征串 // templates1[IDs1[i]] = temp1; // //无重复添加新的ID // AddIDIntoAllIDList(allIDs1, IDs1[i]); // } //} int sim1_count = 0; foreach (long id in allIDs1) { int r = FSDK.MatchFaces(ref templates1[id], ref temp1, ref simlarity); if (simlarity > 0.8f)//发现相似 { countID1[id] = countID1[id] >= 100 ? 100 : countID1[id] + 1; sim1_count = sim1_count + 1; break; } } if (sim1_count == 0) { //记录当前ID出现次数 countID1[IDs1[i]] = countID1[IDs1[i]] >= 100 ? 100 : countID1[IDs1[i]] + 1; //更新当前ID的特征串 templates1[IDs1[i]] = temp1; //无重复添加新的ID AddIDIntoAllIDList(allIDs1, IDs1[i]); } } } catch { } } //处理2号相机当前帧 for (int i = 0; i < IDs2.Length; ++i) { try { //计算人脸特征模板 FSDK.TFacePosition facePosition2 = new FSDK.TFacePosition(); FSDK.GetTrackerFacePosition(tracker2, 0, IDs2[i], ref facePosition2); //绘制人脸矩形框 int left = facePosition2.xc - (int)(facePosition2.w * 0.5); int top = facePosition2.yc - (int)(facePosition2.w * 0.5); g2.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition2.w * 1.0), (int)(facePosition2.w * 1.0)); //计算当前ID对应人脸特征串 byte[] temp2 = image2.GetFaceTemplateInRegion(ref facePosition2); //如果是新的ID,则与原来的每个ID对应的模板进行比对,看相似度 float simlarity = 0; foreach (long id in allIDs1) { int r = FSDK.MatchFaces(ref templates1[id], ref temp2, ref simlarity); if (simlarity > 0.8f)//发现相似 { countID1[id] = 0; int a = Convert.ToInt32(id); DateTime XiaCheShiJian = DateTime.Now.ToLocalTime(); SqlCommand sqlCmd = new SqlCommand("UPDATE [SafeSystem].[dbo].[FaceRecognition] SET [XiaCheShiJian] = @XiaCheShiJian WHERE Template = " + a + "and XiaCheShiJian is null", conn); sqlCmd.Parameters.Add("@XiaCheShiJian", System.Data.SqlDbType.DateTime); sqlCmd.Parameters["@XiaCheShiJian"].Value = XiaCheShiJian; sqlCmd.ExecuteNonQuery(); RemoveIDFromAllIDList(allIDs1, IDs1[i]); break; } } } catch { } } //组织当前全部ID列表字符串 string strAll1 = ""; int index1 = 0; foreach (int id in allIDs1) { if (index1 == 0) { strAll1 += "(" + id + ":" + countID1[id] + ")"; } else { strAll1 += ",(" + id + ":" + countID1[id] + ")"; } index1++; } //显示当前实时数据到当前帧 t2 = DateTime.Now; TimeSpan ts = t2 - t1; t1 = t2; g1.DrawString("Frames Count:" + loops, new Font("微软雅黑", 20f), Brushes.Red, 10, 0); g1.DrawString("All Person:" + allIDs1.Count + "[" + strAll1 + "]", new Font("微软雅黑", 20f), Brushes.Red, 10, 50); g1.DrawString("Frame Time:" + ts.TotalMilliseconds.ToString("####.#") + "ms", new Font("微软雅黑", 20f), Brushes.Red, 10, 100); g1.DrawString("Frame Rate:" + (1000.0 / ts.TotalMilliseconds).ToString("####.#") + "fps", new Font("微软雅黑", 20f), Brushes.Red, 10, 150); g1.Dispose(); g1 = null; Helper.SetImage(box1, frameImage1); Thread.Sleep(SleepTime); g2.Dispose(); g2 = null; Helper.SetImage(box2, frameImage2); Thread.Sleep(SleepTime); //每处理100帧图像就清理一次出现次数很少的ID,减少计数错误 if (loops == 100) { List <long> bak1 = new List <long>(); foreach (long id in allIDs1) { if (countID1[id] > MiniAppear) { bak1.Add(id); int a = Convert.ToInt32(id); DateTime ShangCheShiJian = DateTime.Now.ToLocalTime(); SqlCommand sqlCmd = new SqlCommand("if not exists (SELECT [Template] FROM [SafeSystem].[dbo].[FaceRecognition] Where Template = '" + a + "' and XiaCheShiJian is null) INSERT INTO [SafeSystem].[dbo].[FaceRecognition] ([Template],[ShangCheShiJian]) " + " values(@Template, @ShangCheShiJian)", conn); sqlCmd.Parameters.Add("@Template", System.Data.SqlDbType.Int); sqlCmd.Parameters.Add("@ShangCheShiJian", System.Data.SqlDbType.DateTime); sqlCmd.Parameters["@Template"].Value = a; sqlCmd.Parameters["@ShangCheShiJian"].Value = ShangCheShiJian; sqlCmd.ExecuteNonQuery(); } else { countID1[id] = 0; } } allIDs1 = bak1; loops = 0; } loops++; GC.Collect(); // collect the garbage after the deletion conn.Close(); } FSDK.SaveTrackerMemoryToFile(tracker1, TrackerMemoryFile); FSDK.FreeTracker(tracker1); FSDK.FreeTracker(tracker2); FSDKCam.CloseVideoCamera(cameraHandle1); FSDKCam.CloseVideoCamera(cameraHandle2); FSDKCam.FinalizeCapturing(); }
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 }
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(); }
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(); }
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(); }