我把视频通过人脸检测进行截图 把每15帧图片的人脸图像保存出来存到文件夹并按数字顺序命名!
为了便于筛选又写了一个小程序,便于展示图片并且进行分类!
可是期间发现在c#中 Directory.GetFiles(dialog.SelectedPath)返回的是一个string[] 而且顺序是按照ascii码表排序的!
顺序如下:
0.jpg
1.jpg
10.jpg
100.jpg
11.jpg
12.jpg
......
逛了很多论坛之后找到了解决的方法:
先要using System.Text.RegularExpressions;
path = Directory.GetFiles(dialog.SelectedPath).OrderBy(s => int.Parse(Regex.Match(s, @"\d+").Value)).ToArray();//只能匹配第一组数字
path = Directory.GetFiles(dialog.SelectedPath).OrderBy(s => int.Parse(Regex.Match(s, @"[0-9]+(?=[^0-9]*$)").Value)).ToArray();//可以匹配最后一组数字
因为getfiles之后得到的是文件绝对路径所以如果路径中有数字的话就只能用第二种方法了 当然文件名字一定要是数字才行!!!
/// <summary>
/// 将图片进行手工分类/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择图片所在的文件夹(路径不能出现数字)";
if (dialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(dialog.SelectedPath))
{
MessageBox.Show(this, "文件夹路径不能为空", "提示");
return;
}
// path = Directory.GetFiles(dialog.SelectedPath).OrderBy(s => int.Parse(Regex.Match(s, @"\d+").Value)).ToArray();//只能匹配第一组数字
path = Directory.GetFiles(dialog.SelectedPath).OrderBy(s => int.Parse(Regex.Match(s, @"[0-9]+(?=[^0-9]*$)").Value)).ToArray();//可以匹配最后一组数字
if (path.Length > 0){
pictureBox1.Image = Image.FromFile(path[0]);
}
}
FolderBrowserDialog dialog2 = new FolderBrowserDialog();
dialog2.Description = "请要保存的文件夹";
if (dialog2.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(dialog2.SelectedPath))
{
MessageBox.Show(this, "文件夹路径不能为空", "提示");
return;
}
subpath = dialog2.SelectedPath;
happy = Directory.GetFiles(@subpath + @"\happy\").Length;
sad = Directory.GetFiles(@subpath + @"\sad\").Length;
angry = Directory.GetFiles(@subpath + @"\angry\").Length;
disgust = Directory.GetFiles(@subpath + @"\disgust\").Length;
surprise = Directory.GetFiles(@subpath + @"\surprise\").Length;
fear = Directory.GetFiles(@subpath + @"\fear\").Length;
neatral = Directory.GetFiles(@subpath + @"\neatral\").Length;
other = Directory.GetFiles(@subpath + @"\other\").Length;
ifFenLei = true;
}
}