Read an exported mask
Check the mask from Export a polygon mask to PNG. If you skipped that exercise, download dogs-exported.zip and extract it in your working folder.
Read the pixel values
With Python 3 and Pillow installed, save this as load_exported.py beside the dogs_exported folder:
import json
from pathlib import Path
from PIL import Image
folder = Path("dogs_exported")
labels = json.loads((folder / "label_names.json").read_text())
mask = Image.open(folder / "label.png")
counts = mask.histogram()
for label_id, label in enumerate(labels):
print(label_id, label, counts[label_id], "pixels")
assert mask.size == (2048, 1280)
assert labels == ["_background_", "dog"]
assert counts[0] > 0 and counts[1] > 0
mask.point(lambda value: 255 if value == 1 else 0).save(folder / "dog-preview.png")
Run it:
python load_exported.py
You should see pixel counts for background and dog, with no assertion error. These checks are for the supplied sample; change the expected size and labels when using your own annotations.
Look at the dog mask
Open dogs_exported/dog-preview.png. The middle dog's polygon should be white and the background black.
The preview makes the shape visible. Keep label.png for code that needs the original class IDs, and keep label_names.json so those numbers can be mapped back to labels.
