· 6 years ago · Apr 15, 2020, 05:36 PM
1from IPython.display import Image, display
2from os.path import abspath, join, exists
3import matplotlib.pyplot as plt
4from io import BytesIO
5from PIL import Image
6import numpy as np
7import os
8import requests
9import time
10import json
11import torchvision.datasets as dset
12
13import warnings
14warnings.filterwarnings("ignore")
15
16
17LOCAL = True
18
19PROTOCOL = 'http'
20PREFIX = 'api'
21if LOCAL:
22 DOMAIN = 'localhost'
23 PORT = 5000
24else:
25 DOMAIN = 'demo.wildbook.org'
26 PORT = 80
27
28DATASET_DIR = './dataset/'
29
30def _url(route, prefix=True, postfix=True):
31 url = '%s:%s/%s/%s/'% (DOMAIN, PORT, PREFIX, route, ) if prefix else '%s:%s/%s/'% (DOMAIN, PORT, route, )
32 while '//' in url:
33 url = url.replace('//', '/')
34 if not postfix:
35 # Remove ending slash
36 url = url[:-1]
37 url = '%s://%s' % (PROTOCOL, url, )
38 return url
39
40def _pprint(value):
41 return json.dumps(value, sort_keys=True, indent=4)
42
43def _request(function, url, data_dict=None, file_dict=None, verbose=True):
44 # Make a copy of the data_dict so we don't do destructive changes
45 data = {}
46
47 if data_dict is not None:
48 data_dict_ = data_dict.copy()
49 for key in data_dict_:
50 data_dict_[key] = json.dumps(data_dict_[key])
51 data['data'] = data_dict_
52
53 if file_dict is not None:
54 data['files'] = file_dict
55
56 if verbose:
57 print('Requesting URL = %r' % (url, ))
58 try:
59 response = function(url, **data)
60 assert response.ok
61 response_dict = response.json()
62 print(response_dict)
63 response_dict = response_dict['response']
64 except AssertionError:
65 print('!!! FAILED REQUEST !!!')
66 print('\t URL = %r' % (url, ))
67 print('\t METHOD = %r' % (function, ))
68 print('\t CODE = %s' % (response.status_code, ))
69 print('\t RESPONSE = %s' % (response.text, ))
70 response_dict = None
71 if verbose:
72 print('...done')
73
74 return response_dict
75
76
77def get(*args, **kwargs):
78 return _request(requests.get, *args, **kwargs)
79
80
81def put(*args, **kwargs):
82 return _request(requests.put, *args, **kwargs)
83
84
85def post(*args, **kwargs):
86 return _request(requests.post, *args, **kwargs)
87
88
89def delete(*args, **kwargs):
90 return _request(requests.delete, *args, **kwargs)
91
92def canvas(image_list, detections_list=None):
93 if detections_list is None:
94 detections_list = [[]] * len(image_list)
95 assert len(image_list) == len(detections_list)
96
97 plt.figure(figsize=(15, 15), dpi=300)
98 plt.subplot(len(image_list), 1, 1)
99
100 for index in range(len(image_list)):
101 plt.subplot(len(image_list), 1, index + 1)
102 plt.axis('off')
103
104 image_ = image_list[index].copy()
105 h, w = image_.shape[:2]
106 detections = detections_list[index]
107
108 if len(detections) > 0:
109 classes = []
110 viewpoints = {}
111 for detection in detections:
112 x0 = detection['xtl']
113 y0 = detection['ytl']
114 x1 = x0 + detection['width']
115 y1 = y0 + detection['height']
116 species = detection['species']
117 viewpoint = detection['viewpoint']
118 if species not in viewpoints:
119 viewpoints[species] = []
120 viewpoints[species].append(viewpoint)
121 color = (255, 0, 0)
122 classes.append(species)
123 coord_list = []
124 for r in range(7):
125 coord_list += [(x0 + r, _) for _ in range(y0, y1)]
126 coord_list += [(x1 + r, _) for _ in range(y0, y1)]
127 coord_list += [(_, y0 + r) for _ in range(x0, x1)]
128 coord_list += [(_, y1 + r) for _ in range(x0, x1)]
129
130 for (x, y) in coord_list:
131 if 0 <= y and y < h and 0 <= x and x < w:
132 image_[y, x] = color
133 classes_str = ','.join(set(classes))
134 viewpoints_str = '%s' % (viewpoints, )
135 plt.title('Found: %s\n' % (viewpoints_str, ))
136 plt.imshow(image_)
137
138def job_status(jobid):
139 data_dict = {
140 'jobid': jobid,
141 }
142 status = get(_url('engine/job/status'), data_dict=data_dict)
143 if status is not None:
144 status = status.get('jobstatus', None)
145 return status
146
147def job_result(jobid):
148 data_dict = {
149 'jobid': jobid,
150 }
151 result = post(_url('engine/job/result'), data_dict=data_dict)
152 return result
153
154def job_monitor(jobid):
155 LIMIT = 100
156 counter = 0
157 status = None
158 print('Checking status...')
159 start = time.time()
160 while status not in ['unknown', 'completed', 'exception']:
161 if counter >= LIMIT:
162 print('...limited reached, exiting prematurely')
163 break
164 status = job_status(jobid)
165 print('\tstatus: %r' % (status, ))
166 counter += 1
167 time.sleep(5.0)
168 end = time.time()
169 duration = end - start
170 print('...finished! (took approx. %0.02f seconds)\n' % (duration, ))
171
172 result = job_result(jobid)
173 return result
174
175
176def upload(image_filepath):
177 print('Uploading %s...' % (image_filepath, ))
178 file_dict = {
179 'image': open(image_filepath, 'rb'),
180 }
181 gid = post(_url('upload/image'), file_dict=file_dict)
182 return gid
183
184def get_image_uuid(gid_list):
185 data_dict = {
186 'gid_list': gid_list,
187 }
188 image_uuid_list = get(_url('image/uuid'), data_dict=data_dict)
189 return image_uuid_list
190
191def upload_annotation(gid, bbox, theta=None, species=None, viewpoint=None, name=None, **kwargs):
192 # Upload a new Annotation without coming from a sourced Detection (i.e. from a different ground-truth source)
193 if gid is None:
194 return None
195 data_dict = {
196 'gid_list': [gid],
197 'bbox_list': [bbox],
198 'theta_list': None if theta is None else [theta],
199 'species_list': None if theta is None else [species],
200 'viewpoint_list': None if theta is None else [viewpoint],
201 'name_list': None if theta is None else [name],
202 }
203 aid_list = post(_url('annot'), data_dict=data_dict, **kwargs)
204 aid = aid_list[0]
205 return aid
206
207
208gid_list_database = []
209gid_list_query = []
210paths = []
211
212for dirpath, _, filenames in os.walk(DATASET_DIR):
213# if len(filenames) > 6:
214 for filename in filenames:
215 image_filepath = os.path.join(dirpath, filename)
216 paths.append(image_filepath)
217
218i = 0
219paths = sorted(paths)
220for image_filepath in paths:
221 gid = upload(image_filepath)
222 img = Image.open(image_filepath)
223 start = 'phs'
224 name = image_filepath[image_filepath.index(start):image_filepath.index(start) + len(start) + 3]
225 annotation_config = {
226 'bbox': [0, 0, img.size[0], img.size[1]], #bounding box, full image for now
227 'theta': 0.5,
228 'species': 'norppa',
229 'viewpoint': None,
230 'name': name #seal label
231 }
232 aid = upload_annotation(gid, **annotation_config)
233 if aid is None:
234 print('Could not get fully upload image! ', image_filepath)
235 continue
236 if 'query' in image_filepath:
237 gid_list_query.append(gid)
238 put(_url('image/imageset/rowid/'), data_dict={'gid_list':[gid], 'imgsetid_list': [9]})
239 else:
240 gid_list_database.append(gid)
241 put(_url('image/imageset/rowid/'), data_dict={'gid_list':[gid], 'imgsetid_list': [8]})
242 i+=1
243image_uuid_list = list(map(lambda gid: get_image_uuid(gid), gid_list_query))
244
245print('Image RowIDs (GIDs): %s' % (_pprint(gid_list_query), ))
246print('Image UUIDs: %s' % (_pprint(image_uuid_list), ))