Skip to content

Commit 63f224f

Browse files
committed
Adds super class
1 parent aeeca27 commit 63f224f

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
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 `usage.ipynb` notebook 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: 27 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,18 @@ 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+
)

usage.ipynb

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
},
8989
{
9090
"cell_type": "code",
91-
"execution_count": 5,
91+
"execution_count": 6,
9292
"metadata": {},
9393
"outputs": [
9494
{
@@ -117,7 +117,7 @@
117117
},
118118
{
119119
"cell_type": "code",
120-
"execution_count": 6,
120+
"execution_count": 7,
121121
"metadata": {},
122122
"outputs": [
123123
{
@@ -143,7 +143,7 @@
143143
" 'x_max': 825}]"
144144
]
145145
},
146-
"execution_count": 6,
146+
"execution_count": 7,
147147
"metadata": {},
148148
"output_type": "execute_result"
149149
}
@@ -162,7 +162,7 @@
162162
},
163163
{
164164
"cell_type": "code",
165-
"execution_count": 7,
165+
"execution_count": 8,
166166
"metadata": {},
167167
"outputs": [],
168168
"source": [
@@ -178,16 +178,16 @@
178178
},
179179
{
180180
"cell_type": "code",
181-
"execution_count": 8,
181+
"execution_count": 9,
182182
"metadata": {},
183183
"outputs": [
184184
{
185185
"data": {
186186
"text/plain": [
187-
"['person', 'dog']"
187+
"['dog', 'person']"
188188
]
189189
},
190-
"execution_count": 8,
190+
"execution_count": 9,
191191
"metadata": {},
192192
"output_type": "execute_result"
193193
}
@@ -205,16 +205,16 @@
205205
},
206206
{
207207
"cell_type": "code",
208-
"execution_count": 9,
208+
"execution_count": 10,
209209
"metadata": {},
210210
"outputs": [
211211
{
212212
"data": {
213213
"text/plain": [
214-
"{'person': 2, 'dog': 1}"
214+
"{'dog': 1, 'person': 2}"
215215
]
216216
},
217-
"execution_count": 9,
217+
"execution_count": 10,
218218
"metadata": {},
219219
"output_type": "execute_result"
220220
}
@@ -226,16 +226,16 @@
226226
},
227227
{
228228
"cell_type": "code",
229-
"execution_count": 10,
229+
"execution_count": 11,
230230
"metadata": {},
231231
"outputs": [
232232
{
233233
"data": {
234234
"text/plain": [
235-
"['person', 'dog']"
235+
"['dog', 'person']"
236236
]
237237
},
238-
"execution_count": 10,
238+
"execution_count": 11,
239239
"metadata": {},
240240
"output_type": "execute_result"
241241
}
@@ -253,7 +253,7 @@
253253
},
254254
{
255255
"cell_type": "code",
256-
"execution_count": 11,
256+
"execution_count": 12,
257257
"metadata": {},
258258
"outputs": [
259259
{
@@ -262,7 +262,7 @@
262262
"[0.9998661, 0.9996547]"
263263
]
264264
},
265-
"execution_count": 11,
265+
"execution_count": 12,
266266
"metadata": {},
267267
"output_type": "execute_result"
268268
}
@@ -281,9 +281,17 @@
281281
},
282282
{
283283
"cell_type": "code",
284-
"execution_count": null,
284+
"execution_count": 13,
285285
"metadata": {},
286-
"outputs": [],
286+
"outputs": [
287+
{
288+
"name": "stdout",
289+
"output_type": "stream",
290+
"text": [
291+
"1\n"
292+
]
293+
}
294+
],
287295
"source": [
288296
"CONFIDENCE_THRESHOLD = 0.9997\n",
289297
"print(len(ds.get_confidences_above_threshold(confidences, CONFIDENCE_THRESHOLD)))"

0 commit comments

Comments
 (0)