Skip to content

Commit 665088c

Browse files
authored
Merge pull request #13 from robmarkcole/add-face
Add face api
2 parents aeeca27 + 73ffdcd commit 665088c

File tree

6 files changed

+310
-42
lines changed

6 files changed

+310
-42
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
[![build status](http://img.shields.io/travis/robmarkcole/deepstack-python/master.svg?style=flat)](https://travis-ci.org/robmarkcole/deepstack-python)
44

55
# deepstack-python
6-
Unofficial python API for DeepStack. Provides class for making requests to the object detection endpoint, and functions for processing the result. See the `usage.ipynb` notebook for usage.
6+
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.
712

813
## Development
9-
* Use `venv`
14+
* Use `venv` -> `source venv/bin/activate`
1015
* `pip install -r requirements-dev.txt`
1116
* Run tests with `venv/bin/pytest tests/*`
17+
* Black format with `venv/bin/black deepstack/core.py`

deepstack/core.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
HTTP_OK = 200
1010
DEFAULT_TIMEOUT = 10 # seconds
1111

12+
## API urls
13+
URL_OBJECT_DETECTION = "http://{}:{}/v1/vision/detection"
14+
URL_FACE_DETECTION = "http://{}:{}/v1/vision/face"
15+
1216

1317
def format_confidence(confidence: Union[str, float]) -> float:
1418
"""Takes a confidence from the API like
@@ -70,21 +74,21 @@ class DeepstackException(Exception):
7074
pass
7175

7276

73-
class DeepstackObject:
74-
"""The object detection API locates and classifies 80
75-
different kinds of objects in a single image.."""
77+
class Deepstack(object):
78+
"""Base class for deepstack."""
7679

7780
def __init__(
7881
self,
7982
ip_address: str,
8083
port: str,
8184
api_key: str = "",
8285
timeout: int = DEFAULT_TIMEOUT,
86+
url_detection: str = "",
8387
):
8488

85-
self._url_object_detection = "http://{}:{}/v1/vision/detection".format(
86-
ip_address, port
87-
)
89+
self._ip_address = ip_address
90+
self._port = port
91+
self._url_detection = url_detection
8892
self._api_key = api_key
8993
self._timeout = timeout
9094
self._predictions = []
@@ -97,10 +101,9 @@ def process_file(self, file_path: str):
97101
def process_image_bytes(self, image_bytes: bytes):
98102
"""Process an image."""
99103
self._predictions = []
104+
url = self._url_detection.format(self._ip_address, self._port)
100105

101-
response = post_image(
102-
self._url_object_detection, image_bytes, self._api_key, self._timeout
103-
)
106+
response = post_image(url, image_bytes, self._api_key, self._timeout)
104107

105108
if response.status_code == HTTP_OK:
106109
if response.json()["success"]:
@@ -113,3 +116,32 @@ def process_image_bytes(self, image_bytes: bytes):
113116
def predictions(self):
114117
"""Return the classifier attributes."""
115118
return self._predictions
119+
120+
121+
class DeepstackObject(Deepstack):
122+
"""Work with objects"""
123+
124+
def __init__(
125+
self,
126+
ip_address: str,
127+
port: str,
128+
api_key: str = "",
129+
timeout: int = DEFAULT_TIMEOUT,
130+
):
131+
super().__init__(
132+
ip_address, port, api_key, timeout, url_detection=URL_OBJECT_DETECTION
133+
)
134+
135+
class DeepstackFace(Deepstack):
136+
"""Work with objects"""
137+
138+
def __init__(
139+
self,
140+
ip_address: str,
141+
port: str,
142+
api_key: str = "",
143+
timeout: int = DEFAULT_TIMEOUT,
144+
):
145+
super().__init__(
146+
ip_address, port, api_key, timeout, url_detection=URL_FACE_DETECTION
147+
)

tests/images/couple.jpg

1.29 MB
Loading

tests/images/idris.jpg

205 KB
Loading

usage-face-recognition.ipynb

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

usage.ipynb renamed to usage-object-detection.ipynb

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
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-
"Pull the latest image for noavx:\n",
9-
"```\n",
10-
"docker pull deepquestai/deepstack:noavx\n",
11-
"```\n",
7+
"This package provides convenience classes and functions for working with deepstack object detection API.\n",
128
"\n",
139
"Run deepstack with:\n",
1410
"```\n",
15-
"docker run -e VISION-DETECTION=True -e VISION-FACE=True -e MODE=High -d \\\n",
11+
"docker run -e VISION-DETECTION=True -e MODE=High -d \\\n",
1612
" -v localstorage:/datastore -p 5000:5000 \\\n",
1713
" -e API-KEY=\"Mysecretkey\" \\\n",
1814
" --name deepstack deepquestai/deepstack:noavx\n",
@@ -46,6 +42,13 @@
4642
"TIMEOUT = 8"
4743
]
4844
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## Object detection"
50+
]
51+
},
4952
{
5053
"cell_type": "code",
5154
"execution_count": 3,
@@ -83,12 +86,12 @@
8386
"cell_type": "markdown",
8487
"metadata": {},
8588
"source": [
86-
"Process an image directly"
89+
"Perform object detection"
8790
]
8891
},
8992
{
9093
"cell_type": "code",
91-
"execution_count": 5,
94+
"execution_count": 6,
9295
"metadata": {},
9396
"outputs": [
9497
{
@@ -117,7 +120,7 @@
117120
},
118121
{
119122
"cell_type": "code",
120-
"execution_count": 6,
123+
"execution_count": 7,
121124
"metadata": {},
122125
"outputs": [
123126
{
@@ -143,7 +146,7 @@
143146
" 'x_max': 825}]"
144147
]
145148
},
146-
"execution_count": 6,
149+
"execution_count": 7,
147150
"metadata": {},
148151
"output_type": "execute_result"
149152
}
@@ -153,20 +156,31 @@
153156
]
154157
},
155158
{
156-
"cell_type": "markdown",
159+
"cell_type": "code",
160+
"execution_count": 14,
157161
"metadata": {},
162+
"outputs": [
163+
{
164+
"data": {
165+
"text/plain": [
166+
"3"
167+
]
168+
},
169+
"execution_count": 14,
170+
"metadata": {},
171+
"output_type": "execute_result"
172+
}
173+
],
158174
"source": [
159-
"## Helper functions\n",
160-
"The package provides helper functions for extracting info out of deepstack predictions"
175+
"len(dsobject.predictions)"
161176
]
162177
},
163178
{
164-
"cell_type": "code",
165-
"execution_count": 7,
179+
"cell_type": "markdown",
166180
"metadata": {},
167-
"outputs": [],
168181
"source": [
169-
"# help(ds)"
182+
"## Helper functions\n",
183+
"The package provides helper functions for extracting info out of deepstack predictions"
170184
]
171185
},
172186
{
@@ -178,16 +192,16 @@
178192
},
179193
{
180194
"cell_type": "code",
181-
"execution_count": 8,
195+
"execution_count": 9,
182196
"metadata": {},
183197
"outputs": [
184198
{
185199
"data": {
186200
"text/plain": [
187-
"['person', 'dog']"
201+
"['dog', 'person']"
188202
]
189203
},
190-
"execution_count": 8,
204+
"execution_count": 9,
191205
"metadata": {},
192206
"output_type": "execute_result"
193207
}
@@ -205,16 +219,16 @@
205219
},
206220
{
207221
"cell_type": "code",
208-
"execution_count": 9,
222+
"execution_count": 10,
209223
"metadata": {},
210224
"outputs": [
211225
{
212226
"data": {
213227
"text/plain": [
214-
"{'person': 2, 'dog': 1}"
228+
"{'dog': 1, 'person': 2}"
215229
]
216230
},
217-
"execution_count": 9,
231+
"execution_count": 10,
218232
"metadata": {},
219233
"output_type": "execute_result"
220234
}
@@ -226,16 +240,16 @@
226240
},
227241
{
228242
"cell_type": "code",
229-
"execution_count": 10,
243+
"execution_count": 11,
230244
"metadata": {},
231245
"outputs": [
232246
{
233247
"data": {
234248
"text/plain": [
235-
"['person', 'dog']"
249+
"['dog', 'person']"
236250
]
237251
},
238-
"execution_count": 10,
252+
"execution_count": 11,
239253
"metadata": {},
240254
"output_type": "execute_result"
241255
}
@@ -253,7 +267,7 @@
253267
},
254268
{
255269
"cell_type": "code",
256-
"execution_count": 11,
270+
"execution_count": 12,
257271
"metadata": {},
258272
"outputs": [
259273
{
@@ -262,7 +276,7 @@
262276
"[0.9998661, 0.9996547]"
263277
]
264278
},
265-
"execution_count": 11,
279+
"execution_count": 12,
266280
"metadata": {},
267281
"output_type": "execute_result"
268282
}
@@ -281,12 +295,23 @@
281295
},
282296
{
283297
"cell_type": "code",
284-
"execution_count": null,
298+
"execution_count": 16,
285299
"metadata": {},
286-
"outputs": [],
300+
"outputs": [
301+
{
302+
"data": {
303+
"text/plain": [
304+
"1"
305+
]
306+
},
307+
"execution_count": 16,
308+
"metadata": {},
309+
"output_type": "execute_result"
310+
}
311+
],
287312
"source": [
288313
"CONFIDENCE_THRESHOLD = 0.9997\n",
289-
"print(len(ds.get_confidences_above_threshold(confidences, CONFIDENCE_THRESHOLD)))"
314+
"len(ds.get_confidences_above_threshold(confidences, CONFIDENCE_THRESHOLD))"
290315
]
291316
},
292317
{

0 commit comments

Comments
 (0)