Skip to content

Commit

Permalink
Renamed format functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Jan 27, 2019
1 parent 64c84a7 commit 84cfdfe
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 96 deletions.
105 changes: 41 additions & 64 deletions examples/COCO Format.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,55 +49,14 @@
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<annotation>\n",
" <folder>/datasets/Car/car</folder>\n",
" <path>/datasets/Car/car/2015-tesla-model-s-90d-black-profile-in-front-of-modern-house.jpg</path>\n",
" <filename>2015-tesla-model-s-90d-black-profile-in-front-of-modern-house.jpg</filename>\n",
" <size>\n",
" <width>900</width>\n",
" <height>600</height>\n",
" <depth>3</depth>\n",
" </size>\n",
" <object>\n",
" <name>wheel</name>\n",
" <pose>Unspecified</pose>\n",
" <truncated>1</truncated>\n",
" <difficult>0</difficult>\n",
" <bndbox>\n",
" <xmin>242</xmin>\n",
" <ymin>310</ymin>\n",
" <xmax>836</xmax>\n",
" <ymax>458</ymax>\n",
" </bndbox>\n",
" </object>\n",
" <object>\n",
" <name>glass</name>\n",
" <pose>Unspecified</pose>\n",
" <truncated>1</truncated>\n",
" <difficult>0</difficult>\n",
" <bndbox>\n",
" <xmin>263</xmin>\n",
" <ymin>188</ymin>\n",
" <xmax>720</xmax>\n",
" <ymax>256</ymax>\n",
" </bndbox>\n",
" </object>\n",
"</annotation>\n",
"\n"
]
}
],
"source": [
"for image in dataset.iter_images():\n",
" draw = image.draw(bbox=False)\n",
" plt.imshow(draw)\n",
" plt.show()\n",
" print(etree.tostring(image.export(style='voc'), pretty_print=True).decode('utf-8'))"
" etree.tostring(image.export(style='voc'), pretty_print=True).decode('utf-8')"
]
},
{
Expand All @@ -109,8 +68,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'categories': [{'id': 1, 'name': 'wheel', 'supercategory': None, 'metadata': {}}]}\n",
"{'categories': [{'id': 2, 'name': 'glass', 'supercategory': None, 'metadata': {}}]}\n"
"{'categories': [{'id': 1, 'name': 'wheel', 'supercategory': None, 'metadata': {}, 'color': '#fa2200'}]}\n",
"{'categories': [{'id': 2, 'name': 'glass', 'supercategory': None, 'metadata': {}, 'color': '#ac17e4'}]}\n"
]
}
],
Expand All @@ -123,8 +82,30 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": false
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"/datasets/Car/car/2015-tesla-model-s-90d-black-profile-in-front-of-modern-house.jpg\": [\n",
" \"1 0.26889 0.51667 0.66000 0.24667\",\n",
" \"2 0.29222 0.31333 0.50778 0.11333\"\n",
" ]\n",
"}\n"
]
}
],
"source": [
"print(json.dumps(dataset.yolo(), indent=4))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand All @@ -134,24 +115,18 @@
" \"info\": {},\n",
" \"categories\": [\n",
" {\n",
" \"categories\": [\n",
" {\n",
" \"id\": 1,\n",
" \"name\": \"wheel\",\n",
" \"supercategory\": null,\n",
" \"metadata\": {}\n",
" }\n",
" ]\n",
" \"id\": 1,\n",
" \"name\": \"wheel\",\n",
" \"supercategory\": null,\n",
" \"metadata\": {},\n",
" \"color\": \"#fa2200\"\n",
" },\n",
" {\n",
" \"categories\": [\n",
" {\n",
" \"id\": 2,\n",
" \"name\": \"glass\",\n",
" \"supercategory\": null,\n",
" \"metadata\": {}\n",
" }\n",
" ]\n",
" \"id\": 2,\n",
" \"name\": \"glass\",\n",
" \"supercategory\": null,\n",
" \"metadata\": {},\n",
" \"color\": \"#ac17e4\"\n",
" }\n",
" ],\n",
" \"images\": [\n",
Expand Down Expand Up @@ -297,7 +272,8 @@
" 594,\n",
" 148\n",
" ],\n",
" \"metadata\": {}\n",
" \"metadata\": {},\n",
" \"color\": \"#a42559\"\n",
" },\n",
" {\n",
" \"id\": 2,\n",
Expand Down Expand Up @@ -424,15 +400,16 @@
" 457,\n",
" 68\n",
" ],\n",
" \"metadata\": {}\n",
" \"metadata\": {},\n",
" \"color\": \"#b403dd\"\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"print(json.dumps(dataset.export(), indent=4))"
"print(json.dumps(dataset.coco(), indent=4))"
]
},
{
Expand Down
13 changes: 7 additions & 6 deletions imantics/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def contains(self, item):
def __contains__(self, item):
return self.contains(item)

def _coco(self, include=True):
def coco(self, include=True):
"""
Generates COCO format of annotation
Expand All @@ -263,16 +263,17 @@ def _coco(self, include=True):
'area': int(self.area),
'segmentation': self.polygons.segmentation,
'bbox': self.bbox.bbox(style=BBox.WIDTH_HEIGHT),
'metadata': self.metadata
'metadata': self.metadata,
'color': self.color.hex
}

if include:
image = category = {}
if self.image:
image = self.image._coco(include=False)
image = self.image.coco(include=False)

if self.category:
category = self.category._coco()
category = self.category.coco()

return {
'categories': [category],
Expand All @@ -282,7 +283,7 @@ def _coco(self, include=True):

return annotation

def _yolo(self):
def yolo(self):

height = self.bbox.height / self.image.height
width = self.bbox.width / self.image.width
Expand All @@ -292,7 +293,7 @@ def _yolo(self):

return "{} {:.5f} {:.5f} {:.5f} {:.5f}".format(label, x, y, width, height)

def _voc(self):
def voc(self):

element = E('object',
E('name', self.category.name),
Expand Down
20 changes: 10 additions & 10 deletions imantics/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ def __init__(self, id, metadata={}):
self.id = id
self.metadata = metadata

def _coco(self):
def coco(self):
return {}

def _vgg(self):
def vgg(self):
return []

def _voc(self):
def voc(self):
return None

def _yolo(self):
def yolo(self):
return []

def _paperjs(self):
def paperjs(self):
return {}

def export(self, style=COCO):
"""
Exports object into specified style
"""
return {
COCO: self._coco(),
VGG: self._vgg(),
YOLO: self._yolo(),
VOC: self._voc(),
PAPERJS: self._paperjs()
COCO: self.coco(),
VGG: self.vgg(),
YOLO: self.yolo(),
VOC: self.voc(),
PAPERJS: self.paperjs()
}.get(style)

def save(self, file):
Expand Down
5 changes: 3 additions & 2 deletions imantics/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ def __init__(self, name, parent=None, metadata={}, id=0, color=None):

super().__init__(id, metadata)

def _coco(self, include=True):
def coco(self, include=True):

category = {
'id': self.id,
'name': self.name,
'supercategory': self.parent.name if self.parent else None,
'metadata': self.metadata
'metadata': self.metadata,
'color': self.color.hex
}

if include:
Expand Down
12 changes: 6 additions & 6 deletions imantics/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ def split(self, ratios, random=False):

print(percents)

def _coco(self):
def coco(self):
coco = {
'info': {},
'categories': [c._coco() for c in self.iter_categories()],
'images': [i._coco(include=False) for i in self.iter_images()],
'annotations': [a._coco(include=False) for a in self.iter_annotations()]
'categories': [c.coco(include=False) for c in self.iter_categories()],
'images': [i.coco(include=False) for i in self.iter_images()],
'annotations': [a.coco(include=False) for a in self.iter_annotations()]
}

return coco

def _yolo(self):
def yolo(self):
yolo = {}

for image in self.iter_images():
yolo[image.path] = image._yolo()
yolo[image.path] = image.yolo()

return yolo

Expand Down
17 changes: 10 additions & 7 deletions imantics/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def iter_categories(self):
for key, category in self.categories.items():
yield category

def _coco(self, include=True):
def coco(self, include=True):
image = {
'id': self.id,
'width': self.width,
Expand All @@ -208,12 +208,12 @@ def _coco(self, include=True):
categories = []
for category in self.iter_categories():
category.id = len(categories) + 1
categories.append(category._coco(include=False))
categories.append(category.coco(include=False))

annotations = []
for annotation in self.iter_annotations():
annotation.id = len(annotations) + 1
annotations.append(annotation._coco(include=False))
annotations.append(annotation.coco(include=False))

return {
'categories': categories,
Expand All @@ -223,7 +223,7 @@ def _coco(self, include=True):

return image

def _yolo(self):
def yolo(self):
yolo = []

categories = []
Expand All @@ -232,15 +232,15 @@ def _yolo(self):
categories.append(category)

for annotation in self.iter_annotations():
yolo.append(annotation._yolo())
yolo.append(annotation.yolo())

return yolo

def _voc(self):
def voc(self, pretty=False):

annotations = []
for annotation in self.iter_annotations():
annotations.append(annotation._voc())
annotations.append(annotation.voc())

element = E('annotation',
E('folder', self.path[: -1*(len(self.file_name)+1)]),
Expand All @@ -254,6 +254,9 @@ def _voc(self):
*annotations
)

if pretty:
return ET.tostring(element, pretty_print=True).decode('utf-8')

return element

def save(self, file_path, style=COCO):
Expand Down
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from setuptools import setup
import subprocess


def get_tag():
result = subprocess.run(["git", "describe", "--abbrev=0", "--tags"], stdout=subprocess.PIPE)
return str(result.stdout.decode("utf-8")).strip()[1:]


with open("README.md", "r") as fh:
long_description = fh.read()


setup(
name='imantics',
version='0.1.3',
version=get_tag(),
description='Python package for managing image annotations',
url='https://github.com/jsbroks/imantics',
author='Justin Brooks',
Expand Down

0 comments on commit 84cfdfe

Please sign in to comment.