Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom dataset from LabelImg #908

Closed
RomRoc opened this issue Feb 18, 2019 · 1 comment
Closed

Create custom dataset from LabelImg #908

RomRoc opened this issue Feb 18, 2019 · 1 comment

Comments

@RomRoc
Copy link

RomRoc commented Feb 18, 2019

I developed a simple python script to transform annotations from LabelImg tool to Keras-Retinanet annotations format.

You can run it even in a notebook. Xml annotations image files should stay into the same DATASET_DIR.
I share it here in case it can be useful to someone else:

import xml.etree.ElementTree as ET
import numpy as np
import csv

DATASET_DIR = '/path/to/dataset'

annotations = []
classes = set([])

for xml_file in [f for f in os.listdir(DATASET_DIR) if f.endswith(".xml")]:
  tree = ET.parse(os.path.join(DATASET_DIR, xml_file))
  root = tree.getroot()

  file_name = None

  for elem in root:
    if elem.tag == 'filename':
      file_name = os.path.join(DATASET_DIR, elem.text)

    if elem.tag == 'object':
      obj_name = None
      coords = []
      for subelem in elem:
        if subelem.tag == 'name':
          obj_name = subelem.text
        if subelem.tag == 'bndbox':
          for subsubelem in subelem:
            coords.append(subsubelem.text)
      item = [file_name] + coords + [obj_name]
      annotations.append(item)
      classes.add(obj_name)

with open(ANNOTATIONS_FILE, 'w') as f:
  writer = csv.writer(f)
  writer.writerows(annotations)

with open(CLASSES_FILE, 'w') as f:
  for i, line in enumerate(classes):
    f.write('{},{}\n'.format(line,i))
@hgaiser
Copy link
Contributor

hgaiser commented Feb 19, 2019

Thank you for your interest and for sharing it, but I'm afraid I'll have to close this issue (as there isn't really an issue to be resolved). The issue will still be accessible, so anyone searching for this should be able to find it. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants