forked from kazuto1011/deeplab-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getImages.py
225 lines (191 loc) · 6.99 KB
/
getImages.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
#!/usr/bin/env python
# coding: utf-8
#
# Authors: Bernat Felip, Kazuto Nakashima
# URL: https://sirbernardphilip.github.io, https://kazuto1011.github.io
# Date: 14 May 2021
from __future__ import absolute_import, division, print_function
import click
import cv2
import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from omegaconf import OmegaConf
from tqdm import tqdm
from libs.models import *
from libs.utils import DenseCRF
banned = ["wall", "bridge", "bush", "ceiling", "carpet", "building", "floor", "gravel", "stone", "grass", "road", "roof", "snow", "wall"]
def get_device(cuda):
cuda = cuda and torch.cuda.is_available()
device = torch.device("cuda" if cuda else "cpu")
if cuda:
current_device = torch.cuda.current_device()
print("Device:", torch.cuda.get_device_name(current_device))
else:
print("Device: CPU")
return device
def get_classtable(CONFIG):
with open(CONFIG.DATASET.LABELS) as f:
classes = {}
for label in f:
label = label.rstrip().split("\t")
classes[int(label[0])] = label[1].split(",")[0]
return classes
def setup_postprocessor(CONFIG):
# CRF post-processor
postprocessor = DenseCRF(
iter_max=CONFIG.CRF.ITER_MAX,
pos_xy_std=CONFIG.CRF.POS_XY_STD,
pos_w=CONFIG.CRF.POS_W,
bi_xy_std=CONFIG.CRF.BI_XY_STD,
bi_rgb_std=CONFIG.CRF.BI_RGB_STD,
bi_w=CONFIG.CRF.BI_W,
)
return postprocessor
def preprocessing(image, device, CONFIG):
# Resize
scale = CONFIG.IMAGE.SIZE.TEST / max(image.shape[:2])
image = cv2.resize(image, dsize=None, fx=scale, fy=scale)
raw_image = image.astype(np.uint8)
# Subtract mean values
image = image.astype(np.float32)
image -= np.array(
[
float(CONFIG.IMAGE.MEAN.B),
float(CONFIG.IMAGE.MEAN.G),
float(CONFIG.IMAGE.MEAN.R),
]
)
# Convert to torch.Tensor and add "batch" axis
image = torch.from_numpy(image.transpose(2, 0, 1)).float().unsqueeze(0)
image = image.to(device)
return image, raw_image
def inference(model, image, raw_image=None, postprocessor=None):
_, _, H, W = image.shape
# Image -> Probability map
logits = model(image)
logits = F.interpolate(logits, size=(H, W), mode="bilinear", align_corners=False)
probs = F.softmax(logits, dim=1)[0]
probs = probs.cpu().numpy()
# Refine the prob map with CRF
if postprocessor and raw_image is not None:
probs = postprocessor(raw_image, probs)
labelmap = np.argmax(probs, axis=0)
return labelmap
@click.group()
@click.pass_context
def main(ctx):
"""
Demo with a trained model
"""
print("Mode:", ctx.invoked_subcommand)
@main.command()
@click.option(
"-c",
"--config-path",
type=click.File(),
required=True,
help="Dataset configuration file in YAML",
)
@click.option(
"-ip",
"--in-path",
type=click.Path(exists=True),
required=True,
help="Images to be processed",
)
@click.option(
"-mp",
"--map-path",
type=click.Path(exists=True),
required=True,
help="Images to be processed",
)
@click.option(
"-o",
"--out-path",
type=click.Path(exists=True),
required=True,
help="Output path of JSONs",
)
def multiple(config_path, in_path, map_path, out_path):
"""
Inference from multiple images
"""
# Setup
CONFIG = OmegaConf.load(config_path)
classes = get_classtable(CONFIG)
count = 0;
# Inference
tours = sorted(os.listdir(in_path))
for tour in tqdm(tours, leave=False):
images_path = os.path.join(in_path, tour)
images = sorted(os.listdir(images_path))
for image_path in tqdm(images, leave=False):
count = count + 1
path = os.path.join(images_path, image_path)
image = cv2.imread(path, cv2.IMREAD_COLOR)
image = cv2.resize(image, (1024,512))
#print(imageAux.shape)20484096
imageAux = cv2.resize(cv2.imread(path, cv2.IMREAD_COLOR), (513,256))
labelmap = np.load(os.path.join(map_path, tour, image_path[:-5]+'_map.npy'))
labels = np.unique(labelmap)
out_image_path = os.path.join(out_path, tour)
if(not os.path.exists(out_image_path)):
os.mkdir(out_image_path)
# Show result for each class
for i, label in enumerate(labels):
found = False
for ban in banned:
if ban in classes[label]:
found = True
if(found):
continue
mask = np.zeros([256,513,3], np.uint8)
mask[:,:,0] = ((labelmap == label)*255).astype(np.uint8)
mask[:,:,1] = ((labelmap == label)*255).astype(np.uint8)
mask[:,:,2] = ((labelmap == label)*255).astype(np.uint8)
#cv2.imshow("First", mask)
cv2.waitKey(0)
mask = cv2.resize(mask, (1024,512))
#cv2.imshow("Second", mask)
cv2.waitKey(0)
gray_mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
#cv2.imshow("Third", gray_mask)
cv2.waitKey(0)
ret, thresh = cv2.threshold(mask, 50, 255, cv2.THRESH_BINARY)
thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for c in contours:
x,y,w,h = cv2.boundingRect(c)
if w*h > 400:
cv2.putText(image, classes[label],(x+20,y+10),cv2.FONT_HERSHEY_SIMPLEX,0.4,(0,255,0),1,cv2.LINE_AA)
cv2.drawContours(image, [c], 0, (0, 255, 0), 1)
#cv2.drawContours(image, contours, 0, (0, 255, 0), 10)
#M = cv2.moments(contours[0])
#print("center X : '{}'".format(round(M['m10'] / M['m00'])))
#print("center Y : '{}'".format(round(M['m01'] / M['m00'])))
#cv2.circle(image, (round(M['m10'] / M['m00']), round(M['m01'] / M['m00'])), 5, (0, 255, 0), -1)
#cv2.imshow(classes[label],image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
print(os.path.join(out_image_path, image_path[:-5]+'.jpeg'))
cv2.imwrite(os.path.join(out_image_path, image_path[:-5]+'.jpeg'), image)
#for i, label in enumerate(labels):
#labelmap = cv2.resize(labelmap, (4096,2048))
#print(labelmap)
# mask = labelmap == label
# print(mask)
#mask = cv2.resize(mask, (409,2048))
#cv2.imshow('image',image)
#cv2.waitKey(0)
#plt.imshow(mask.astype(np.float32), alpha=0.5)
# plt.axis('off')
#plt.show()
if __name__ == "__main__":
main()