Skip to content

Commit c234c06

Browse files
committed
formatting
1 parent 354619c commit c234c06

File tree

1 file changed

+38
-72
lines changed

1 file changed

+38
-72
lines changed

deepstack/core.py

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
URL_CUSTOM = "http://{ip}:{port}/v1/vision/custom/{custom_model}"
1414
URL_OBJECT_DETECTION = "http://{ip}:{port}/v1/vision/detection"
1515
URL_FACE_DETECTION = "http://{ip}:{port}/v1/vision/face"
16-
URL_FACE_REGISTRATION = "http://{ip}:{port}/v1/vision/face/register"
17-
URL_FACE_RECOGNITION = "http://{ip}:{port}/v1/vision/face/recognize"
16+
URL_FACE_REGISTER = "http://{ip}:{port}/v1/vision/face/register"
17+
URL_FACE_RECOGNIZE = "http://{ip}:{port}/v1/vision/face/recognize"
1818
URL_SCENE_DETECTION = "http://{ip}:{port}/v1/vision/scene"
1919

2020

@@ -25,16 +25,14 @@ def format_confidence(confidence: Union[str, float]) -> float:
2525
return round(float(confidence) * 100, 1)
2626

2727

28-
def get_confidences_above_threshold(
29-
confidences: List[float], confidence_threshold: float
30-
) -> List[float]:
28+
def get_confidences_above_threshold(confidences: List[float], confidence_threshold: float) -> List[float]:
3129
"""Takes a list of confidences and returns those above a confidence_threshold."""
3230
return [val for val in confidences if val >= confidence_threshold]
3331

3432

35-
def get_recognised_faces(predictions: List[Dict]) -> List[Dict]:
33+
def get_recognized_faces(predictions: List[Dict]) -> List[Dict]:
3634
"""
37-
Get the recognised faces.
35+
Get the recognized faces.
3836
"""
3937
try:
4038
matched_faces = {
@@ -59,9 +57,7 @@ def get_object_confidences(predictions: List[Dict], target_object: str):
5957
"""
6058
Return the list of confidences of instances of target label.
6159
"""
62-
confidences = [
63-
pred["confidence"] for pred in predictions if pred["label"] == target_object
64-
]
60+
confidences = [pred["confidence"] for pred in predictions if pred["label"] == target_object]
6561
return confidences
6662

6763

@@ -70,26 +66,17 @@ def get_objects_summary(predictions: List[Dict]):
7066
Get a summary of the objects detected.
7167
"""
7268
objects = get_objects(predictions)
73-
return {
74-
target_object: len(get_object_confidences(predictions, target_object))
75-
for target_object in objects
76-
}
69+
return {target_object: len(get_object_confidences(predictions, target_object)) for target_object in objects}
7770

7871

79-
def post_image(
80-
url: str, image_bytes: bytes, api_key: str, timeout: int, data: dict = {}
81-
):
72+
def post_image(url: str, image_bytes: bytes, api_key: str, timeout: int, data: dict = {}):
8273
"""Post an image to Deepstack."""
8374
try:
84-
data["api_key"] = api_key
85-
response = requests.post(
86-
url, files={"image": image_bytes}, data=data, timeout=timeout
87-
)
75+
data["api_key"] = api_key # Insert the api_key
76+
response = requests.post(url, files={"image": image_bytes}, data=data, timeout=timeout)
8877
return response
8978
except requests.exceptions.Timeout:
90-
raise DeepstackException(
91-
f"Timeout connecting to Deepstack, current timeout is {timeout} seconds"
92-
)
79+
raise DeepstackException(f"Timeout connecting to Deepstack, current timeout is {timeout} seconds")
9380
except requests.exceptions.ConnectionError as exc:
9481
raise DeepstackException(f"Connection error: {exc}")
9582

@@ -106,35 +93,39 @@ def __init__(
10693
api_key: str = "",
10794
timeout: int = DEFAULT_TIMEOUT,
10895
url_detect: str = None,
109-
url_recognise: str = None,
96+
url_recognize: str = None,
11097
url_register: str = None,
11198
):
11299

113100
self._url_detect = url_detect
114-
self._url_recognise = url_recognise
101+
self._url_recognize = url_recognize
115102
self._url_register = url_register
116103
self._api_key = api_key
117104
self._timeout = timeout
118105
self._response = None
119106

120107
def detect(self, image_bytes: bytes):
121-
"""Process image_bytes, performing detection."""
108+
"""Process image_bytes and detect."""
122109
self._response = None
123-
response = post_image(
124-
self._url_detect, image_bytes, self._api_key, self._timeout
125-
)
110+
response = post_image(self._url_detect, image_bytes, self._api_key, self._timeout)
126111

127112
if not response.status_code == HTTP_OK:
128-
raise DeepstackException(
129-
f"Error from request, status code: {response.status_code}"
130-
)
113+
raise DeepstackException(f"Error from request, status code: {response.status_code}")
131114
return
132115

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

121+
def recognize(self):
122+
"""Process image_bytes and recognize."""
123+
raise NotImplementedError
124+
125+
def register(self):
126+
"""Perform a registration."""
127+
raise NotImplementedError
128+
138129
@property
139130
def predictions(self):
140131
"""Return the predictions."""
@@ -145,26 +136,15 @@ class DeepstackObject(Deepstack):
145136
"""Work with objects"""
146137

147138
def __init__(
148-
self,
149-
ip: str,
150-
port: str,
151-
api_key: str = "",
152-
timeout: int = DEFAULT_TIMEOUT,
153-
custom_model: str = None,
139+
self, ip: str, port: str, api_key: str = "", timeout: int = DEFAULT_TIMEOUT, custom_model: str = None,
154140
):
155141
if not custom_model:
156142
super().__init__(
157-
api_key,
158-
timeout,
159-
url_detect=URL_OBJECT_DETECTION.format(ip=ip, port=port),
143+
api_key, timeout, url_detect=URL_OBJECT_DETECTION.format(ip=ip, port=port),
160144
)
161145
else:
162146
super().__init__(
163-
api_key,
164-
timeout,
165-
url_detect=URL_CUSTOM.format(
166-
ip=ip, port=port, custom_model=custom_model
167-
),
147+
api_key, timeout, url_detect=URL_CUSTOM.format(ip=ip, port=port, custom_model=custom_model),
168148
)
169149

170150
@property
@@ -177,16 +157,10 @@ class DeepstackScene(Deepstack):
177157
"""Work with scenes"""
178158

179159
def __init__(
180-
self,
181-
ip: str,
182-
port: str,
183-
api_key: str = "",
184-
timeout: int = DEFAULT_TIMEOUT,
160+
self, ip: str, port: str, api_key: str = "", timeout: int = DEFAULT_TIMEOUT,
185161
):
186162
super().__init__(
187-
api_key,
188-
timeout,
189-
url_detect=URL_SCENE_DETECTION.format(ip=self._ip, port=self._port),
163+
api_key, timeout, url_detect=URL_SCENE_DETECTION.format(ip=self._ip, port=self._port),
190164
)
191165

192166
@property
@@ -199,26 +173,22 @@ class DeepstackFace(Deepstack):
199173
"""Work with objects"""
200174

201175
def __init__(
202-
self,
203-
ip: str,
204-
port: str,
205-
api_key: str = "",
206-
timeout: int = DEFAULT_TIMEOUT,
176+
self, ip: str, port: str, api_key: str = "", timeout: int = DEFAULT_TIMEOUT,
207177
):
208178
super().__init__(
209179
api_key,
210180
timeout,
211181
url_detect=URL_FACE_DETECTION.format(ip=self._ip, port=self._port),
212-
url_register=URL_FACE_REGISTRATION.format(ip=self._ip, port=self._port),
213-
url_recognise=URL_FACE_RECOGNITION.format(ip=self._ip, port=self._port),
182+
url_register=URL_FACE_REGISTER.format(ip=self._ip, port=self._port),
183+
url_recognize=URL_FACE_RECOGNIZE.format(ip=self._ip, port=self._port),
214184
)
215185

216186
@property
217187
def predictions(self):
218188
"""Return the classifier attributes."""
219189
return self._response["predictions"]
220190

221-
def register_face(self, name: str, image_bytes: bytes):
191+
def register(self, name: str, image_bytes: bytes):
222192
"""
223193
Register a face name to a file.
224194
"""
@@ -231,24 +201,20 @@ def register_face(self, name: str, image_bytes: bytes):
231201
data={"userid": name},
232202
)
233203

234-
if response.status_code == 200 and response.json()["success"] == True:
204+
if response.status_code == HTTP_OK and response.json()["success"] == True:
235205
return
236206

237-
elif response.status_code == 200 and response.json()["success"] == False:
207+
elif response.status_code == HTTP_OK and response.json()["success"] == False:
238208
error = response.json()["error"]
239209
raise DeepstackException(f"Error from Deepstack: {error}")
240210

241-
def recognise(self, image_bytes: bytes):
211+
def recognize(self, image_bytes: bytes):
242212
"""Process image_bytes, performing recognition."""
243213

244-
response = post_image(
245-
self._url_recognise, image_bytes, self._api_key, self._timeout
246-
)
214+
response = post_image(self._url_recognize, image_bytes, self._api_key, self._timeout)
247215

248216
if not response.status_code == HTTP_OK:
249-
raise DeepstackException(
250-
f"Error from request, status code: {response.status_code}"
251-
)
217+
raise DeepstackException(f"Error from request, status code: {response.status_code}")
252218
return
253219

254220
self._response = response.json()

0 commit comments

Comments
 (0)