-
Notifications
You must be signed in to change notification settings - Fork 32
/
generateMutilRes.py
executable file
·113 lines (87 loc) · 3.92 KB
/
generateMutilRes.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
import gdal
import os
import config
from src import util
import logging
util.check_dir(config.path_logs)
util.set_logger(os.path.join(config.path_logs, 'generateMutiRes.log'))
def drange(start: float, stop: float, step: float):
r = start
while r < stop:
yield round(r, 1)
r += step
def getRes(path_tif: str) -> None:
"""
Reading Resolution of input iamge
Input:
path_tif: path of input to be processed
Output:
None
"""
ds = gdal.Open(path_tif)
resolution = round(ds.GetGeoTransform()[1], 1)
return resolution
logging.info('Iterating Training data in folder: {}'.format(config.path_image))
for root, dirs, files in os.walk(config.path_image):
for file in files:
if file.endswith(tuple(config.image_ext)):
logging.info('Processing : {}'.format(os.path.join(
root, file)))
currentResolution = getRes(os.path.join(root, file))
listResolution = list(drange(
max(currentResolution, config.minResolution),
config.maxResolution,
config.step))
for res in listResolution:
gdalOption = gdal.WarpOptions(
format='VRT', xRes=res, yRes=res)
# Creating output file name
path_image_output = os.path.join(
config.path_image_vrt,
util.getNamenoExt(file),
util.getNamenoExt(file) + '_' + str(res)+'.vrt')
path_label_output = os.path.join(
config.path_label_vrt,
util.getNamenoExt(file),
util.getNamenoExt(file) + '_' + str(res)+'.vrt')
# Creating folders
util.check_dir(os.path.dirname(path_image_output))
util.check_dir(os.path.dirname(path_label_output))
# Creating VRT of input image
gdal.Warp(path_image_output, os.path.abspath(os.path.join(
root, file)), options=gdalOption)
# Creating VRT of label image
gdal.Warp(path_label_output, os.path.abspath(os.path.join(
config.path_label, file)), options=gdalOption)
logging.info('Iterating Validation data in folder: {}'.format(config.path_vali_image))
for root, dirs, files in os.walk(config.path_vali_image):
for file in files:
if file.endswith(tuple(config.image_ext)):
logging.info('Processing : {}'.format(os.path.join(
root, file)))
currentResolution = getRes(os.path.join(root, file))
listResolution = list(drange(
max(currentResolution, config.minResolution),
config.maxResolution,
config.step))
for res in listResolution:
gdalOption = gdal.WarpOptions(
format='VRT', xRes=res, yRes=res)
# Creating output file name
path_vali_image_output = os.path.join(
config.path_vali_image_vrt,
util.getNamenoExt(file),
util.getNamenoExt(file) + '_' + str(res)+'.vrt')
path_vali_label_output = os.path.join(
config.path_vali_label_vrt,
util.getNamenoExt(file),
util.getNamenoExt(file) + '_' + str(res)+'.vrt')
# Creating folders
util.check_dir(os.path.dirname(path_vali_image_output))
util.check_dir(os.path.dirname(path_vali_label_output))
# Creating VRT of input image
gdal.Warp(path_vali_image_output, os.path.abspath(os.path.join(
root, file)), options=gdalOption)
# Creating VRT of label image
gdal.Warp(path_vali_label_output, os.path.abspath(os.path.join(
config.path_vali_label, file)), options=gdalOption)