[关闭]
@dragonfive 2015-12-06T05:41:15.000000Z 字数 2590 阅读 610

c#解析XML文件来获得pascal_voc特定目标负样本

c#编程


最近在做船只识别方面的事情,需要大量的负样本来训练adaboost分类器。我从网上下载到一个pascal_voc的数据集,需要找到不包含船只的那些复制出来。

数据集特点

对于每个图片有一个xml文件,介绍了这个文件的信息,有个object标签介绍了图片中目标类别
老人与狗
比如上面这副图片的xml文件为:

  1. <annotation>
  2. <folder>VOC2007</folder>
  3. <filename>000001.jpg</filename>
  4. <source>
  5. <database>The VOC2007 Database</database>
  6. <annotation>PASCAL VOC2007</annotation>
  7. <image>flickr</image>
  8. <flickrid>341012865</flickrid>
  9. </source>
  10. <owner>
  11. <flickrid>Fried Camels</flickrid>
  12. <name>Jinky the Fruit Bat</name>
  13. </owner>
  14. <size>
  15. <width>353</width>
  16. <height>500</height>
  17. <depth>3</depth>
  18. </size>
  19. <segmented>0</segmented>
  20. <object>
  21. <name>dog</name>
  22. <pose>Left</pose>
  23. <truncated>1</truncated>
  24. <difficult>0</difficult>
  25. <bndbox>
  26. <xmin>48</xmin>
  27. <ymin>240</ymin>
  28. <xmax>195</xmax>
  29. <ymax>371</ymax>
  30. </bndbox>
  31. </object>
  32. <object>
  33. <name>person</name>
  34. <pose>Left</pose>
  35. <truncated>1</truncated>
  36. <difficult>0</difficult>
  37. <bndbox>
  38. <xmin>8</xmin>
  39. <ymin>12</ymin>
  40. <xmax>352</xmax>
  41. <ymax>498</ymax>
  42. </bndbox>
  43. </object>
  44. </annotation>

比如上面这个就包括dog和person

解决方案

我们需要做的就是这道叶子节点里的name看是不是boat如果不是的话就取到这个图片

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. namespace 获取负样本
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. /*首先获取xml目录下的所有文件的目录列表和名称*/
  15. //List<string> fileNames = new List<string>();//存储文件名;
  16. //C:\Users\dragonfive\Desktop\pascal_voc\VOCtrainval_06-Nov-2007\VOCdevkit\VOC2007\Annotations
  17. string path =@"C:\Users\dragonfive\Desktop\pascal_voc\VOCtrainval_06-Nov-2007\VOCdevkit\VOC2007\Annotations\";
  18. string imageSourcePath = @"C:\Users\dragonfive\Desktop\pascal_voc\VOCtrainval_06-Nov-2007\VOCdevkit\VOC2007\JPEGImages\";
  19. string imageDestPath = @"D:\IP_CV_WorkSpace\Img\NegSample\";
  20. int numberOfNegSample = 0;
  21. foreach (var file in Directory.GetFiles(path,"*.xml"))//这个获取的文件名带前面的目录;
  22. {
  23. //Console.WriteLine(file);
  24. //获取该路径的不带扩展名的文件名;
  25. string fileName = Path.GetFileNameWithoutExtension(file);
  26. //Console.WriteLine(fileName);
  27. //下面读取xml的内容
  28. //string xmlData = File.ReadAllText(file, Encoding.Default);
  29. //Console.WriteLine(xmlData);
  30. /*循环完成每个xml文件的解析,如果没有boat就复制到新的目录*/
  31. XmlDocument doc = new XmlDocument();
  32. doc.Load(file);
  33. XmlElement root = doc.DocumentElement;
  34. XmlNodeList listNodes = root.SelectNodes("/annotation/object/name");
  35. bool hasBoat = false;
  36. foreach (XmlNode node in listNodes )
  37. {
  38. //Console.WriteLine(node.InnerText);
  39. //如果其中含有boat就continue,否则赋值到负样本的位置;
  40. if (node.InnerText == "boat")
  41. {
  42. hasBoat = true;
  43. Console.WriteLine(fileName+"里面有船");
  44. break;
  45. }
  46. }
  47. if (hasBoat==false)
  48. {
  49. //复制
  50. File.Copy(imageSourcePath + fileName + ".jpg", imageDestPath + fileName + ".jpg",true);
  51. Console.WriteLine("成功复制"+fileName);
  52. numberOfNegSample++;
  53. }
  54. //Console.ReadKey();
  55. }
  56. Console.WriteLine("共计复制负样本个数为:" + numberOfNegSample);
  57. Console.ReadKey();
  58. }
  59. }
  60. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注