-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.anton.py
287 lines (232 loc) · 8.73 KB
/
process.anton.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
from wbia.detecttools.directory import Directory
import utool as ut
import numpy as np
from os.path import abspath, join, split
import json
import wbia # NOQA
import tqdm
import cv2
ibs = None # NOQA
'''bash (host machine)
cd /datadrive
mkdir -p $(pwd)/wbia
cd $(pwd)/wbia
echo """
HOST_UID=$(id -u)
HOST_USER=$(whoami)
""" > $(pwd)/wbia.env
docker run \
-d \
-p 5000:5000 \
--name wbia.ggr \
-v $(pwd)/db:/data/db \
-v $(pwd)/cache:/cache \
-v /lynx-pie/wbia_pie_v2/reid-data/lynxcoco:/data/lynxcoco \
-v /lynx-pie/wbia_pie_v2/lynxcoco/annotations:/data/annotations \
--env-file $(pwd)/wbia.env \
--restart unless-stopped \
wildme/wbia:latest
sudo chown -R azureuser:azureuser .
docker exec -it wbia.ggr embed
'''
def background():
"""
# NOQA
cd ~/Downloads
rsync -aP \
anton:/lynx-pie/wbia_pie_v2/reid-data/lynxcoco/images/ \
$(pwd)/wbia/import/
rsync -aP \
anton:/lynx-pie/wbia_pie_v2/lynxcoco/annotations/instances_train_right.json \
$(pwd)/wbia/import/
rsync -aP \
anton:/lynx-pie/wbia_pie_v2/lynxcoco/annotations/instances_test_right.json \
$(pwd)/wbia/import/
rsync -aP \
$(pwd)/wbia/db/output/ \
anton:/lynx-pie/wbia_pie_v2/reid-data/lynxcoco/images/train_right_background/
"""
IMPORT_PATH = '/data/import/'
direct = Directory(IMPORT_PATH, recursive=True, images=True)
filepaths = list(direct.files())
gids = ibs.get_valid_gids()
processed = set(ibs.get_image_uris_original(gids))
filepaths_ = sorted(list(set(filepaths) - processed))
chunks = ut.ichunks(filepaths_, 16)
for filepath_chunk in tqdm.tqdm(chunks):
try:
gid = ibs.add_images(filepath_chunk)
print(filepath_chunk)
print(gid, ibs.get_image_uris_original(gid))
except Exception:
pass
gids = ibs.get_valid_gids()
uris_original = ibs.get_image_uris_original(gids)
filenames_original = [split(uri)[1] for uri in uris_original]
filenames_dict = dict(zip(filenames_original, gids))
filenames_dict_ = dict(zip(gids, filenames_original))
# Import existing JSON files
train_coco_json = '/data/import/instances_train_right.json'
test_coco_json = '/data/import/instances_test_right.json'
for coco_filepath in [train_coco_json, test_coco_json]:
with open(coco_filepath) as coco_file:
coco_dict = json.load(coco_file)
image_list = coco_dict.get('images', [])
id_dict = {}
image_dict = {}
for image in image_list:
# Get file name
image_filename = image['file_name']
image_filepath = abspath(join(
IMPORT_PATH, 'train_right', image_filename
))
image_id = image['id']
image_dict[image_filename] = {
'id': image_id,
'filepath': image_filepath,
}
id_dict[image_id] = image_filename
print(len(image_list))
add_gids = []
add_bbox = []
for annotation in coco_dict.get('annotations', []):
# annot_id = annotation['id']
annot_bbox = annotation['bbox']
image_id = annotation['image_id']
try:
assert image_id in id_dict
image_filename = id_dict[image_id]
assert image_filename in image_dict
assert image_filename in filenames_dict
except AssertionError:
continue
gid = filenames_dict[image_filename]
xtl, ytl, w, h = annot_bbox
xtl = int(np.around(xtl))
ytl = int(np.around(ytl))
w = int(np.around(w))
h = int(np.around(h))
add_gids.append(gid)
add_bbox.append((xtl, ytl, w, h))
print(len(add_gids))
ibs.add_annots(add_gids, add_bbox)
aids = ibs.get_valid_aids()
ibs.set_annot_species(aids, ['lynx'] * len(aids))
masks = ibs.get_annot_probchip_fpath(aids) # NOQA
images = ibs.get_images(gids) # NOQA
THRESH = 256 * 0.5
for gid in tqdm.tqdm(gids):
image = ibs.get_images(gid)
aids = ibs.get_image_aids(gid)
overlay = np.zeros(image.shape, dtype=np.float32)
for aid in aids:
mask = cv2.imread(ibs.get_annot_probchip_fpath(aid))
xtl, ytl, w, h = ibs.get_annot_bboxes(aid)
mask = cv2.resize(mask, (w, h))
overlay[ytl:ytl+h, xtl:xtl+w, :] = mask
cv2.imwrite('/data/db/raw/raw.%d.original.jpg' % (gid, ), overlay)
overlay[overlay < THRESH] = 0
overlay = cv2.dilate(overlay, (15, 15), iterations=5)
overlay = cv2.blur(overlay, (30, 30))
canvas = image.astype(np.float32)
canvas = canvas * (overlay / 255.0)
canvas = np.around(canvas)
canvas[canvas < 0] = 0
canvas[canvas > 255] = 255
canvas = canvas.astype(np.uint8)
cv2.imwrite('/data/db/raw/raw.%d.jpg' % (gid, ), image)
cv2.imwrite('/data/db/raw/raw.%d.overlay.jpg' % (gid, ), overlay)
cv2.imwrite('/data/db/raw/raw.%d.mask.jpg' % (gid, ), canvas)
cv2.imwrite('/data/db/output/%s' % (filenames_dict_[gid]), canvas)
cv2.imwrite('/data/db/output/%s.original.jpg' % (
filenames_dict_[gid].replace('.', '-')
),
image
)
def hotspotter():
IMPORT_PATH = '/data/lynxcoco/images/train_new/'
direct = Directory(IMPORT_PATH, recursive=True, images=True)
filepaths = list(direct.files())
gids = ibs.get_valid_gids()
processed = set(ibs.get_image_uris_original(gids))
filepaths_ = sorted(list(set(filepaths) - processed))
chunks = ut.ichunks(filepaths_, 1)
for filepath_chunk in tqdm.tqdm(chunks):
try:
gid = ibs.add_images(filepath_chunk)
print(filepath_chunk)
print(gid, ibs.get_image_uris_original(gid))
except Exception:
pass
gids = ibs.get_valid_gids()
uris_original = ibs.get_image_uris_original(gids)
filenames_original = [split(uri)[1] for uri in uris_original]
filenames_dict = dict(zip(filenames_original, gids))
filenames_dict_ = dict(zip(gids, filenames_original))
# Import existing JSON files
train_coco_json = '/data/annotations/instances_train_new.json'
test_coco_json = '/data/annotations/instances_val_new.json'
train_gids = []
test_gids = []
for coco_filepath in [train_coco_json, test_coco_json]:
with open(coco_filepath) as coco_file:
coco_dict = json.load(coco_file)
image_list = coco_dict.get('images', [])
id_dict = {}
image_dict = {}
for image in image_list:
# Get file name
image_filename = image['file_name']
image_filepath = abspath(join(
IMPORT_PATH, 'train_right', image_filename
))
image_id = image['id']
image_dict[image_filename] = {
'id': image_id,
'filepath': image_filepath,
}
id_dict[image_id] = image_filename
print(len(image_list))
add_gids = []
add_bbox = []
add_names = []
add_views = []
for annotation in coco_dict.get('annotations', []):
# annot_id = annotation['id']
annot_bbox = annotation['bbox']
name = annotation['attributes']['individual_id'].lower()
viewpoint = annotation['viewpoint'].lower()
assert viewpoint in ['left', 'right']
image_id = annotation['image_id']
try:
assert image_id in id_dict
image_filename = id_dict[image_id]
assert image_filename in image_dict
assert image_filename in filenames_dict
except AssertionError:
continue
gid = filenames_dict[image_filename]
xtl, ytl, w, h = annot_bbox
xtl = int(np.around(xtl))
ytl = int(np.around(ytl))
w = int(np.around(w))
h = int(np.around(h))
if '_train_' in coco_filepath:
train_gids.append(gid)
if '_val_' in coco_filepath:
test_gids.append(gid)
add_gids.append(gid)
add_bbox.append((xtl, ytl, w, h))
add_names.append(name)
add_views.append(viewpoint)
print(len(add_gids))
add_aids = ibs.add_annots(add_gids, add_bbox)
ibs.set_annot_names(add_aids, add_names)
ibs.set_annot_viewpoints(add_aids, add_views)
aids = ibs.get_valid_aids()
ibs.set_annot_species(aids, ['lynx'] * len(aids))
# Start Hotspotter
# Run code in feasability.py
if __name__ == '__main__':
background()
hotspotter()