-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelme_polygon_to_mask
executable file
·39 lines (34 loc) · 1.13 KB
/
labelme_polygon_to_mask
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
37
38
39
#!/usr/bin/env python3
import os
import sys
import json
import cv2
import numpy as np
inputDir = sys.argv[1]
output_dir = "mask/"
#creating the ground_truth folder if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
json_files = os.listdir(inputDir)
#loading the json file
for image_json in json_files:
#neglect mask folder
# print(image_json)
if os.path.isdir(inputDir+image_json) == True:
continue
with open(inputDir+image_json) as file:
data = json.load(file)
#replace . with _ in the filename
filename = data["imagePath"].split("/")[-1]
if "." in filename:
splitted = filename.split(".")
splitted = splitted[:-1]
filename = ""
for i in range(len(splitted)):
filename += splitted[i]
# creating a new ground truth image
mask = np.zeros((data["imageHeight"], data["imageWidth"]), dtype='uint8')
for shape in data['shapes']:
mask = cv2.fillPoly(mask, [np.array(shape['points'], dtype=np.int32)], 255)
filename = image_json[:-5]
# saving the ground truth masks
cv2.imwrite(os.path.join(output_dir,filename) + ".png", mask)