from PIL import Image, ImageDraw
from pathlib import Path
import json
Labeling
Using LabelMe
LabelMe
LabelMe is simple-to-use GUI for labeling. It is straightforward to use (pip install labelme
, run in terminal as labelme
):
The segmentation points will be saved as json files:
{
"version": "5.1.1",
"flags": {},
"shapes": [
{
"label": "1",
"points": [
[
4.029411764705877,
1.0882352941176439
],
[
0.7941176470588189,
53.73529411764706
],
[
0.7941176470588189,
221.97058823529412
],
[
12.852941176470587,
1.3823529411764672
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
...
Let’s load some image and generated json file:
= Image.open("images/solar_panels.png")
im = im.size
width, height print(width, height)
im
256 256
Let’s convert the json into a mask:
with open('images/solar_panels.json') as f:
= json.load(f) data
There are:
= data['shapes']
points len(points)
20
20 data points, let’s make a mask out of those:
= Image.new('L', (width, height), 0)
mask for group in points:
= group['label']
label_class = group['points']
polygon = [(round(x), round(y)) for x,y in polygon]
polygon =255, fill=255)
ImageDraw.Draw(mask).polygon(polygon, outline mask
and save this as a file:
'images/solar_panels_mask.png') mask.save(
assert Path('images/solar_panels_mask.png').is_file()