Skip to content

Commit 6618b5c

Browse files
committed
Update notebooks
1 parent 11b2080 commit 6618b5c

File tree

4 files changed

+50
-101
lines changed

4 files changed

+50
-101
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
# deepstack-python
66
Unofficial python API for [DeepStack](https://python.deepstack.cc/). Provides class for making requests to the object detection endpoint, and functions for processing the result. See the Jupyter notebooks for usage.
7-
8-
## Services
9-
Face and object detection endpoints return bounding boxes of faces and objects respectively.
10-
11-
TODO: add face registration and recognition.
7+
Run deepstack (CPU, noAVX mode):
8+
```
9+
docker run -e VISION-DETECTION=True -e VISION-FACE=True -e MODE=High -d \
10+
-v localstorage:/datastore -p 5000:5000 \
11+
-e API-KEY="Mysecretkey" \
12+
--name deepstack deepquestai/deepstack:noavx
13+
```
1214

1315
## Development
1416
* Use `venv` -> `source venv/bin/activate`

deepstack/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def register_face(self, name: str, image_bytes: bytes):
161161
)
162162

163163
if response.status_code == 200 and response.json()["success"] == True:
164-
print("Taught face {} using file {}".format(name, file_path))
164+
return
165165
elif response.status_code == 200 and response.json()["success"] == False:
166166
error = response.json()["error"]
167-
print("Error: {}".format(error))
167+
raise DeepstackException(f"Error from Deepstack: {error}")

usage-face-recognition.ipynb

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"This package provides convenience classes and functions for working with deepstack face API. \n",
8-
"\n",
9-
"Run deepstack with:\n",
10-
"```\n",
11-
"docker run -e VISION-FACE=True -e MODE=High -d \\\n",
12-
" -v localstorage:/datastore -p 5000:5000 \\\n",
13-
" -e API-KEY=\"Mysecretkey\" \\\n",
14-
" --name deepstack deepquestai/deepstack:noavx\n",
15-
"```"
7+
"Work with the deepstack face API. "
168
]
179
},
1810
{
@@ -37,7 +29,7 @@
3729
"IP_ADDRESS = 'localhost'\n",
3830
"PORT = '5000'\n",
3931
"API_KEY = \"Mysecretkey\"\n",
40-
"TIMEOUT = 8"
32+
"TIMEOUT = 10 # Default is 10"
4133
]
4234
},
4335
{
@@ -96,15 +88,16 @@
9688
"name": "stdout",
9789
"output_type": "stream",
9890
"text": [
99-
"[{'confidence': 0.9999846, 'y_min': 162, 'x_min': 1620, 'y_max': 680, 'x_max': 1982}, {'confidence': 0.99997175, 'y_min': 230, 'x_min': 867, 'y_max': 729, 'x_max': 1199}]\n"
91+
"CPU times: user 8.38 ms, sys: 9.7 ms, total: 18.1 ms\n",
92+
"Wall time: 2.72 s\n"
10093
]
10194
}
10295
],
10396
"source": [
104-
"#%%time\n",
97+
"%%time\n",
10598
"try:\n",
106-
" dsface.process_file(image_path)\n",
107-
" print(dsface.predictions)\n",
99+
" with open(image_path, \"rb\") as image_bytes:\n",
100+
" dsface.detect(image_bytes)\n",
108101
"except ds.DeepstackException as exc:\n",
109102
" print(exc)"
110103
]
@@ -174,41 +167,33 @@
174167
},
175168
{
176169
"cell_type": "code",
177-
"execution_count": 8,
170+
"execution_count": 9,
178171
"metadata": {},
179172
"outputs": [
180173
{
181174
"name": "stdout",
182175
"output_type": "stream",
183176
"text": [
184-
"Taught face idris using file tests/images/couple.jpg\n"
177+
"CPU times: user 4.46 ms, sys: 3.62 ms, total: 8.09 ms\n",
178+
"Wall time: 2.78 s\n"
185179
]
186180
}
187181
],
188182
"source": [
189-
"dsface.register_face(name=\"idris\", file_path = 'tests/images/couple.jpg')"
183+
"%%time\n",
184+
"try:\n",
185+
" with open(image_path, \"rb\") as image_bytes:\n",
186+
" dsface.register_face(\"idris\", image_bytes)\n",
187+
"except ds.DeepstackException as exc:\n",
188+
" print(exc)"
190189
]
191190
},
192191
{
193-
"cell_type": "code",
194-
"execution_count": 10,
192+
"cell_type": "markdown",
195193
"metadata": {},
196-
"outputs": [
197-
{
198-
"ename": "FileNotFoundError",
199-
"evalue": "[Errno 2] No such file or directory: 'tests/images/couplesdf.jpg'",
200-
"output_type": "error",
201-
"traceback": [
202-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
203-
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
204-
"\u001b[0;32m<ipython-input-10-ab14dae0eb4a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdsface\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mregister_face\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"idris\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfile_path\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'tests/images/couplesdf.jpg'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
205-
"\u001b[0;32m~/Github/deepstack-python/deepstack/core.py\u001b[0m in \u001b[0;36mregister_face\u001b[0;34m(self, name, file_path)\u001b[0m\n\u001b[1;32m 155\u001b[0m \"\"\"\n\u001b[1;32m 156\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 157\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfile_path\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"rb\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 158\u001b[0m response = post_image(\n\u001b[1;32m 159\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mURL_FACE_REGISTRATION\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_ip_address\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_port\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
206-
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'tests/images/couplesdf.jpg'"
207-
]
208-
}
209-
],
210194
"source": [
211-
"dsface.register_face(name=\"idris\", file_path = 'tests/images/couplesdf.jpg')"
195+
"## Face recognition\n",
196+
"Recoginition will match any faces that have been taught. This is slower than face detection"
212197
]
213198
},
214199
{
@@ -219,17 +204,6 @@
219204
"The package provides helper functions for extracting info out of deepstack predictions"
220205
]
221206
},
222-
{
223-
"cell_type": "code",
224-
"execution_count": null,
225-
"metadata": {},
226-
"outputs": [],
227-
"source": [
228-
"name=\"idris\"\n",
229-
"file_path=\"foo.jpg\"\n",
230-
"print(\"Taught face {} using file {}\".format(name, file_path))"
231-
]
232-
},
233207
{
234208
"cell_type": "code",
235209
"execution_count": null,

usage-object-detection.ipynb

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"This package provides convenience classes and functions for working with deepstack object detection API.\n",
8-
"\n",
9-
"Run deepstack with:\n",
10-
"```\n",
11-
"docker run -e VISION-DETECTION=True -e MODE=High -d \\\n",
12-
" -v localstorage:/datastore -p 5000:5000 \\\n",
13-
" -e API-KEY=\"Mysecretkey\" \\\n",
14-
" --name deepstack deepquestai/deepstack:noavx\n",
15-
"```\n",
16-
"\n",
17-
"Note that by default, the minimum confidence for detected objects is 0.45"
7+
"Work with the deepstack object detection API (Yolo-v3). Note that by default, the minimum confidence for detected objects is 0.45"
188
]
199
},
2010
{
@@ -32,14 +22,14 @@
3222
},
3323
{
3424
"cell_type": "code",
35-
"execution_count": 2,
25+
"execution_count": 15,
3626
"metadata": {},
3727
"outputs": [],
3828
"source": [
3929
"IP_ADDRESS = 'localhost'\n",
4030
"PORT = '5000'\n",
4131
"API_KEY = \"Mysecretkey\"\n",
42-
"TIMEOUT = 8"
32+
"TIMEOUT = 10 # Default is 10"
4333
]
4434
},
4535
{
@@ -86,26 +76,29 @@
8676
"cell_type": "markdown",
8777
"metadata": {},
8878
"source": [
89-
"Perform object detection"
79+
"Perform object detection - can be slow"
9080
]
9181
},
9282
{
9383
"cell_type": "code",
94-
"execution_count": 6,
84+
"execution_count": 14,
9585
"metadata": {},
9686
"outputs": [
9787
{
9888
"name": "stdout",
9989
"output_type": "stream",
10090
"text": [
101-
"[{'confidence': 0.9998661, 'label': 'person', 'y_min': 0, 'x_min': 258, 'y_max': 676, 'x_max': 485}, {'confidence': 0.9996547, 'label': 'person', 'y_min': 0, 'x_min': 405, 'y_max': 652, 'x_max': 639}, {'confidence': 0.99745613, 'label': 'dog', 'y_min': 311, 'x_min': 624, 'y_max': 591, 'x_max': 825}]\n"
91+
"Timeout connecting to Deepstack, current timeout is 8 seconds\n",
92+
"CPU times: user 3.88 ms, sys: 2.42 ms, total: 6.3 ms\n",
93+
"Wall time: 8.02 s\n"
10294
]
10395
}
10496
],
10597
"source": [
106-
"#%%time\n",
98+
"%%time\n",
10799
"try:\n",
108-
" dsobject.process_file(image_path)\n",
100+
" with open(image_path, 'rb') as image_bytes:\n",
101+
" dsobject.detect(image_bytes)\n",
109102
"except ds.DeepstackException as exc:\n",
110103
" print(exc)"
111104
]
@@ -156,7 +149,7 @@
156149
},
157150
{
158151
"cell_type": "code",
159-
"execution_count": 14,
152+
"execution_count": 8,
160153
"metadata": {},
161154
"outputs": [
162155
{
@@ -165,7 +158,7 @@
165158
"3"
166159
]
167160
},
168-
"execution_count": 14,
161+
"execution_count": 8,
169162
"metadata": {},
170163
"output_type": "execute_result"
171164
}
@@ -186,7 +179,7 @@
186179
"cell_type": "markdown",
187180
"metadata": {},
188181
"source": [
189-
"Get the set of unique labels"
182+
"Get the set objects"
190183
]
191184
},
192185
{
@@ -206,14 +199,14 @@
206199
}
207200
],
208201
"source": [
209-
"ds.get_object_labels(dsobject.predictions)"
202+
"ds.get_objects(dsobject.predictions)"
210203
]
211204
},
212205
{
213206
"cell_type": "markdown",
214207
"metadata": {},
215208
"source": [
216-
"Get a summary of the number of occurances of labels"
209+
"Get a summary of the number of occurances of objects"
217210
]
218211
},
219212
{
@@ -237,31 +230,11 @@
237230
"summary"
238231
]
239232
},
240-
{
241-
"cell_type": "code",
242-
"execution_count": 11,
243-
"metadata": {},
244-
"outputs": [
245-
{
246-
"data": {
247-
"text/plain": [
248-
"['dog', 'person']"
249-
]
250-
},
251-
"execution_count": 11,
252-
"metadata": {},
253-
"output_type": "execute_result"
254-
}
255-
],
256-
"source": [
257-
"list(summary.keys())"
258-
]
259-
},
260233
{
261234
"cell_type": "markdown",
262235
"metadata": {},
263236
"source": [
264-
"Get a list of confidences for a single label type, e.g. `person`"
237+
"Get a list of confidences for a single object type, e.g. `person`"
265238
]
266239
},
267240
{
@@ -281,8 +254,8 @@
281254
}
282255
],
283256
"source": [
284-
"confidences = ds.get_label_confidences(dsobject.predictions, 'person')\n",
285-
"confidences"
257+
"person_confidences = ds.get_object_confidences(dsobject.predictions, 'person')\n",
258+
"person_confidences"
286259
]
287260
},
288261
{
@@ -294,7 +267,7 @@
294267
},
295268
{
296269
"cell_type": "code",
297-
"execution_count": 16,
270+
"execution_count": 13,
298271
"metadata": {},
299272
"outputs": [
300273
{
@@ -303,14 +276,14 @@
303276
"1"
304277
]
305278
},
306-
"execution_count": 16,
279+
"execution_count": 13,
307280
"metadata": {},
308281
"output_type": "execute_result"
309282
}
310283
],
311284
"source": [
312285
"CONFIDENCE_THRESHOLD = 0.9997\n",
313-
"len(ds.get_confidences_above_threshold(confidences, CONFIDENCE_THRESHOLD))"
286+
"len(ds.get_confidences_above_threshold(person_confidences, CONFIDENCE_THRESHOLD))"
314287
]
315288
},
316289
{

0 commit comments

Comments
 (0)