Skip to content

Commit 413e622

Browse files
committed
add script to convert annotation information in json file #24
1 parent a663bb8 commit 413e622

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

create_poly_json.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import cv2 as cv
22
import numpy as np
33
import json
4+
import os
5+
import argparse
6+
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument('--epsilon', type=float, default=2.5, help='epsilon pixel to approximate the polygons')
9+
parser.add_argument('--input', type=str, default="images_test/Abbey_Road_watershed_mask.png", help='image mask input to compute all polygons')
10+
parser.add_argument('--config', type=str, default="config.json", help='config file content labels informations')
11+
opt = parser.parse_args()
412

513
def to_categorical(y, num_classes=None):
614
y = np.array(y, dtype='int').ravel()
@@ -11,9 +19,9 @@ def to_categorical(y, num_classes=None):
1119
categorical[np.arange(n), y] = 1
1220
return categorical
1321

14-
img = cv.imread("images_test/Abbey_Road_watershed_mask.png")
22+
img = cv.imread(opt.input)
1523
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
16-
config = json.load(open("config.json"))['labels']
24+
config = json.load(open(opt.config))['labels']
1725

1826
shape = (gray.shape[0],gray.shape[1],1)
1927
gray = gray.reshape(shape)
@@ -26,7 +34,7 @@ def to_categorical(y, num_classes=None):
2634
data["shapes"] = []
2735
data["lineColor"]= [0,255,0,128]
2836
data["fillColor"]= [255,0,0,128]
29-
data["imagePath"]= "Abbey_Road.jpg"
37+
data["imagePath"]= os.path.basename(opt.input).replace("_watershed_mask.png",".jpg")
3038
data["imageData"]= None
3139
data["imageHeight"] = img.shape[0]
3240
data["imageWidth"] = img.shape[1]
@@ -39,11 +47,11 @@ def to_categorical(y, num_classes=None):
3947
im, contour , hierarchy = cv.findContours(person, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
4048

4149
for i,c in enumerate(contour) :
42-
contour[i] = cv.approxPolyDP(c,1.5,True)
50+
contour[i] = cv.approxPolyDP(c,opt.epsilon,True)
4351
d = {}
4452
d['label'] = label
4553
d["line_color"] = None
46-
d["fill_color"] = None
54+
d["fill_color"] = config[label]['color']
4755
if contour[i].shape[0] < 3:
4856
continue
4957
d["points"] = contour[i].reshape(contour[i].shape[0],contour[i].shape[2]).tolist()

0 commit comments

Comments
 (0)