Skip to content

Commit f42a90a

Browse files
committed
Adds type hints
1 parent 0c968a6 commit f42a90a

File tree

5 files changed

+202
-63
lines changed

5 files changed

+202
-63
lines changed

deepstack/core.py

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,74 @@
33
"""
44
import requests
55
from PIL import Image
6+
from typing import Union, List, Set, Dict
67

78
## Const
89
HTTP_OK = 200
910
HTTP_BAD_REQUEST = 400
1011
HTTP_UNAUTHORIZED = 401
11-
TIMEOUT = 30 # seconds
12+
TIMEOUT = 20 # seconds
1213

1314

14-
def format_confidence(confidence):
15+
def format_confidence(confidence: Union[str, float]) -> float:
1516
"""Takes a confidence from the API like
1617
0.55623 and returne 55.6 (%).
1718
"""
18-
return round(float(confidence)*100, 1)
19+
return round(float(confidence) * 100, 1)
1920

2021

21-
def get_matched_faces(predictions: dict):
22+
def get_confidences_above_threshold(
23+
confidences: List[float], confidence_threshold: float
24+
) -> List[float]:
25+
"""Takes a list of confidences and returns those above a confidence_threshold."""
26+
return [val for val in confidences if val >= confidence_threshold]
27+
28+
29+
def get_object_labels(predictions: List[Dict]) -> Set[str]:
30+
"""
31+
Get a list of the unique object labels predicted.
32+
"""
33+
labels = [pred["label"] for pred in predictions]
34+
return set(labels)
35+
36+
37+
def get_label_confidences(predictions: List[Dict], target_label: str):
38+
"""
39+
Return the list of confidences of instances of target label.
2240
"""
23-
Get the predicted faces and their confidence.
41+
confidences = [
42+
pred["confidence"] for pred in predictions if pred["label"] == target_label
43+
]
44+
return confidences
45+
46+
47+
def get_objects_summary(predictions: List[Dict]):
2448
"""
25-
matched_faces = {}
26-
matched_faces = {
27-
face["userid"]: format_confidence(face["confidence"])
28-
for face in predictions
29-
if not face["userid"] == "unknown"
49+
Get a summary of the objects detected.
50+
"""
51+
labels = get_object_labels(predictions)
52+
return {
53+
label: len(get_label_confidences(predictions, target_label=label))
54+
for label in labels
3055
}
31-
return matched_faces
3256

3357

3458
def post_image(url: str, image: bytes):
35-
"""Post an image to the classifier."""
59+
"""Post an image to Deepstack."""
3660
response = requests.post(url, files={"image": image}, timeout=TIMEOUT)
3761
return response
3862

3963

40-
class DeepstackFace:
41-
"""Work with faces."""
64+
class DeepstackObject:
65+
"""The object detection API locates and classifies 80
66+
different kinds of objects in a single image.."""
4267

4368
def __init__(self, ip_address: str, port: str):
4469

45-
self._url_check = "http://{}:{}/v1/vision/face/recognize".format(
70+
self._url_object_detection = "http://{}:{}/v1/vision/detection".format(
4671
ip_address, port
4772
)
48-
49-
self._faces = None
50-
self._matched = {}
51-
52-
def register_face(self, file_path: str, userid: str):
53-
"""Register a face with Deepstack."""
73+
self._predictions = []
5474

5575
def process_file(self, file_path: str):
5676
"""Process an image file."""
@@ -59,22 +79,14 @@ def process_file(self, file_path: str):
5979

6080
def process_image_bytes(self, image_bytes: bytes):
6181
"""Process an image."""
62-
response = post_image(self._url_check, image_bytes)
82+
self._predictions = []
83+
84+
response = post_image(self._url_object_detection, image_bytes)
6385
if response:
6486
if response.status_code == HTTP_OK:
65-
predictions_json = response.json()["predictions"]
66-
self._faces = len(predictions_json)
67-
self._matched = get_matched_faces(predictions_json)
68-
69-
else:
70-
self._faces = None
71-
self._matched = {}
87+
self._predictions = response.json()["predictions"]
7288

7389
@property
74-
def attributes(self):
90+
def predictions(self):
7591
"""Return the classifier attributes."""
76-
return {
77-
"faces": self._faces,
78-
"matched_faces": self._matched,
79-
"total_matched_faces": len(self._matched),
80-
}
92+
return self._predictions

tests/images/couple.jpg

-1.29 MB
Binary file not shown.

tests/images/idris.jpg

-205 KB
Binary file not shown.

tests/images/test-image3.jpg

202 KB
Loading

usage.ipynb

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

0 commit comments

Comments
 (0)