kwcoco.formats.labelme module

Helpers for labelme files

kwcoco.formats.labelme.labelme_to_coco_structure(labelme_data, special_options=True)[source]

Helper to convert labelme data into dictionaries suitable for adding to a CocoDataset.

Parameters:

labelme_data (dict) – data read from a labelme json file.

Example

>>> from kwcoco.formats.labelme import *  # NOQA
>>> labelme_data = {
>>>     'flags': {},
>>>     'imageData': None,
>>>     'imageHeight': 4032,
>>>     'imagePath': 'filename.jpg',
>>>     'imageWidth': 3024,
>>>     'shapes': [
>>>         {
>>>             'description': '',
>>>             'flags': {},
>>>             'group_id': None,
>>>             'label': 'category1',
>>>             'points': [[1527.0, 2319.5], [1512.0, 2317.5], [1503.5, 2295.0], [1568.5, 2243.0], [1561.5, 2278.0], [1548.5, 2307.0], [1541.0, 2315.5]],
>>>             'shape_type': 'polygon',
>>>         },
>>>         {
>>>             'description': '',
>>>             'flags': {},
>>>             'group_id': None,
>>>             'label': 'category1',
>>>             'points': [[1346.0, 2285.5], [1318.0, 2282.5], [1370.5, 2241.0], [1360.5, 2258.0], [1357.5, 2272.0], [1354.5, 2278.0]],
>>>             'shape_type': 'polygon',
>>>         },
>>>         {
>>>             'description': 'image level description',
>>>             'flags': {},
>>>             'group_id': None,
>>>             'label': '__metadata__',
>>>             'points': [[1346.0, 2285.5]],
>>>             'shape_type': 'point',
>>>         },
>>>     ],
>>>     'version': '5.3.1',
>>> }
>>> img, anns = labelme_to_coco_structure(labelme_data)
>>> print(f'img = {ub.urepr(img, nl=1)}')
>>> print(f'anns = {ub.urepr(anns, nl=2)}')
class kwcoco.formats.labelme.LabelMeFile(data, fpath=None)[source]

Bases: NiceRepr

Helper class to manage and create a LabelMe JSON file.

SeeAlso:

~/code/labelme/labelme/label_file.py ~/code/labelme/labelme/shape.py

Example

>>> # xdoctest: +REQUIRES(module:kwutil)
>>> from kwcoco.formats.labelme import LabelMeFile
>>> self = LabelMeFile.demo()
>>> print(self.dumps())

Initialize the LabelMe file structure.

See LabelMeFile.new() to create an empty file to populate

Parameters:
  • data (Dict) – the labelme dictionary

  • fpath (str | PathLike | None) – The parent of this path determines where relative paths are resolved from. This is also where the data will be written on a dump.

classmethod demo()[source]

Create an instance of this class for demos and tests

Returns:

LabeMeFile

classmethod empty(image_path=None, image_height=None, image_width=None, fpath=None)[source]

Create a new empty file for a specific image.

Returns:

LabeMeFile

Example

>>> from kwcoco.formats.labelme import *  # NOQA
>>> self = LabelMeFile.empty('foo.png')
>>> print(f'self={self}')
classmethod load(file)[source]

Load a file from a path.

Returns:

LabeMeFile

classmethod multiple_from_coco(coco_dset)[source]
reroot(absolute=True)[source]
classmethod from_coco(coco_dset, image_id=None)[source]

Convert an image in a CocoDataset into a LabeMeFile.

Parameters:
  • coco_dset (CocoDataset) – dataset to convert to labelme

  • image_id (int | None) – The image in the cocodataset to convert to a labelme file. if unspecified, the dataset must have one image in it, otherwise

    we raise an error.

Returns:

LabeMeFile

Example

>>> import kwcoco
>>> from kwcoco.formats.labelme import LabelMeFile
>>> coco_dset = kwcoco.CocoDataset.demo('vidshapes8')
>>> image_id = sorted(coco_dset.images())[0]
>>> self = LabelMeFile.from_coco(coco_dset, image_id)
>>> coco_recon = self.to_coco()
>>> recon = LabelMeFile.from_coco(coco_recon)
>>> # Need to ignore the "extra" data
>>> for shape in recon.data['shapes']:
...     shape.pop('extra')
>>> for shape in self.data['shapes']:
...     shape.pop('extra')
>>> assert self.data == recon.data
add_to_coco(coco_dset)[source]

Add the information in this labelme file to an existing coco file.

Parameters:

coco_dset (CocoDataset) – the dataset to add to

SeeAlso:

LabelMeFile.to_coco

to_coco()[source]

Convert this labelme file into a standalone coco dataset.

Returns:

CocoDataset

SeeAlso:

LabelMeFile.add_to_coco

Example

>>> # xdoctest: +REQUIRES(module:kwutil)
>>> from kwcoco.formats.labelme import LabelMeFile
>>> self = LabelMeFile.demo()
>>> coco_dset = self.to_coco()
>>> print(f'coco_dset.dataset = {ub.urepr(coco_dset.dataset, nl=2)}')
>>> recon = LabelMeFile.from_coco(coco_dset)
>>> # FIXME: recon is not perfect
>>> print(f'self={self}')
>>> print(f'recon={recon}')
>>> print(self.dumps())
>>> print(recon.dumps())
add_polygon(label, points, group_id=None, flags=None, **kwargs)[source]

Add a polygon shape.

Parameters:
  • label (str) – Category namae / label for the shape.

  • points (list of list of float) – List of (x, y) points defining the polygon.

  • group_id (int, optional) – Group ID for the shape.

  • flags (dict, optional) – Additional flags for the shape.

add_rectangle(label, bbox, group_id=None, flags=None, **kwargs)[source]

Add a rectangle shape.

Parameters:
  • label (str) – Label for the shape.

  • bbox (list of float) – Bounding box [x, y, width, height].

  • group_id (int, optional) – Group ID for the shape.

  • flags (dict, optional) – Additional flags for the shape.

dump(file=None)[source]

Save the LabelMe JSON data to a file.

Parameters:

output_path (str) – Path where the JSON file will be saved.

dumps()[source]

Save the LabelMe JSON data to a file.

Returns:

str