zoukankan      html  css  js  c++  java
  • c#解析XML文件来获得pascal_voc特定目标负样本

    近期在做船仅仅识别方面的事情,须要大量的负样本来训练adaboost分类器。

    我从网上下载到一个pascal_voc的数据集。须要找到不包括船仅仅的那些复制出来。

    数据集特点

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

    
    <annotation>
        <folder>VOC2007</folder>
        <filename>000001.jpg</filename>
        <source>
            <database>The VOC2007 Database</database>
            <annotation>PASCAL VOC2007</annotation>
            <image>flickr</image>
            <flickrid>341012865</flickrid>
        </source>
        <owner>
            <flickrid>Fried Camels</flickrid>
            <name>Jinky the Fruit Bat</name>
        </owner>
        <size>
            <width>353</width>
            <height>500</height>
            <depth>3</depth>
        </size>
        <segmented>0</segmented>
        <object>
            <name>dog</name>
            <pose>Left</pose>
            <truncated>1</truncated>
            <difficult>0</difficult>
            <bndbox>
            <xmin>48</xmin>
            <ymin>240</ymin>
            <xmax>195</xmax>
            <ymax>371</ymax>
            </bndbox>
        </object>
        <object>
            <name>person</name>
            <pose>Left</pose>
            <truncated>1</truncated>
            <difficult>0</difficult>
            <bndbox>
            <xmin>8</xmin>
            <ymin>12</ymin>
            <xmax>352</xmax>
            <ymax>498</ymax>
            </bndbox>
        </object>
    </annotation>

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

    解决方式

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

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace 获取负样本
    {
        class Program
        {
            static void Main(string[] args)
            {
                /*首先获取xml文件夹下的全部文件的文件夹列表和名称*/
                //List<string> fileNames = new List<string>();//存储文件名称;
                //C:UsersdragonfiveDesktoppascal_vocVOCtrainval_06-Nov-2007VOCdevkitVOC2007Annotations
                string path = @"C:UsersdragonfiveDesktoppascal_vocVOCtrainval_06-Nov-2007VOCdevkitVOC2007Annotations";
                string imageSourcePath = @"C:UsersdragonfiveDesktoppascal_vocVOCtrainval_06-Nov-2007VOCdevkitVOC2007JPEGImages";
                string imageDestPath  = @"D:IP_CV_WorkSpaceImgNegSample";
                int numberOfNegSample = 0;
                foreach (var file in  Directory.GetFiles(path,"*.xml"))//这个获取的文件名称带前面的文件夹;
                {
                    //Console.WriteLine(file);
                    //获取该路径的不带扩展名的文件名称;
                    string fileName = Path.GetFileNameWithoutExtension(file);
                    //Console.WriteLine(fileName);
                    //以下读取xml的内容
                    //string xmlData = File.ReadAllText(file, Encoding.Default);
                    //Console.WriteLine(xmlData);
                    /*循环完毕每一个xml文件的解析,假设没有boat就拷贝到新的文件夹*/
                    XmlDocument doc = new XmlDocument();
                    doc.Load(file);
                    XmlElement root = doc.DocumentElement;
    
                    XmlNodeList listNodes = root.SelectNodes("/annotation/object/name");
                    bool hasBoat = false;
                    foreach (XmlNode node in listNodes )
                    {
                        //Console.WriteLine(node.InnerText);
                        //假设当中含有boat就continue,否则赋值到负样本的位置;
                        if (node.InnerText == "boat")
                        {
                            hasBoat = true;
                            Console.WriteLine(fileName+"里面有船");
                            break;
                        }   
                    }
                    if (hasBoat==false)
                    {
                        //复制
                        File.Copy(imageSourcePath + fileName + ".jpg", imageDestPath + fileName + ".jpg",true);
                        Console.WriteLine("成功复制"+fileName);
                        numberOfNegSample++;
                    }
    
                    //Console.ReadKey();
                }
    
                Console.WriteLine("共计复制负样本个数为:" + numberOfNegSample);
                Console.ReadKey();
            }
        }
    }
    
  • 相关阅读:
    wifite+aimon-ng
    DC-2
    chrome插件开发
    mongoose的基本操作方法
    webpack中的require.context
    sequelize 数据类型 model
    React17 系统精讲 结合TS打造旅游电商平台
    2021必修 React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目
    21.8.2
    胡渊鸣《浅析信息学竞赛中概率论的基础与应用》学习笔记
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7363216.html
Copyright © 2011-2022 走看看