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
Polygon2instances for all shapesfile-level
flagsas a dictionarythe labeled image as a
Imageinstance
For each returned polygon, additional attributes are attached:
group_id: int | Nonefrom the shape entryflags: dictfrom 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
labelmepackage 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_maskImage.PolygonsImage,Polygon2
Methods
Attributes