Skip to content

Commit 7510afa

Browse files
committed
adds scene
Things got a bit messy as scene does not return 'predictions'
1 parent f490457 commit 7510afa

File tree

4 files changed

+266
-44
lines changed

4 files changed

+266
-44
lines changed

deepstack/core.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
URL_FACE_DETECTION = "http://{}:{}/v1/vision/face"
1515
URL_FACE_REGISTRATION = "http://{}:{}/v1/vision/face/register"
1616
URL_FACE_RECOGNITION = "http://{}:{}/v1/vision/face/recognize"
17+
URL_SCENE_DETECTION = "http://{}:{}/v1/vision/scene"
1718

1819

1920
def format_confidence(confidence: Union[str, float]) -> float:
@@ -113,26 +114,27 @@ def __init__(
113114
self._url_detection = url_detection
114115
self._api_key = api_key
115116
self._timeout = timeout
116-
self._predictions = []
117+
self._response = None
117118

118119
def detect(self, image_bytes: bytes):
119120
"""Process image_bytes, performing detection."""
120-
self._predictions = []
121+
self._response = None
121122
url = self._url_detection.format(self._ip_address, self._port)
122123

123124
response = post_image(url, image_bytes, self._api_key, self._timeout)
124125

125-
if response.status_code == HTTP_OK:
126-
if response.json()["success"]:
127-
self._predictions = response.json()["predictions"]
128-
else:
129-
error = response.json()["error"]
130-
raise DeepstackException(f"Error from Deepstack: {error}")
126+
if not response.status_code == HTTP_OK:
127+
raise DeepstackException(f"Error from request: {response.status_code}")
128+
return
129+
130+
self._response = response.json()
131+
if not self._response["success"]:
132+
raise DeepstackException(f"Error from Deepstack: {error}")
131133

132134
@property
133135
def predictions(self):
134-
"""Return the classifier attributes."""
135-
return self._predictions
136+
"""Return the predictions."""
137+
raise NotImplementedError
136138

137139

138140
class DeepstackObject(Deepstack):
@@ -149,6 +151,31 @@ def __init__(
149151
ip_address, port, api_key, timeout, url_detection=URL_OBJECT_DETECTION
150152
)
151153

154+
@property
155+
def predictions(self):
156+
"""Return the predictions."""
157+
return self._response["predictions"]
158+
159+
160+
class DeepstackScene(Deepstack):
161+
"""Work with scenes"""
162+
163+
def __init__(
164+
self,
165+
ip_address: str,
166+
port: str,
167+
api_key: str = "",
168+
timeout: int = DEFAULT_TIMEOUT,
169+
):
170+
super().__init__(
171+
ip_address, port, api_key, timeout, url_detection=URL_SCENE_DETECTION
172+
)
173+
174+
@property
175+
def predictions(self):
176+
"""Return the predictions."""
177+
return self._response
178+
152179

153180
class DeepstackFace(Deepstack):
154181
"""Work with objects"""
@@ -164,6 +191,11 @@ def __init__(
164191
ip_address, port, api_key, timeout, url_detection=URL_FACE_DETECTION
165192
)
166193

194+
@property
195+
def predictions(self):
196+
"""Return the classifier attributes."""
197+
return self._response["predictions"]
198+
167199
def register_face(self, name: str, image_bytes: bytes):
168200
"""
169201
Register a face name to a file.
@@ -185,14 +217,14 @@ def register_face(self, name: str, image_bytes: bytes):
185217

186218
def recognise(self, image_bytes: bytes):
187219
"""Process image_bytes, performing recognition."""
188-
self._predictions = []
189220
url = URL_FACE_RECOGNITION.format(self._ip_address, self._port)
190221

191222
response = post_image(url, image_bytes, self._api_key, self._timeout)
192223

193-
if response.status_code == HTTP_OK:
194-
if response.json()["success"]:
195-
self._predictions = response.json()["predictions"]
196-
else:
197-
error = response.json()["error"]
198-
raise DeepstackException(f"Error from Deepstack: {error}")
224+
if not response.status_code == HTTP_OK:
225+
raise DeepstackException(f"Error from request: {response.status_code}")
226+
return
227+
228+
self._response = response.json()
229+
if not self._response["success"]:
230+
raise DeepstackException(f"Error from Deepstack: {error}")

usage-face-recognition.ipynb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
"name": "stdout",
9090
"output_type": "stream",
9191
"text": [
92-
"CPU times: user 11.6 ms, sys: 14.2 ms, total: 25.8 ms\n",
93-
"Wall time: 2.72 s\n"
92+
"CPU times: user 7.9 ms, sys: 10.4 ms, total: 18.3 ms\n",
93+
"Wall time: 5.08 s\n"
9494
]
9595
}
9696
],
@@ -176,8 +176,8 @@
176176
"name": "stdout",
177177
"output_type": "stream",
178178
"text": [
179-
"CPU times: user 5.03 ms, sys: 4.16 ms, total: 9.19 ms\n",
180-
"Wall time: 3.52 s\n"
179+
"CPU times: user 6.26 ms, sys: 5.42 ms, total: 11.7 ms\n",
180+
"Wall time: 6.24 s\n"
181181
]
182182
}
183183
],
@@ -200,16 +200,16 @@
200200
},
201201
{
202202
"cell_type": "code",
203-
"execution_count": 10,
203+
"execution_count": 9,
204204
"metadata": {},
205205
"outputs": [
206206
{
207207
"name": "stdout",
208208
"output_type": "stream",
209209
"text": [
210210
"[{'confidence': 0.7536658, 'userid': 'Idris Elba', 'y_min': 154, 'x_min': 1615, 'y_max': 682, 'x_max': 1983}, {'confidence': 0, 'userid': 'unknown', 'y_min': 237, 'x_min': 869, 'y_max': 732, 'x_max': 1214}]\n",
211-
"CPU times: user 7.67 ms, sys: 17.7 ms, total: 25.3 ms\n",
212-
"Wall time: 6.01 s\n"
211+
"CPU times: user 7.26 ms, sys: 15.9 ms, total: 23.1 ms\n",
212+
"Wall time: 6.79 s\n"
213213
]
214214
}
215215
],
@@ -235,7 +235,7 @@
235235
},
236236
{
237237
"cell_type": "code",
238-
"execution_count": 11,
238+
"execution_count": 10,
239239
"metadata": {},
240240
"outputs": [
241241
{
@@ -244,7 +244,7 @@
244244
"{'Idris Elba': 75.4}"
245245
]
246246
},
247-
"execution_count": 11,
247+
"execution_count": 10,
248248
"metadata": {},
249249
"output_type": "execute_result"
250250
}

usage-object-detection.ipynb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 10,
25+
"execution_count": 2,
2626
"metadata": {},
2727
"outputs": [],
2828
"source": [
@@ -41,7 +41,7 @@
4141
},
4242
{
4343
"cell_type": "code",
44-
"execution_count": 11,
44+
"execution_count": 3,
4545
"metadata": {},
4646
"outputs": [],
4747
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "code",
53-
"execution_count": 12,
53+
"execution_count": 4,
5454
"metadata": {},
5555
"outputs": [
5656
{
@@ -81,15 +81,15 @@
8181
},
8282
{
8383
"cell_type": "code",
84-
"execution_count": 16,
84+
"execution_count": 5,
8585
"metadata": {},
8686
"outputs": [
8787
{
8888
"name": "stdout",
8989
"output_type": "stream",
9090
"text": [
91-
"CPU times: user 6.44 ms, sys: 13 ms, total: 19.4 ms\n",
92-
"Wall time: 5.42 s\n"
91+
"CPU times: user 9.32 ms, sys: 10.8 ms, total: 20.1 ms\n",
92+
"Wall time: 9.23 s\n"
9393
]
9494
}
9595
],
@@ -111,7 +111,7 @@
111111
},
112112
{
113113
"cell_type": "code",
114-
"execution_count": 17,
114+
"execution_count": 6,
115115
"metadata": {},
116116
"outputs": [
117117
{
@@ -137,7 +137,7 @@
137137
" 'x_max': 797}]"
138138
]
139139
},
140-
"execution_count": 17,
140+
"execution_count": 6,
141141
"metadata": {},
142142
"output_type": "execute_result"
143143
}
@@ -148,7 +148,7 @@
148148
},
149149
{
150150
"cell_type": "code",
151-
"execution_count": 18,
151+
"execution_count": 7,
152152
"metadata": {},
153153
"outputs": [
154154
{
@@ -157,7 +157,7 @@
157157
"3"
158158
]
159159
},
160-
"execution_count": 18,
160+
"execution_count": 7,
161161
"metadata": {},
162162
"output_type": "execute_result"
163163
}
@@ -183,7 +183,7 @@
183183
},
184184
{
185185
"cell_type": "code",
186-
"execution_count": 19,
186+
"execution_count": 8,
187187
"metadata": {},
188188
"outputs": [
189189
{
@@ -192,7 +192,7 @@
192192
"['dog', 'person']"
193193
]
194194
},
195-
"execution_count": 19,
195+
"execution_count": 8,
196196
"metadata": {},
197197
"output_type": "execute_result"
198198
}
@@ -210,7 +210,7 @@
210210
},
211211
{
212212
"cell_type": "code",
213-
"execution_count": 20,
213+
"execution_count": 9,
214214
"metadata": {},
215215
"outputs": [
216216
{
@@ -219,7 +219,7 @@
219219
"{'dog': 1, 'person': 2}"
220220
]
221221
},
222-
"execution_count": 20,
222+
"execution_count": 9,
223223
"metadata": {},
224224
"output_type": "execute_result"
225225
}
@@ -238,7 +238,7 @@
238238
},
239239
{
240240
"cell_type": "code",
241-
"execution_count": 21,
241+
"execution_count": 10,
242242
"metadata": {},
243243
"outputs": [
244244
{
@@ -247,7 +247,7 @@
247247
"[0.9995428, 0.9994912]"
248248
]
249249
},
250-
"execution_count": 21,
250+
"execution_count": 10,
251251
"metadata": {},
252252
"output_type": "execute_result"
253253
}
@@ -266,7 +266,7 @@
266266
},
267267
{
268268
"cell_type": "code",
269-
"execution_count": 22,
269+
"execution_count": 11,
270270
"metadata": {},
271271
"outputs": [
272272
{
@@ -275,7 +275,7 @@
275275
"0"
276276
]
277277
},
278-
"execution_count": 22,
278+
"execution_count": 11,
279279
"metadata": {},
280280
"output_type": "execute_result"
281281
}

usage-scene-detection.ipynb

Lines changed: 190 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)