-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
167 lines (128 loc) · 4.5 KB
/
classifier.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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
from django.contrib.gis.gdal import GDALRaster
import subprocess
from PIL import Image
import os
from joblib import dump, load
import sys
from progress.bar import Bar
from postprocessor import get_adjacent_indices
Image.MAX_IMAGE_PIXELS = None
def path_maker(path):
if not os.path.exists(path):
os.makedirs(path)
def l1c_classifier(inputDir, outputDir, modelDir):
file_name = inputDir + '.png'
for root, dirs, files in os.walk(inputDir, topdown = False):
for name in dirs:
if name == 'IMG_DATA':
inpath = os.path.abspath(os.path.join(root, name)) + '/'
model_path = os.path.join(modelDir,'Model.joblib')
path_maker(outputDir)
# reading 13 bands
inpath = os.path.join(inpath,'*_B*.jp2')
# storing vrt file
outPath = os.path.join(outputDir,'resampled_stack.vrt')
# getting band value
command = "gdalbuildvrt -resolution user -tr 20 20 -separate -overwrite {0} {1}".format(outPath,inpath)
subprocess.run(command, shell=True)
rast = GDALRaster(outPath)
rastersBands = []
index_arr = []
for band in rast.bands:
b = band.data()
rastersBands.append(b)
# indexs = cal_index(rastersBands[0],rastersBands[1],rastersBands[2],rastersBands[3],rastersBands[4],rastersBands[5],rastersBands[6],rastersBands[7],
# rastersBands[8],rastersBands[9],rastersBands[10],rastersBands[11],rastersBands[12])
# for index in indexs:
# rastersBands.append(index)
print('reading 13 bands')
#print('15 indexs done')
rasterStack = np.dstack(rastersBands)
#rasterStack = np.dstack(index_arr)
X_test = np.expand_dims(rasterStack,axis=0)
#scaler = load('scaler.gz')
loaded_model = load(model_path)
rgbArray = np.zeros((X_test.shape[1],X_test.shape[2],3), 'uint8')
post_processingArray = np.zeros((X_test.shape[1],X_test.shape[2]),dtype='object')
bar = Bar('Classifying Image', max = X_test.shape[1])
def color(row,col,r,g,b):
rgbArray[row,col, 0] = r * 255
rgbArray[row,col, 1] = g * 255
rgbArray[row,col, 2] = b * 255
ci = cl = ot = sh = sn = wa = 0
for row in range(X_test.shape[1]):
#in_arr = scaler.transform(X_test[0][row])
in_arr = X_test[0][row]
y_pred = loaded_model.predict(in_arr)
for class_v,col in zip (y_pred,range(in_arr.shape[0])):
post_processingArray[row][col] = class_v
if class_v == 'cirrus':
ci = ci + 1
color(row,col,0.733, 0.773, 0.925) # red
elif class_v == 'cloud':
cl = cl + 1
color(row,col,0.949, 0.949, 0.949) # white
elif class_v == 'other':
ot = ot + 1
color(row,col,0, 1, 0) # yellow
elif class_v == 'shadow':
sh = sh + 1
color(row,col,0.467, 0.298, 0.043) # black
elif class_v == 'snow':
sn = sn + 1
color(row,col,0.325, 1, 0.980) # green
elif class_v == 'water':
wa = wa + 1
color(row,col,0, 0, 1) # blue
bar.next()
total = ci + cl + ot + sh + sn + wa
print("\nci-{0}, cl-{1}, ot-{2}, sh-{3}, sn-{4}, wa-{5}, total-{6}\n".format(ci,cl,ot,sh,sn,wa,total))
img = Image.fromarray(rgbArray,'RGB')
# storing RGB file
file_name = 'classified_' + file_name.split('/')[-1]
RGB_file_path = os.path.join(outputDir,file_name)
img.save(RGB_file_path)
print('image saved at',RGB_file_path)
command = "rm {0}".format(outPath)
subprocess.run(command, shell=True)
#np.save('classifying_image.npy',post_processingArray)
bar.finish()
bar = Bar('Post Processing', max = X_test.shape[1])
rgbArray = np.zeros((X_test.shape[1],X_test.shape[2],3), 'uint8')
m = post_processingArray.shape[0]
n = post_processingArray.shape[1]
ci = cl = ot = sh = sn = wa = 0
for row in range(m):
for col in range(n):
class_v = get_adjacent_indices(row,col,m,n,post_processingArray)
if class_v == 'cirrus':
ci = ci + 1
color(row,col,0.733, 0.773, 0.925) # red
elif class_v == 'cloud':
cl = cl + 1
color(row,col,0.949, 0.949, 0.949) # white
elif class_v == 'other':
ot = ot + 1
color(row,col,0, 1, 0) # yellow
elif class_v == 'shadow':
sh = sh + 1
color(row,col,0.467, 0.298, 0.043) # black
elif class_v == 'snow':
sn = sn + 1
color(row,col,0.325, 1, 0.980) # green
elif class_v == 'water':
wa = wa + 1
color(row,col,0, 0, 1) # blue
bar.next()
total = ci + cl + ot + sh + sn + wa
print("\nci-{0}, cl-{1}, ot-{2}, sh-{3}, sn-{4}, wa-{5}, total-{6}\n".format(ci,cl,ot,sh,sn,wa,total))
img = Image.fromarray(rgbArray,'RGB')
# storing RGB file
file_name = 'post_processed_' + file_name.split('/')[-1]
RGB_file_path = os.path.join(outputDir,file_name)
img.save(RGB_file_path)
bar.finish()
print('image saved at',RGB_file_path)