背景环境
1、采用Emgu.CV 4.1.1.x + WPF + .NET Framework 4.5x
2、数据库及ORM框架:Mysql 5.7x + SqlSgar 5.0.3.2
实现效果图
实现过程
一、摄像管理
1、摄像开启与关闭
(1) 开启摄像
/* 前提:先设计好Wpf设计界面(如图所示)
* 人脸头像:imageDes(Image类型)
*
* 摄像区域:imageShow(ImageBox类型)
* 属于Emgu.CV.UI.ImageBox(WinForm对象),通过<WindowsFormsHost Margin="0" x:Name="windowsFormHost1">……</WindowsFormsHost>进行调用
* 需引用:【xmlns:ui="clr-namespace:Emgu.CV.UI;assembly=Emgu.CV.UI"】
*
* 具体实现部分:
* 1.先申明所需的对象
* 2.调用摄像头对象ImageGrabbed事件进行人脸捕获
*/
/// <summary>
/// 摄像头对象
/// </summary>
private VideoCapture capture { get; set; }
/// <summary>
/// 摄像头图像
/// </summary>
private UMat uMat { get; set; }
/// <summary>
/// 人脸数量
/// </summary>
private int faceDetectCount = 0;
/// <summary>
/// 人脸训练模型
/// </summary>
private FaceTranModel facetranModel;
/// <summary>
/// 开启摄像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnCollect_Click(object sender, RoutedEventArgs e)
{
capture = new VideoCapture(0)
{
//图像水平反转
FlipHorizontal = true
};
//capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 30);
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 480);//480*320
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 270);
//capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Zoom, 0.5);
try
{
//重置PictureBox对象
imageShow.Image = null;
//实时获取图像
capture.ImageGrabbed += (s, ee) =>
{
uMat = new UMat();
capture.Retrieve(uMat, 1);
faceDetectCount = FaceRecognizeHelper.FaceDetecting(uMat);
imageShow.Image = uMat;
};
capture.Start();
}
catch (Exception ex)
{
LogTextHelper.Error("[ERROR] 摄像头开启失败:" + ex.Message);
//MessageDxUtil.ShowError(ex.Message);
LoadLogInfo();
}
BtnCollect.IsEnabled = !capture.IsOpened;
}
/// <summary>
/// 加载跟踪日志
/// </summary>
private void LoadLogInfo()
{
//清空缓存数据
richTBoxLog.Document.Blocks.Clear();
string Logfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log",
DateTime.Now.ToString("yyyyMMdd") + ".log");
if (File.Exists(Logfile))
{
Parah.Inlines.Add(FileUtil.FileToString(Logfile));
FlowDoc.Blocks.Add(Parah);
richTBoxLog.Document = FlowDoc;
}
}
/*构造窗体函数*/
public MainWindow()
{
InitializeComponent();
facetranModel = new FaceTranModel();
DataContext = facetranModel;
}
(2)关闭摄像
/// <summary>
/// 关闭摄像头
/// </summary>
private void StopVedioCamera()
{
capture.Stop();
capture.Dispose();
capture = null;
}
2、人脸数据采集
(1) 检查验证输入
#region 检查验证输入
/// <summary>
/// 验证TextBox输入内容
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private bool CheckName(TextBox tBox)
{
string name = tBox.Text.Trim();
if (string.IsNullOrEmpty(name))
{
MessageDxUtil.ShowWarning("用户姓名不能为空!");
tBox.Focus();
return false;
}
if (name.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) >= 0)
{
MessageDxUtil.ShowWarning("用户姓名有非法字符!");
tBox.Focus();
return false;
}
return true;
}
/// <summary>
/// 验证Image加载的图片
/// </summary>
/// <param name="picImage"></param>
/// <returns></returns>
private bool CheckPicture(System.Windows.Controls.Image picImage)
{
if (picImage.Source == null)
{
MessageDxUtil.ShowWarning("未检测到人脸,请重新采集!");
picImage.Focus();
return false;
}
return true;
}
#endregion
/* MessageDxUtil 公共会话类 */
public class MessageDxUtil
{
/// <summary>
/// 显示一般的提示信息
/// </summary>
/// <param name="message">提示信息</param>
public static DialogResult ShowTips(string message)
{
return MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 显示警告信息
/// </summary>
/// <param name="message">警告信息</param>
public static DialogResult ShowWarning(string message)
{
return MessageBox.Show(message, "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
/// <summary>
/// 显示错误信息
/// </summary>
/// <param name="message">错误信息</param>
public static DialogResult ShowError(string message)
{
return MessageBox.Show(message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/* 其它省略 */
}
(2) 采集事件
/// <summary>
/// 采集人脸
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
if (c