LabelMeReader#

class LabelMeReader(filename: str)[source][source]#

Read annotations from a LabelMe JSON file.

Parameters:

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

Raises:

Changed in version 2.5.0: An unreadable file or invalid JSON now raises, where it used to return an incompletely initialised reader.

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.

Examples

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.