Skip to content

Adds scene #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@
[![build status](http://img.shields.io/travis/robmarkcole/deepstack-python/master.svg?style=flat)](https://travis-ci.org/robmarkcole/deepstack-python)

# deepstack-python
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.
Run deepstack (CPU, noAVX mode):
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.

Run deepstack with all three endpoints active (CPU, noAVX mode):
```
docker run \
-e VISION-SCENE=True \
-e VISION-DETECTION=True \
-e VISION-FACE=True \
-v localstorage:/datastore \
-p 5000:5000 \
-e API-KEY="" \
--name deepstack deepquestai/deepstack:noavx
```
Check deepstack is running using curl (from root of this repo):
```
curl -X POST -F image=@tests/images/test-image3.jpg 'http://localhost:5000/v1/vision/detection'
```
docker run -e VISION-DETECTION=True -e VISION-FACE=True -e MODE=High -d \
-v localstorage:/datastore -p 5000:5000 \
-e API-KEY="Mysecretkey" \
--name deepstack deepquestai/deepstack:noavx
If all goes well you should see the following returned:
```
{"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}]}
```

## Development
* Use `venv` -> `source venv/bin/activate`
* `pip install -r requirements-dev.txt`
* Create venv -> `python3.7 -m venv venv`
* Use venv -> `source venv/bin/activate`
* `pip3 install -r requirements.txt` and `pip3 install -r requirements-dev.txt`
* Run tests with `venv/bin/pytest tests/*`
* Black format with `venv/bin/black deepstack/core.py` and `venv/bin/black tests/test_deepstack.py`

## Jupyter
* Docs are created using Jupyter notebooks
* Install in venv with -> `pip3 install jupyterlab`
* Run -> `venv/bin/jupyter lab`

## Deployment to pypi
* Generate requirements with -> `pip3 freeze > requirements.txt`
* Create a distribution with -> `python3 setup.py sdist` (creates dist)
* Upload packages with twine with -> `twine upload dist/*`
66 changes: 49 additions & 17 deletions deepstack/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
URL_FACE_DETECTION = "http://{}:{}/v1/vision/face"
URL_FACE_REGISTRATION = "http://{}:{}/v1/vision/face/register"
URL_FACE_RECOGNITION = "http://{}:{}/v1/vision/face/recognize"
URL_SCENE_DETECTION = "http://{}:{}/v1/vision/scene"


def format_confidence(confidence: Union[str, float]) -> float:
Expand Down Expand Up @@ -113,26 +114,27 @@ def __init__(
self._url_detection = url_detection
self._api_key = api_key
self._timeout = timeout
self._predictions = []
self._response = None

def detect(self, image_bytes: bytes):
"""Process image_bytes, performing detection."""
self._predictions = []
self._response = None
url = self._url_detection.format(self._ip_address, self._port)

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

if response.status_code == HTTP_OK:
if response.json()["success"]:
self._predictions = response.json()["predictions"]
else:
error = response.json()["error"]
raise DeepstackException(f"Error from Deepstack: {error}")
if not response.status_code == HTTP_OK:
raise DeepstackException(f"Error from request: {response.status_code}")
return

self._response = response.json()
if not self._response["success"]:
raise DeepstackException(f"Error from Deepstack: {error}")

@property
def predictions(self):
"""Return the classifier attributes."""
return self._predictions
"""Return the predictions."""
raise NotImplementedError


class DeepstackObject(Deepstack):
Expand All @@ -149,6 +151,31 @@ def __init__(
ip_address, port, api_key, timeout, url_detection=URL_OBJECT_DETECTION
)

@property
def predictions(self):
"""Return the predictions."""
return self._response["predictions"]


class DeepstackScene(Deepstack):
"""Work with scenes"""

def __init__(
self,
ip_address: str,
port: str,
api_key: str = "",
timeout: int = DEFAULT_TIMEOUT,
):
super().__init__(
ip_address, port, api_key, timeout, url_detection=URL_SCENE_DETECTION
)

@property
def predictions(self):
"""Return the predictions."""
return self._response


class DeepstackFace(Deepstack):
"""Work with objects"""
Expand All @@ -164,6 +191,11 @@ def __init__(
ip_address, port, api_key, timeout, url_detection=URL_FACE_DETECTION
)

@property
def predictions(self):
"""Return the classifier attributes."""
return self._response["predictions"]

def register_face(self, name: str, image_bytes: bytes):
"""
Register a face name to a file.
Expand All @@ -185,14 +217,14 @@ def register_face(self, name: str, image_bytes: bytes):

def recognise(self, image_bytes: bytes):
"""Process image_bytes, performing recognition."""
self._predictions = []
url = URL_FACE_RECOGNITION.format(self._ip_address, self._port)

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

if response.status_code == HTTP_OK:
if response.json()["success"]:
self._predictions = response.json()["predictions"]
else:
error = response.json()["error"]
raise DeepstackException(f"Error from Deepstack: {error}")
if not response.status_code == HTTP_OK:
raise DeepstackException(f"Error from request: {response.status_code}")
return

self._response = response.json()
if not self._response["success"]:
raise DeepstackException(f"Error from Deepstack: {error}")
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
black
requests_mock
requests_mock
matplotlib
68 changes: 35 additions & 33 deletions usage-face-recognition.ipynb

Large diffs are not rendered by default.

78 changes: 39 additions & 39 deletions usage-object-detection.ipynb

Large diffs are not rendered by default.

190 changes: 190 additions & 0 deletions usage-scene-detection.ipynb

Large diffs are not rendered by default.