-
Notifications
You must be signed in to change notification settings - Fork 0
/
box21_api.py
282 lines (235 loc) · 8.46 KB
/
box21_api.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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../01_api.ipynb.
# %% auto 0
__all__ = ['Box21Api']
# %% ../01_api.ipynb 4
from fastcore.utils import *
# %% ../01_api.ipynb 5
import requests
class Box21Api:
"Box21 Api class"
def __init__(self, email:str, password:str, host:str, port:int, project_id:int):
self.email = email
self.password = password
self.host = host
self.port = port
self.project_id = project_id
self.token = self.get_token()
def post(self, url, payload, files=None):
self.token = self.get_token()
session = requests.Session()
session.headers.update({'x-access-token': self.token})
if files:
response = session.post(self.host + ':' + str(self.port) + url, data=payload, files=files)
else:
response = session.post(self.host + ':' + str(self.port) + url, data=payload)
return response
def get(self, url, payload=None):
self.token = self.get_token()
session = requests.Session()
session.headers.update({'x-access-token': self.token})
response = session.get(self.host + ':' + str(self.port) + url, data=payload)
return response
def get_token(self):
session = requests.Session()
# Get token
payload = {
"email": self.email, "password": self.password}
response = session.post(self.host + ':' + str(self.port) + '/api/login', data=payload)
token = response.json().get('token')
return token
# %% ../01_api.ipynb 8
from .asset import Asset
@patch
def get_assets(self:Box21Api, offset:int=0, limit:int=None) -> [Asset]:
payload = {
"offset": offset,
"limit": limit,
"project_id": self.project_id
}
url = '/api/v2/assets'
response = self.get(url, payload)
asset_jsons = response.json()
return [Asset.from_json(asset_json) for asset_json in asset_jsons]
@patch
def get_asset(self:Box21Api, asset_id: int) -> Asset:
url = '/api/asset'
response = self.post(url, {'asset_id': asset_id})
asset_json = response.json()
return Asset.from_json(asset_json)
# %% ../01_api.ipynb 12
from PIL import Image
import io
@patch
def download_asset(self:Box21Api, asset_id: int) -> Image:
self.token = self.get_token()
url = '/api/asset/download'
payload = {
"asset_id": asset_id,
"project_id": self.project_id
}
response = self.post(url, payload)
return Image.open(io.BytesIO(response.content))
# %% ../01_api.ipynb 16
from PIL import Image
import io, json
from .annotation import Annotation, BoundingBox, Keypoint
@patch
def get_annotations(self:Box21Api, asset_id: int) -> [Annotation]:
self.token = self.get_token()
url = '/api/asset/annotations'
payload = {
"asset_id": asset_id,
}
response = self.post(url, payload)
annotations = []
for annotation_json in response.json():
asset_id = annotation_json['asset_id']
annotation_id = annotation_json['id']
certainty = annotation_json['certainty']
label_id = annotation_json['label_id']
label_name = annotation_json['relationships']['label']['name']
project_id = annotation_json['project_id']
validated = annotation_json['validated']
coords = json.loads(annotation_json['coords'])
if annotation_json['type'] == 1:
x, y, w, h = coords
annotations.append(
BoundingBox(asset_id, annotation_id, certainty, label_id, label_name, project_id, validated, x, y, w, h))
else:
x, y = coords
annotations.append(
Keypoint(asset_id, annotation_id, certainty, label_id, label_name, project_id, validated, x, y))
return annotations
# %% ../01_api.ipynb 20
@patch
def update_asset_meta(self:Box21Api, asset_id: int, key: str, value: str) -> [Annotation]:
self.token = self.get_token()
url = '/api/asset/meta/update-value'
payload = {
"asset_id": asset_id,
"key": key,
"value": value
}
response = self.post(url, payload)
return Asset.from_json(response.json())
@patch
def delete_asset_meta_key(self:Box21Api, asset_id: int, key: str) -> [Annotation]:
self.token = self.get_token()
url = '/api/asset/meta/delete-key'
payload = {
"asset_id": asset_id,
"key": key
}
response = self.post(url, payload)
return Asset.from_json(response.json())
# %% ../01_api.ipynb 26
from .label import Label
@patch
def get_labels(self:Box21Api) -> [Label]:
self.token = self.get_token()
url = '/api/labels'
payload = {
"project_id": self.project_id
}
response = self.post(url, payload)
labels = []
for label_json in response.json():
labels.append(
Label(
id=label_json['id'],
name=label_json['name'],
parent_id=label_json['parent_id'],
project_id=label_json['project_id'],
type=label_json['type']
))
return labels
# %% ../01_api.ipynb 29
@patch
def get_label_annotations(self:Box21Api, label: Label) -> [Annotation]:
self.token = self.get_token()
url = '/api/label/annotations'
payload = {
"label_id": label.id
}
response = self.post(url, payload)
annotations = []
for annotation_json in response.json():
asset_id = annotation_json['asset_id']
annotation_id = annotation_json['id']
certainty = annotation_json['certainty']
label_id = annotation_json['label_id']
label_name = annotation_json['relationships']['label']['name']
project_id = annotation_json['project_id']
validated = annotation_json['validated']
coords = json.loads(annotation_json['coords'])
if annotation_json['type'] == 1:
x, y, w, h = coords
annotations.append(
BoundingBox(asset_id, annotation_id, certainty, label_id, label_name, project_id, validated, x, y, w, h))
else:
x, y = coords
annotations.append(
Keypoint(asset_id, annotation_id, certainty, label_id, label_name, project_id, validated, x, y))
return annotations
# %% ../01_api.ipynb 33
from pathlib import Path
from .annotation import Annotation
from .annotation import BoundingBox
from .annotation import Keypoint
@patch
def add_asset(self:Box21Api, file_path: Path, meta, annotations: [Annotation]= [], validated=False, in_validation_set=False) -> [Asset]:
if not isinstance(meta, dict):
return 'meta argument should be a python dictionary'
bounding_boxes = []
keypoints = []
for annotation in annotations:
if isinstance(annotation, BoundingBox):
if annotation.x > 1:
return 'Incorrect coordinates, should be between 0 and 1'
bounding_boxes.append({
'normalized_xywh': [annotation.x, annotation.y, annotation.width, annotation.height],
'label': annotation.label_name,
'confidence': annotation.certainty
})
elif isinstance(annotation, Keypoint):
if annotation.x > 1:
return 'Incorrect coordinates, should be between 0 and 1'
keypoints.append({
'normalized_xywh': [annotation.x, annotation.y],
'label': annotation.label_name,
'confidence': annotation.certainty
})
self.token = self.get_token()
url = '/api/assets/add'
payload = {
'meta': json.dumps(meta),
'validated': validated,
'in_validation_set': in_validation_set,
'filename': file_path.name,
'bounding_boxes': json.dumps(bounding_boxes),
'keypoints': json.dumps(keypoints),
'project_id': self.project_id
}
files = {'file': open(file_path, 'rb')}
response = self.post(url, payload, files=files)
#print(response.text)
return Asset.from_json(response.json())
@patch
def delete_assets(self:Box21Api, asset_ids: [int]):
url = '/api/assets/delete'
payload = {
'asset_ids': json.dumps(asset_ids)
}
response = self.post(url, payload)
# %% ../01_api.ipynb 41
from .job import Job
@patch
def update_job(self:Box21Api, job_id, processing=None, processed=None, progress=None):
url = '/api/update-job'
payload = {
'job_id': job_id,
'processing': processing,
'processed': processed,
'progress': progress
}
response = self.post(url, payload)