Skip to content

Instantly share code, notes, and snippets.

@yongjun823
Created July 5, 2018 07:23
Show Gist options
  • Select an option

  • Save yongjun823/a0eb816b75727d1aa4c621371d9a5efd to your computer and use it in GitHub Desktop.

Select an option

Save yongjun823/a0eb816b75727d1aa4c621371d9a5efd to your computer and use it in GitHub Desktop.
obj bbox dataset slice
from PIL import Image
from pprint import pprint
import xmltodict
with open('pizza.xml') as fd:
doc = xmltodict.parse(fd.read())
file_path = doc['annotation']['path']
main_img = Image.open(file_path)
obj_arr = doc['annotation']['object']
for idx, obj in enumerate(obj_arr):
pprint(obj)
name_class = obj['name']
box = obj['bndbox']
img = main_img.crop((int(box['xmin']), int(box['ymin']), int(box['xmax']), int(box['ymax']))) \
.save('{}-{}.jpg'.format(name_class, idx))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment