/// <summary> /// 更新已登记的人脸图片。 /// </summary> /// <param name="f"></param> /// <param name="fd"></param> public void UpdateFacePicture(FaceInfo f, FaceData fd) { if (DataSource == DataSrcType.FileSystem) { //存储抓拍到的新图片,替换原始图片 string savepath; int uid = f.userid; switch (f.angleType) { case FaceData.FaceAngleType.Middle: savepath = Path.GetDirectoryName(fd.text) + "\\" + _FaceDic.GetUserName(uid) + "0.jpg"; break; case FaceData.FaceAngleType.Up: savepath = Path.GetDirectoryName(fd.text) + "\\" + _FaceDic.GetUserName(uid) + "1.jpg"; break; case FaceData.FaceAngleType.Down: savepath = Path.GetDirectoryName(fd.text) + "\\" + _FaceDic.GetUserName(uid) + "2.jpg"; break; case FaceData.FaceAngleType.Left: savepath = Path.GetDirectoryName(fd.text) + "\\" + _FaceDic.GetUserName(uid) + "3.jpg"; break; case FaceData.FaceAngleType.Right: savepath = Path.GetDirectoryName(fd.text) + "\\" + _FaceDic.GetUserName(uid) + "4.jpg"; break; default: savepath = fd.text; break; } Console.WriteLine("Save shot face: " + savepath); f.text = savepath; if (f.FaceShotBmp != null) { f.FaceShotBmp.Save(savepath); } } else { //数据库更新 //TODO } }
private Rectangle drawFaceWires() { //DateTime t1 = DateTime.Now; Rectangle rect = new Rectangle(Frame.Bitmap.Width / 2 - 50, Frame.Bitmap.Height / 2 - 50, 100, 100); //foreach (FaceInfo f in Face.TracingFaceList) for (int i = 0; i < Face.TracingFaceList.Count; i++) { FaceInfo f = Face.TracingFaceList[i]; if ((DateTime.Now - f.procTime).TotalMilliseconds > FaceRecgnize.MAX_FRAME_INTERVAL * 2) { continue; } //rect = f.getPos(); if (_LastFacePos.ContainsKey(f.faceid)) { rect = _LastFacePos[f.faceid]; //获取上次位置 int dx = f.posLeft + f.posRight - rect.Left - rect.Right; //框中心位置x的偏移量 int dy = f.posTop + f.posBottom - rect.Top - rect.Bottom; //y double d_ratio = Math.Sqrt(dx * dx + dy * dy) / 4 / rect.Width * 0.9; //本次画框坐标调整比率 int dw = f.FaceWidth() - rect.Width; //宽度偏移量 double w_ratio = (double)Math.Abs(dw) / rect.Width * 0.95; //宽度调整比率 rect.Height = rect.Width = rect.Width + (int)(dw * w_ratio); //新框宽高 if (rect.Width > 2 * f.FaceWidth() || rect.Width < f.FaceWidth() / 2) //安全矫正 { rect.Height = rect.Width = f.FaceWidth(); } rect.X = (int)((rect.Left + rect.Right + dx * d_ratio - rect.Width) / 2); //新框左上角x rect.Y = (int)((rect.Top + rect.Bottom + dy * d_ratio - rect.Width) / 2); //新框左上角y if (rect.X < 0 || rect.Y < 0 || rect.X > PictureWidth || rect.Y > PictureHight) //安全矫正 { rect = f.getPos(); } _LastFacePos[f.faceid] = rect;//记录本次位置 } else { rect = _LastFacePos[f.faceid] = f.getPos(); } //设置颜色 if (f.userid != 0) { Face.DrawPenColor = Color.Red; } else { Face.DrawPenColor = Color.Green; } //画框 Face.DrawFaceWire(Frame.Bitmap, rect); //人脸框上写字 if (_FaceCmd == FaceCommand.ShotOneAndFind || _FaceCmd == FaceCommand.ShotAllAndFind) { string strinfo = (f.userid == 0 ? "???" : FaceDic.GetUserName(f.userid)) + " " + (f.gender == FaceInfo.Gender.Mail ? "男" : "女") + " " + f.age + "岁"; Point strPos = new Point(rect.Left + rect.Width / 2, rect.Bottom + 3); StringFormat strformat = new StringFormat(); strformat.Alignment = StringAlignment.Center; Font font = new Font("微软雅黑", Math.Min(f.FaceWidth() / 20 + 7, 20)); SolidBrush sbrush = new SolidBrush(Face.DrawPenColor); Graphics g = Graphics.FromImage(Frame.Bitmap); g.DrawString(strinfo, font, sbrush, strPos, strformat); g.Dispose(); } } //Console.WriteLine("drawFaceWires: runtime=" + (DateTime.Now - t1).TotalMilliseconds); return(rect); }