Skip to content

Instantly share code, notes, and snippets.

@mcowger
Created August 9, 2019 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcowger/4b2afca3a5ed9b420fa91a89413ac945 to your computer and use it in GitHub Desktop.
Save mcowger/4b2afca3a5ed9b420fa91a89413ac945 to your computer and use it in GitHub Desktop.
import xmltodict
from pprint import pprint
with open('thumb0001.xml') as fd:
doc = xmltodict.parse(fd.read())
def remap(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
width = int(doc['annotation']['size']['width'])
height = int(doc['annotation']['size']['height'])
label = doc['annotation']['object']['name']
xmin = int(doc['annotation']['object']['bndbox']['xmin'])
ymin = int(doc['annotation']['object']['bndbox']['ymin'])
xmax = int(doc['annotation']['object']['bndbox']['xmax'])
ymax = int(doc['annotation']['object']['bndbox']['ymax'])
scaled_xmin = remap(xmin, 0, width, 0, 1)
scaled_ymin = remap(ymin, 0, height, 0, 1)
scaled_xmax = remap(xmax, 0, width, 0, 1)
scaled_ymax = remap(ymax, 0, height, 0, 1)
# print("Object: '{}' found @ ({},{})x({},{})".format(
# label, scaled_xmin, scaled_ymin, scaled_xmax, scaled_ymax
# ))
print("TRAIN,gs://bucketname/imagename.jpg,{},{},{},,,{},{},,".format(
label, scaled_xmin, scaled_ymin, scaled_xmax, scaled_ymax
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment