Skip to content

Instantly share code, notes, and snippets.

@Corneliuskruger
Last active January 11, 2019 20:53
Show Gist options
  • Save Corneliuskruger/dc7a52650e24f47d4d87b164e51de16f to your computer and use it in GitHub Desktop.
Save Corneliuskruger/dc7a52650e24f47d4d87b164e51de16f to your computer and use it in GitHub Desktop.
Console app to fix data in the Stanford Dogs Dataset
using System;
using System.Linq;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Specify the abolute directory where the annotations are located.");
Console.WriteLine(@"i.e. c:\temp\breeds\annotation");
var annotationDirectory = Console.ReadLine();
foreach (var dir in Directory.EnumerateDirectories(annotationDirectory))
{
var newFolderName = dir.Substring(dir.LastIndexOf('\\') + 1);
newFolderName = newFolderName.Substring(1, newFolderName.IndexOf('-') - 1);
foreach (var file in Directory.EnumerateFiles(dir))
{
var path = Path.Combine(dir, file);
XDocument doc = XDocument.Load(path);
var root = from k in doc.Elements("annotation")
select k;
var folder = root.Elements("folder").FirstOrDefault();
var filename = root.Elements("filename").FirstOrDefault();
if (folder.Value.ToLower() == "%s" || filename.Value.ToLower() == "%s")
{
var name = Path.GetFileNameWithoutExtension(file);
var filenumber = name.Substring(name.IndexOf('_') + 1);
folder.Value = newFolderName;
filename.Value = $"n{newFolderName}_{filenumber}";
doc.Save(path);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment