-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathczhUtils.py
352 lines (278 loc) · 10.3 KB
/
czhUtils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from osgeo import gdal, gdalnumeric, ogr, gdal_array
import sys
sys.path.append(r'C:\Program Files\GDAL')
# import ogr
# import gdal_array
from skimage import measure
import numpy as np
import json
import os
import datetime
from shapely.geometry import Polygon
# get all the fillpath in the directory include sub-directory
def getfilepath(curDir, filelist, ext=('.TIF', '.tif', '.PNG', '.png', '.JPG', '.jpg')):
if os.path.isfile(curDir):
if curDir.lower().endswith(ext):
filelist.append(curDir)
else:
dir_or_files = os.listdir(curDir)
for dir_file in dir_or_files:
dir_file_path = os.path.join(curDir, dir_file)
# check is file or directory
if os.path.isdir(dir_file_path):
getfilepath(dir_file_path, filelist, ext)
else:
# extension_ = dir_file_path.split('.')[-1]
# if (extension_.lower() in ext):
if dir_file_path.endswith(ext):
filelist.append(dir_file_path)
#
def raster2array(rasters,band_no=1):
"""
Arguments:
rast A gdal Raster object
band_no band numerical order
Example :
raster = gdal.Open(rasterfn)
raster2array(raster,1)
"""
bands = rasters.RasterCount
if band_no>0 and band_no <=bands:
band = rasters.GetRasterBand(band_no)
array = band.ReadAsArray()
else:
array = rasters.ReadAsArray()
return array
# This function will convert the rasterized clipper shapefile
# to a mask for use within GDAL.
def imageToArray(i):
"""
Converts a Python Imaging Library array to a
gdalnumeric image.
"""
a=gdalnumeric.fromstring(i.tobytes(),'b')
a.shape=i.im.size[1], i.im.size[0]
return a
#
def coord2pixelOffset(geotransform, x, y):
"""
Arguments:
geotransform A gdal transform object
x world coordinate x
y world coordinate y
return pixel position in image
Example :
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
coord2pixel(geotransform,xCoord,yCoord)
"""
#left top
originX = geotransform[0]
originY = geotransform[3]
#pixel resolution
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
#ax rotate (here not used)
rotateX = geotransform[2]
rotateY = geotransform[4]
xOffset = int((x - originX) / pixelWidth)
yOffset = int((y - originY) / pixelHeight)
return xOffset, yOffset
#
def getUniqueValue(inL, column):
# intilize a null list
unique_list = []
if (len(inL) == 0): return unique_list
count = len(inL[0])
if column > count: return unique_list
# traverse for all elements
for x in inL:
# check if exists in unique_list or not
if x[column - 1] not in unique_list:
unique_list.append(x[column - 1])
return unique_list
def pixeloffset2coord(geoTransform,pixel_xOffset,pixel_yOffset):
"""
geoTransform: a gdal geoTransform object
pixel_xOffset:
pixel_yOffset:
return: coords
"""
#left top
originX = geoTransform[0]
originY = geoTransform[3]
#pixel resolution
pixelWidth = geoTransform[1]
pixelHeight = geoTransform[5]
# calculate coordinates
coordX = originX+ pixelWidth*pixel_xOffset
coordY = originY+pixelHeight*pixel_yOffset
return coordX,coordY
INFO = {
"description": "sidewalk test Dataset",
"url": "",
"version": "0.1.0",
"year": 2019,
"contributor": "czh_njit",
"date_created": datetime.datetime.utcnow().isoformat(' ')
}
LICENSES = [
{
"id": 1,
"name": "Attribution-NonCommercial-ShareAlike License",
"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
}
]
CATEGORIES = [
{
'id': 1,
'name': 'sidewalk',
'supercategory': '',
},
# {
# 'id': 2,
# 'name': 'background',
# 'supercategory': '',
# },
]
class czhTiff2Json():
"""
create annotation file from tiff file,used for satellite image or aerial image object detection/instance segmentation
"""
def __init__(self,tifFns,lbltiffPath,jsonFn,shpFn="",mode=0):
"""
geotiff?
Arguments:
tifFn: tif file name or path (in)
jsonFn:json file name (out)
shpFn: shapefile name (in)
mode: two ways to get json file,one get from tiff file ,another from shape file
"""
self.tifFns = tifFns
self.lbltiffPath = lbltiffPath
self.jsonFn = jsonFn
self.mode = mode
if mode ==1 :
self.shpFn = shpFn
def createJson(self):
if self.mode ==1:
self.createJsonFromShape()
else:
self.createJsonFromTiffs()
def createJsonFromShape(self):
pass
def createJsonFromTiffs(self):
#check self.tifFns if it's file open or traverse directory
#no directory nest?
lstTiff=[]
if os.path.isdir(self.tifFns):
getfilepath(self.tifFns, lstTiff)
elif os.path.isfile(self.tifFns):
# self.createJsonFromTiffFile(self.tifFns)
lstTiff.append(self.tifFns)
else:
print("input path or directory is error!")
if len(lstTiff)>0:
print(lstTiff)
self.createJsonFromTiffFiles(lstTiff)
def createJsonFromTiffFiles(self,tiffFns):
#check json if exist open else create
# open json
if os.path.exists(self.jsonFn):
self.coco_output = json.load(self.jsonFn)
else:
self.coco_output = {
"info": INFO,
"licenses": LICENSES,
"categories": CATEGORIES,
"images": [],
"annotations": []
}
annotation_idx = 1
for img_idx, tiffn in enumerate(tiffFns):
self.createJsonFromTiffFile(tiffn, img_idx+1, 1, annotation_idx + 10000 * img_idx)
with open(self.jsonFn, 'w') as output_json_file:
json.dump(self.coco_output, output_json_file)
def createJsonFromTiffFile(self, tiff_filepath, img_idx, band_no=1, annotation_idx=1):
print("Processing: ", tiff_filepath)
rasters = gdal.Open(tiff_filepath)
raster_array = raster2array(rasters,band_no)
#get size of image
img_Width = rasters.RasterXSize
img_Height = rasters.RasterYSize
img_size = [img_Width,img_Height]
#create image_info
tiff_filepath = os.path.join(self.lbltiffPath, os.path.basename(tiff_filepath))
image_info = self.create_image_info(img_idx,tiff_filepath,img_size)
self.coco_output["images"].append(image_info)
# create annotation
polygons = self.binaryMask2Polygon(raster_array)
for idx,polygon in enumerate(polygons):
# print(type(polygon), polygon.size)
if polygon.size > 7:
category_info ={'id':1,"is_crowd":0}
annotatin_info =self.create_annotation_info(idx+annotation_idx,img_idx,category_info,polygon,img_size)
self.coco_output["annotations"].append(annotatin_info)
def binaryMask2Polygon(self,binaryMask):
polygons =[]
padded_binary_mask = np.pad(binaryMask, pad_width=1, mode='constant', constant_values=0)
contours = measure.find_contours(padded_binary_mask,0.5)
contours = np.subtract(contours, 1)
def closeContour(contour):
if not np.array_equal(contour[0], contour[-1]):
contour = np.vstack((contour, contour[0]))
return contour
for contour in contours:
contour = closeContour(contour)
contour = measure.approximate_polygon(contour, 1)
if len(contour)<3:
continue
contour = np.flip(contour,axis =1)
# segmentation = contour.ravel().tolist()
#
# # after padding and subtracting 1 we may get -0.5 points in our segmentation
# segmentation = [0 if i < 0 else i for i in segmentation]
# polygons.append(segmentation)
polygons.append(contour)
return polygons
def create_image_info(self,image_id, file_name, image_size,
date_captured=datetime.datetime.utcnow().isoformat(' '),
license_id=1, coco_url="", flickr_url=""):
image_info = {
"id": image_id,
"file_name": file_name,
"width": image_size[0],
"height": image_size[1],
"date_captured": date_captured,
"license": license_id,
"coco_url": coco_url,
"flickr_url": flickr_url
}
return image_info
def create_annotation_info(self,annotation_id, image_id, category_info, segmentation,
image_size=None, tolerance=2, bounding_box=None):
try:
polygon = Polygon(np.squeeze(segmentation))
# print(type(polygon))
area =polygon.area
segmentation = segmentation.ravel().tolist()
# # after padding and subtracting 1 we may get -0.5 points in our segmentation
bbx =[0 if i < 0 else int(i) for i in list(polygon.bounds)]
segmentation = [0 if i < 0 else int(i) for i in segmentation]
annotation_info = {
"id": annotation_id,
"image_id": image_id,
"category_id": category_info["id"],
"iscrowd": category_info["is_crowd"],
"area": area,
"bbox": bbx,
"segmentation": [segmentation],
"width": image_size[0],
"height": image_size[1],
}
except Exception as e:
print("Error in create_annotation_info():", e)
return annotation_info
# test = czhTiff2Json("D:\\2019\\njit learning\\201909\\sidewalk extract\\czhSidewalkExtract\\val\\label","D:\\2019\\njit learning\\201909\\sidewalk extract\\czhSidewalkExtract\\val\\images\\","D:\\2019\\njit learning\\201909\\sidewalk extract\\czhSidewalkExtract\\val\\label\\sidewalk_val.json")
test = czhTiff2Json(r"L:\Datasets\AIRS\val\labels", r"L:\Datasets\AIRS\val\images", r"L:\Datasets\AIRS\val\AIRS_val.json")
# test.createJson()