int AddFacehandle() { #if TIME_DEBUG label2.Text = "Stopwatch REC start"; stopwatch.Reset(); stopwatch.Start(); #endif faceTemplates.Clear(); int maxbear = 0; int minbear = 0; //label1.Text = "Adding new one"; for (int count = 0; count < REPEAT_REM; count++) { Int32 ImageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle)) { return -1; } FSDK.CImage Image = new FSDK.CImage(ImageHandle); Image FrameImage = Image.ToCLRImage(); gr = Graphics.FromImage(FrameImage); FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces(); //只允许有一个人在检测范围内 if (FacePosition.Length == 0) { minbear++; //无人脸状态超出忍受值 if (minbear > MAX_MINBEAR) { #if TIME_DEBUG stopwatch.Stop(); TimeSpan timespan2 = stopwatch.Elapsed; double milliseconds2 = timespan2.TotalMilliseconds; // 总毫秒数 label2.Text = milliseconds2.ToString() + " .REM"; #endif faceTemplates.Clear(); username = ""; //label1.Text = "no people"; recperson = -2; return -1; } count--; continue; } if (FacePosition.Length != 1) { maxbear++; //多人脸状态超出忍受值 if (maxbear > MAX_MAXBEAR) { faceTemplates.Clear(); username = ""; //label1.Text = "Too many people"; recperson = -3; return -1; } count--; continue; } //draw(FacePosition[0]); FaceTemplate Template = new FaceTemplate(); FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]); Template.templateData = Image.GetFaceTemplateUsingEyes(ref features); faceTemplates.Add(Template); Application.DoEvents(); } if (AddNewOne) { if (btn1Click) { btn1Click = false; int i = 0; for (i = 0; i < REPEAT_REM; i++) { int fnum = btn1Count * REPEAT_REM + i; string tpath = ".\\Users\\" + btn1UserName + fnum.ToString() + ".dat"; MemoryStream m = new MemoryStream(faceTemplates[i].templateData); FileStream fs = new FileStream(tpath, FileMode.OpenOrCreate); m.WriteTo(fs); m.Close(); fs.Close(); } } if (btn2Click) { btn2Click = false; int i = 0; for (i = 0; i < REPEAT_REM; i++) { int fnum = btn2Count * REPEAT_REM + i; string tpath = ".\\Users\\" + btn2UserName + fnum.ToString() + ".dat"; MemoryStream m = new MemoryStream(faceTemplates[i].templateData); FileStream fs = new FileStream(tpath, FileMode.OpenOrCreate); m.WriteTo(fs); m.Close(); fs.Close(); } } if(true) { AddNewOne = false; string name = username; username = ""; if (namexist(name) == -1) { userName.Add(name); List<FaceTemplate> temp = new List<FaceTemplate>(faceTemplates.ToArray()); UserTemplates.Add(temp); } else { List<FaceTemplate> temp = new List<FaceTemplate>(faceTemplates.ToArray()); int existnum = namexist(name); int i; for (i = 0; i < temp.Count; i++) { UserTemplates[existnum].Add(temp[i]); } } } } faceTemplates.Clear(); username = ""; //label1.Text = "Saved the man"; #if TIME_DEBUG stopwatch.Stop(); TimeSpan timespan = stopwatch.Elapsed; double milliseconds = timespan.TotalMilliseconds; // 总毫秒数 label2.Text = milliseconds.ToString()+" .REM"; #endif recperson = -1; return 0; }
int Recongnizehandle() { #if TIME_DEBUG label2.Text = "Stopwatch REC start"; stopwatch.Reset(); stopwatch.Start(); #endif faceTemplates.Clear(); //label1.Text = "Recongnize start"; int maxbear = 0; int minbear = 0; reset(); recperson = -1; for (int count = 0; count < REPEAT_REC; count++) { Int32 ImageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle)) { return -1; } FSDK.CImage Image = new FSDK.CImage(ImageHandle); Image FrameImage = Image.ToCLRImage(); gr = Graphics.FromImage(FrameImage); FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces(); if (FacePosition.Length == 0) { minbear++; //无人脸状态超出忍受值 if (minbear > MAX_MINBEAR) { recperson = -2; #if TIME_DEBUG stopwatch.Stop(); TimeSpan timespan2 = stopwatch.Elapsed; double milliseconds2 = timespan2.TotalMilliseconds; // 总毫秒数 label2.Text = milliseconds2.ToString() + " .REM"; #endif return 1; } count--; continue; } if (FacePosition.Length != 1) { maxbear++; //多人脸状态超出忍受值 if (maxbear > MAX_MAXBEAR) { recperson = -3; return 1; } count--; continue; } //draw(FacePosition[0]); FaceTemplate Template = new FaceTemplate(); FSDK.TPoint[] features = Image.DetectEyesInRegion(ref FacePosition[0]); Template.templateData = Image.GetFaceTemplateUsingEyes(ref features); int recnum = recongnize(Template); faceTemplates.Add(Template); who[count] = recnum; if (recnum != -1) { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(userName[recnum], new System.Drawing.Font("Arial", 16), new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen), FacePosition[0].xc, FacePosition[0].yc + FacePosition[0].w * 0.55f, format); } Application.DoEvents(); } recperson = vote(); //label1.Text = "Recongnize the man"; if (recperson > -1) { List<FaceTemplate> temp = new List<FaceTemplate>(faceTemplates.ToArray()); int i; for (i = 0; i < temp.Count; i++) { UserTemplates[recperson].Add(temp[i]); } } #if TIME_DEBUG stopwatch.Stop(); TimeSpan timespan = stopwatch.Elapsed; double milliseconds = timespan.TotalMilliseconds; // 总毫秒数 label2.Text = milliseconds.ToString() + " .REC"; #endif return 0; }
int Normalhandle() { try { Int32 ImageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(CameraHandle, ref ImageHandle)) { return -1; } FSDK.CImage Image = new FSDK.CImage(ImageHandle); Image FrameImage = Image.ToCLRImage(); gr = Graphics.FromImage(FrameImage); pictureBox1.Image = FrameImage; FSDK.TFacePosition[] FacePosition = Image.DetectMultipleFaces(); for (int person = 0; person < FacePosition.Length; person++) draw(FacePosition[person]); GC.Collect(); // collect the garbage after the deletion return 0; } catch { return -1; } }