LabelMeReader#

class LabelMeReader(filename: str)[source]#

Read annotations from a LabelMe JSON file.

Parameters:

filename (str) – path to LabelMe JSON file, or an http(s):// URL

LabelMe is a popular annotation tool that saves annotations in a JSON format. This class reads the JSON file and extracts the image, polygonal shapes, and associated flags. Other popular tools like CVAT and LabelStudio can export to LabelMe format, so this class can be used as a lightweight reader for those formats as well, albeit with some limitations (e.g. only polygonal shapes are supported).

Methods of this class return:

  • a list of Polygon2 instances for all shapes

  • file-level flags as a dictionary

  • the labeled image as a Image instance

For each returned polygon, additional attributes are attached:

  • group_id: int | None from the shape entry

  • flags: dict from the shape entry as a dictionary

Rectangle shapes are converted to 4-vertex polygons.

Example:

from machinevisiontoolbox import LabelMeReader
lme = LabelMeReader("https://github.com/wkentaro/labelme/raw/main/examples/tutorial/apc2016_obj3.json")
len(lme.shapes)  # number of annotated shapes
flags = lme.flags  # file-level flags
image = lme.image  # labeled image (if available)
for polygon in lme.shapes:
    print(polygon.points)  # polygon vertices
    print(polygon.group_id)  # group ID (if any)
    print(polygon.flags)  # shape-level flags

Note

The labelme package provides a more comprehensive interface to LabelMe annotations, including support for all shape types and attributes, but it is quite bloated with many package dependencies. This class is a lightweight alternative focused on polygonal shapes and basic flags.

Seealso:

pixels_mask Image.Polygons Image, Polygon2

Methods

Attributes

filename

flags

Return file-level flags from the LabelMe JSON as a dictionary.

image

Return the image from the LabelMe JSON, or None if not available.

shapes

Return list of shape polygons .