Skip to content

Commit 7583a02

Browse files
authored
Merge pull request #19 from robmarkcole/adds-scene
Adds scene
2 parents e8a8511 + 7510afa commit 7583a02

File tree

6 files changed

+347
-98
lines changed

6 files changed

+347
-98
lines changed

README.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,41 @@
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](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-
Run deepstack (CPU, noAVX mode):
6+
Unofficial python API for [DeepStack](https://python.deepstack.cc/). Provides classes for making requests to the object detection & face detection/recognition endpoints. Also includes some helper functions for processing the results. See the Jupyter notebooks for usage.
7+
8+
Run deepstack with all three endpoints active (CPU, noAVX mode):
9+
```
10+
docker run \
11+
-e VISION-SCENE=True \
12+
-e VISION-DETECTION=True \
13+
-e VISION-FACE=True \
14+
-v localstorage:/datastore \
15+
-p 5000:5000 \
16+
-e API-KEY="" \
17+
--name deepstack deepquestai/deepstack:noavx
18+
```
19+
Check deepstack is running using curl (from root of this repo):
20+
```
21+
curl -X POST -F image=@tests/images/test-image3.jpg 'http://localhost:5000/v1/vision/detection'
822
```
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
23+
If all goes well you should see the following returned:
24+
```
25+
{"success":true,"predictions":[{"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}]}
1326
```
1427

1528
## Development
16-
* Use `venv` -> `source venv/bin/activate`
17-
* `pip install -r requirements-dev.txt`
29+
* Create venv -> `python3.7 -m venv venv`
30+
* Use venv -> `source venv/bin/activate`
31+
* `pip3 install -r requirements.txt` and `pip3 install -r requirements-dev.txt`
1832
* Run tests with `venv/bin/pytest tests/*`
1933
* Black format with `venv/bin/black deepstack/core.py` and `venv/bin/black tests/test_deepstack.py`
34+
35+
## Jupyter
36+
* Docs are created using Jupyter notebooks
37+
* Install in venv with -> `pip3 install jupyterlab`
38+
* Run -> `venv/bin/jupyter lab`
39+
40+
## Deployment to pypi
41+
* Generate requirements with -> `pip3 freeze > requirements.txt`
42+
* Create a distribution with -> `python3 setup.py sdist` (creates dist)
43+
* Upload packages with twine with -> `twine upload dist/*`

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}")

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pytest
22
black
3-
requests_mock
3+
requests_mock
4+
matplotlib

usage-face-recognition.ipynb

Lines changed: 35 additions & 33 deletions
Large diffs are not rendered by default.

usage-object-detection.ipynb

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

usage-scene-detection.ipynb

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

0 commit comments

Comments
 (0)