3
3
"""
4
4
import requests
5
5
from PIL import Image
6
+ from typing import Union , List , Set , Dict
6
7
7
8
## Const
8
9
HTTP_OK = 200
9
10
HTTP_BAD_REQUEST = 400
10
11
HTTP_UNAUTHORIZED = 401
11
- TIMEOUT = 30 # seconds
12
+ TIMEOUT = 20 # seconds
12
13
13
14
14
- def format_confidence (confidence ) :
15
+ def format_confidence (confidence : Union [ str , float ]) -> float :
15
16
"""Takes a confidence from the API like
16
17
0.55623 and returne 55.6 (%).
17
18
"""
18
- return round (float (confidence )* 100 , 1 )
19
+ return round (float (confidence ) * 100 , 1 )
19
20
20
21
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.
22
40
"""
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 ]):
24
48
"""
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
30
55
}
31
- return matched_faces
32
56
33
57
34
58
def post_image (url : str , image : bytes ):
35
- """Post an image to the classifier ."""
59
+ """Post an image to Deepstack ."""
36
60
response = requests .post (url , files = {"image" : image }, timeout = TIMEOUT )
37
61
return response
38
62
39
63
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.."""
42
67
43
68
def __init__ (self , ip_address : str , port : str ):
44
69
45
- self ._url_check = "http://{}:{}/v1/vision/face/recognize " .format (
70
+ self ._url_object_detection = "http://{}:{}/v1/vision/detection " .format (
46
71
ip_address , port
47
72
)
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 = []
54
74
55
75
def process_file (self , file_path : str ):
56
76
"""Process an image file."""
@@ -59,22 +79,14 @@ def process_file(self, file_path: str):
59
79
60
80
def process_image_bytes (self , image_bytes : bytes ):
61
81
"""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 )
63
85
if response :
64
86
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" ]
72
88
73
89
@property
74
- def attributes (self ):
90
+ def predictions (self ):
75
91
"""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
0 commit comments