当通过网络摄像头检测到人脸时,自动捕获图像
本文关键字:图像 网络 摄像头 检测 | 更新日期: 2023-09-27 18:29:25
我正在做一个关于图像处理的研究项目。该项目是用之前生成的自动化试卷来评估用户。
由于这是论文评估过程中的在线过程,我需要从用户的网络摄像头中随机获取用户图像。。。我正在使用C#语言在普通的基于Windows的应用程序中实现这个项目。。
对于这个过程,我已经成功地将用户的图像转换为Windows格式,并且我已经可以检测用户的面部。
问题是,我想在windows窗体中检测人脸时为用户获取图像。我正在使用EMGU CV库来实现此图像检测。。
1) 当用户面部检测到时,我将如何捕获用户图像。。2) 我想让它在随机时间中捕捉图像。。。
这是我用来实现人脸检测的代码。
public class ClassifierTrain
{
#region Variables
//Eigen
MCvTermCriteria termCrit;
EigenObjectRecognizer recognizer;
//training variables
List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();//Images
List<string> Names_List = new List<string>(); //labels
int ContTrain, NumLabels;
//Class Variables
string Error;
bool _IsTrained = false;
#endregion
#region Constructors
/// <summary>
/// Default Constructor, Looks in (Application.StartupPath + "''TrainedFaces") for traing data.
/// </summary>
public ClassifierTrain()
{
termCrit = new MCvTermCriteria(ContTrain, 0.001);
_IsTrained = LoadTrainingData(Application.StartupPath + "''TrainedFaces");
}
/// <summary>
/// Takes String input to a different location for training data
/// </summary>
/// <param name="Training_Folder"></param>
public ClassifierTrain(string Training_Folder)
{
termCrit = new MCvTermCriteria(ContTrain, 0.001);
_IsTrained = LoadTrainingData(Training_Folder);
}
#endregion
#region Public
/// <summary>
/// <para>Return(True): If Training data has been located and Eigen Recogniser has been trained</para>
/// <para>Return(False): If NO Training data has been located of error in training has occured</para>
/// </summary>
public bool IsTrained
{
get { return _IsTrained; }
}
/// <summary>
/// Recognise a Grayscale Image using the trained Eigen Recogniser
/// </summary>
/// <param name="Input_image"></param>
/// <returns></returns>
public string Recognise(Image<Gray, byte> Input_image)
{
if (_IsTrained)
{
string t = recognizer.Recognize(Input_image);
return t;
}
else return "";//Blank prefered else can use null
}
/// <summary>
/// Returns a string contatining any error that has occured
/// </summary>
public string Get_Error
{
get { return Error; }
}
/// <summary>
/// Dispose of Class call Garbage Collector
/// </summary>
public void Dispose()
{
recognizer = null;
trainingImages = null;
Names_List = null;
Error = null;
GC.Collect();
}
#endregion
#region Private
/// <summary>
/// Loads the traing data given a (string) folder location
/// </summary>
/// <param name="Folder_loacation"></param>
/// <returns></returns>
private bool LoadTrainingData(string Folder_loacation)
{
if (File.Exists(Folder_loacation +"''TrainedLabels.xml"))
{
try
{
//message_bar.Text = "";
Names_List.Clear();
trainingImages.Clear();
FileStream filestream = File.OpenRead(Folder_loacation + "''TrainedLabels.xml");
long filelength = filestream.Length;
byte[] xmlBytes = new byte[filelength];
filestream.Read(xmlBytes, 0, (int)filelength);
filestream.Close();
MemoryStream xmlStream = new MemoryStream(xmlBytes);
using (XmlReader xmlreader = XmlTextReader.Create(xmlStream))
{
while (xmlreader.Read())
{
if (xmlreader.IsStartElement())
{
switch (xmlreader.Name)
{
case "NAME":
if (xmlreader.Read())
{
Names_List.Add(xmlreader.Value.Trim());
NumLabels += 1;
}
break;
case "FILE":
if (xmlreader.Read())
{
//PROBLEM HERE IF TRAININGG MOVED
trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "''TrainedFaces''" + xmlreader.Value.Trim()));
}
break;
}
}
}
}
ContTrain = NumLabels;
if (trainingImages.ToArray().Length != 0)
{
//Eigen face recognizer
recognizer = new EigenObjectRecognizer(trainingImages.ToArray(),
Names_List.ToArray(), 5000, ref termCrit); //5000 default
return true;
}
else return false;
}
catch (Exception ex)
{
Error = ex.ToString();
return false;
}
}
else return false;
}
#endregion
}`
有一篇关于Microsoft对您的主题进行研究的非常好的论文:http://research.microsoft.com/en-us/um/people/ablake/papers/ablake/romdhani_iccv01.pdf
这一点,你应该作为一个起点。之后,您应该了解图像处理和直接x图像转换:http://www.c-sharpcorner.com/UploadFile/ShrutiShrivastava/ImageProcessing12192005061519AM/ImageProcessing.aspx
问候,