Skip to content

Instantly share code, notes, and snippets.

@Theoistic
Created June 28, 2022 02:48
Show Gist options
  • Save Theoistic/3298bf596790dfdcac2677522f2a724c to your computer and use it in GitHub Desktop.
Save Theoistic/3298bf596790dfdcac2677522f2a724c to your computer and use it in GitHub Desktop.
Pascal VOC to Yolo txt converter in C#
using System.Globalization;
using System.Text;
using System.Xml.Serialization;
string annotationdir = @"D:\Work\Datasets\AnnotationsXML\001plate";
string imagedir = @"D:\Work\Datasets\Images\001plate";
string[] annotations = Directory.GetFiles(annotationdir);
string[] images = Directory.GetFiles(imagedir);
var culture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentCulture = culture;
foreach(var annotation in annotations)
{
string data = File.ReadAllText(annotation);
Annotation d = (Annotation)XmlDeserializeFromString<Annotation>(data);
StringBuilder sb = new StringBuilder();
foreach(var obj in d.Object)
{
var x_coord = (obj.Bndbox.Xmin + obj.Bndbox.Xmax) / 2 / d.Size.Width;
var y_coord = (obj.Bndbox.Ymin + obj.Bndbox.Ymax) / 2 / d.Size.Height;
var shape_width = (obj.Bndbox.Xmax - obj.Bndbox.Xmin) / d.Size.Width;
var shape_height = (obj.Bndbox.Ymax - obj.Bndbox.Ymin) / d.Size.Height;
sb.AppendLine($"1 {x_coord.ToString("N7")} {y_coord.ToString("N7")} {shape_width.ToString("N7")} {shape_height.ToString("N7")}");
}
string filename = Path.GetFileNameWithoutExtension(d.Filename) + ".txt";
File.WriteAllText(Path.Combine(imagedir, filename), sb.ToString());
}
static object XmlDeserializeFromString<T>(string objectData)
{
var serializer = new XmlSerializer(typeof(T));
object result;
using (TextReader reader = new StringReader(objectData))
{
result = serializer.Deserialize(reader);
}
return result;
}
[XmlRoot(ElementName = "source")]
public class Source
{
[XmlElement(ElementName = "database")]
public string Database { get; set; }
}
[XmlRoot(ElementName = "size")]
public class Size
{
[XmlElement(ElementName = "width")]
public float Width { get; set; }
[XmlElement(ElementName = "height")]
public float Height { get; set; }
[XmlElement(ElementName = "depth")]
public int Depth { get; set; }
}
[XmlRoot(ElementName = "bndbox")]
public class Bndbox
{
[XmlElement(ElementName = "xmin")]
public float Xmin { get; set; }
[XmlElement(ElementName = "ymin")]
public float Ymin { get; set; }
[XmlElement(ElementName = "xmax")]
public float Xmax { get; set; }
[XmlElement(ElementName = "ymax")]
public float Ymax { get; set; }
}
[XmlRoot(ElementName = "object")]
public class Object
{
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "pose")]
public string Pose { get; set; }
[XmlElement(ElementName = "truncated")]
public string Truncated { get; set; }
[XmlElement(ElementName = "difficult")]
public string Difficult { get; set; }
[XmlElement(ElementName = "bndbox")]
public Bndbox Bndbox { get; set; }
}
[XmlRoot(ElementName = "annotation")]
public class Annotation
{
[XmlElement(ElementName = "folder")]
public string Folder { get; set; }
[XmlElement(ElementName = "filename")]
public string Filename { get; set; }
[XmlElement(ElementName = "path")]
public string Path { get; set; }
[XmlElement(ElementName = "source")]
public Source Source { get; set; }
[XmlElement(ElementName = "size")]
public Size Size { get; set; }
[XmlElement(ElementName = "segmented")]
public string Segmented { get; set; }
[XmlElement(ElementName = "object")]
public List<Object> Object { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment