forked from abreheret/PixelAnnotationTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_poly_json.py
67 lines (56 loc) · 2.17 KB
/
create_poly_json.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2 as cv
import numpy as np
import json
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--epsilon', type=float, default=2.5, help='epsilon pixel to approximate the polygons')
parser.add_argument('--input', type=str, default="images_test/Abbey_Road_watershed_mask.png", help='image mask input to compute all polygons')
parser.add_argument('--config', type=str, default="config.json", help='config file content labels informations')
opt = parser.parse_args()
def to_categorical(y, num_classes=None):
y = np.array(y, dtype='int').ravel()
if not num_classes:
num_classes = np.max(y) + 1
n = y.shape[0]
categorical = np.zeros((n, num_classes))
categorical[np.arange(n), y] = 1
return categorical
img = cv.imread(opt.input)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
config = json.load(open(opt.config))['labels']
shape = (gray.shape[0],gray.shape[1],1)
gray = gray.reshape(shape)
images = to_categorical(gray).reshape((gray.shape[0],gray.shape[1],-1))
out = open('images_test/Abbey_Road.json','w')
data = {}
data["version"] = "3.6.16"
data["flags"] = {}
data["shapes"] = []
data["lineColor"]= [0,255,0,128]
data["fillColor"]= [255,0,0,128]
data["imagePath"]= os.path.basename(opt.input).replace("_watershed_mask.png",".jpg")
data["imageData"]= None
data["imageHeight"] = img.shape[0]
data["imageWidth"] = img.shape[1]
for label in config :
person = images[:,:,config[label]['id']]
person[person>0] = 255
person = person.astype('uint8')
im, contour , hierarchy = cv.findContours(person, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for i,c in enumerate(contour) :
contour[i] = cv.approxPolyDP(c,opt.epsilon,True)
d = {}
d['label'] = label
d["line_color"] = None
d["fill_color"] = config[label]['color']
if contour[i].shape[0] < 3:
continue
d["points"] = contour[i].reshape(contour[i].shape[0],contour[i].shape[2]).tolist()
d["shape_type"] = "polygon"
data["shapes"].append(d)
# color = config[label]['color']
# cv.drawContours(img, contour, -1, color, 1)
# cv.imshow("person",img)
# cv.waitKey(0)
json.dump(data,out,indent=2)