-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolylinesToPolygonJSON.py
93 lines (85 loc) · 4.04 KB
/
polylinesToPolygonJSON.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import json
import numpy as np
import copy
ROOT_DIR = os.path.abspath("../../")
def getRegion(imageV,type = 'ring'):
goodRegions = []
orderX = []
#type = type.lower()
it = iter(imageV)
for region in it:
#print(region)
regAtt = region['region_attributes']
shAtt = region['shape_attributes']
if shAtt['name'] == 'polyline':
goodRegions.append(region) #I removed checking for ring here because we use pollyline only for rings and it checkes before
orderX.append(np.average(shAtt['all_points_x']))
if not goodRegions:
#print("No data")
return None
return {'goodRegions':goodRegions, 'orderX':(np.argsort(orderX))}
def linesToPolygons(input = os.path.join(ROOT_DIR,'Mask_RCNN/JSONtransform/testJSON.json'), output = os.path.join(ROOT_DIR,'Mask_RCNN/JSONtransform/testJSONresult.json')):
newJ = {}
shpAttrBase = {'name':'polygon'}
with open(input,'r') as json_file:
dct = json.load(json_file)
for image, imageAtt in dct.items():
#print("Which image:",' ',image)
newJ[image] = {}
for imageK, imageV in imageAtt.items():
#print("imageKeys:",' ',imageK)
if imageK == 'regions':
#print("unsortedRegions:",' ',imageV)
sortRegions = getRegion(imageV, type = 'ring')
if sortRegions == None:
#print("continue, no sortRegions")
continue;
#print("sortRegions:",' ',sortRegions)
#print('orderX: ',sortRegions['orderX'])
shpAtt = shpAttrBase.copy()
newJ[image]['regions'] = []
for i in range(len(sortRegions['orderX'])-1):
#print(i)
firstRegion = sortRegions['goodRegions'][sortRegions['orderX'][i]]['shape_attributes']
secondRegion = sortRegions['goodRegions'][sortRegions['orderX'][i+1]]['shape_attributes']
#print('firstRegion: ',firstRegion)
#print('secondRegion: ',secondRegion)
shpAtt['all_points_x'] = firstRegion['all_points_x'] + secondRegion['all_points_x'][::-1]
shpAtt['all_points_y'] = firstRegion['all_points_y'] + secondRegion['all_points_y'][::-1]
#print('newAtt: ',shpAtt)
regions = {'shape_attributes':shpAtt,'region_attributes':{'type':'RingBndy'}}
newJ[image]['regions'].append(copy.deepcopy(regions))
else:
#print(imageV)
try:
newJ[image][imageK] = imageV
except:
print("can't append:",' ',imageK,",",imageV)
with open(output,'w') as outfile:
json.dump(newJ, outfile,indent=4)
return newJ
#for conveniance run on all jsons in directory and merge them in one
##function takes input folder and take .json files, transforms them and export transformed files and complete combined file in the output folder
##this is still not working :(
def transformallJSON(input_folder, output_folder):
dic_all = {}
for file in os.listdir(input_folder):
if file.endswith(".json"):
try:
input = os.path.join(input_folder, file)
output = os.path.join(output_folder,'transformed' + file)
print(input)
print(output)
dic = polylinesToPolygonJSON.linesToPolygons(input = os.path.join(input_folder, file), output = os.path.join(output_folder,'transformed' + file))
#print(len(dic))
dic_all.update(dic)
#print(len(dic_all))
except:
print('This failed:',file)
pass
filenames =[]
for image, imageAtt in dic_all.items():
print(imageAtt['filename'])
filenames.append(imageAtt['filename'])
return(dic_all, filenames)