-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_labelme2image.py
36 lines (25 loc) · 1.02 KB
/
json_labelme2image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from labelme.utils import shape_to_mask
import json
import os.path as osp
import cv2
import glob
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from tqdm import tqdm
def labelme2images(input_dir, output_dir, extention):
print("Generating dataset")
filenames = glob.glob(osp.join(input_dir, "*.json"))
for filename in tqdm(filenames):
with open(filename, "r", encoding="utf-8") as f:
dj = json.load(f)
shapes = dj['shapes']
h, w = dj['imageHeight'],dj['imageWidth']
mask = np.zeros((h, w), dtype=np.uint8)
for shape in shapes:
mask += shape_to_mask((h, w), shape['points'], shape_type=None,line_width=1, point_size=1)
output_filename = osp.join(output_dir, osp.basename(filename).replace(".json", extention))
im = Image.fromarray(mask > 0)
im.save(output_filename)
if __name__ =="__main__":
labelme2images("data\\masks_json", "data\\masks", extention=".tif")