PubFig face database 链接(http://www.cs.columbia.edu/CAVE/databases/pubfig/)
是一个之前一直想下载的人脸数据库,数据库里面的人脸只提供url并没提供可以下载的是图片,之前因为想要一张一张的自己下非常的累人所以就没有下载过。
之前在博客里面分享了一个利用网络url直接保存文件的博客,利用里面的方法我也写了一个自动保存PubFig的程序,话不多说,直接上代码。
using System.Net; // for WebClient
using System.IO; // for MemoryStream
using System.Text;// for Encoding
using System.Drawing;// for image
private void button1_Click(object sender, EventArgs e)
{
//downloading image from urls
WebClient wc = new WebClient();// key member of downloading
// wc.
MemoryStream ms;
StreamReader m_StreamReader = new StreamReader("..\\..\\eval_urls.txt");// open the txt file with C# form
// 没有用之前的fopen了
while(true)
{
String temp_line = m_StreamReader.ReadLine();// read one line of txt file 用的挺爽
if (temp_line == null)
{
break;// break when reach the end of file;
}
String []parts = temp_line.Split('\t');// split the readed line by '\t'
if ((parts[0].IndexOf('#'))!=-1)
{
continue;// the first and second row of image is useless so discard them
}
String ImgSavePath = ".\\"+parts[0] + "\\";
if (!Directory.Exists(ImgSavePath))
{
Directory.CreateDirectory(ImgSavePath);// create the Directiory when necessary
}
ImgSavePath += parts[0] + "_" + parts[1] + ".jpg";// path of saving image
try
{
wc.DownloadFile(parts[2], ImgSavePath); // using the DownloadFile function instead of DownloadData
// ms = new MemoryStream(wc.DownloadData(parts[2]));
// Image img = Image.FromStream(ms);
// img.Save(ImgSavePath);
}
catch (System.Exception ex)
{
<span style="white-space:pre"> </span>// catch the exception when the image is invalid
}
}
}
总结一下哈
1 c#还是很好用的,很多内存管理的事情都可以不用考虑了,这对于我这种常年写c的人来说真是福音啊
2 DownloadFile 这个函数貌似比共享的博客里面的DownloadData 这个函数要好用一些,直接输入url 和保存的文件名就可以直接下载了
3 好多国外的网站的图片都找不到了,吐槽一下这个库,估计是下不全。
骚年们,有了库就可以干活了,赶紧去干活吧。