1
1
"""
2
2
Deepstack core.
3
3
"""
4
- import imghdr
5
4
import requests
6
5
from PIL import Image
7
6
8
7
## Const
9
- CLASSIFIER = "deepstack"
10
8
HTTP_OK = 200
11
9
HTTP_BAD_REQUEST = 400
12
10
HTTP_UNAUTHORIZED = 401
@@ -17,45 +15,19 @@ def get_matched_faces(predictions: dict):
17
15
"""
18
16
Get the predicted faces and their confidence.
19
17
"""
20
- try :
21
- matched_faces = {
22
- face ["userid" ]: round (face ["confidence" ] * 100 , 1 )
23
- for face in predictions
24
- if not face ["userid" ] == "unknown"
25
- }
26
- return matched_faces
27
- except :
28
- return {}
29
-
30
-
31
- def is_valid_image (file_path : str ):
32
- """
33
- Check file_path is valid image, using PIL then imghdr.
34
- """
35
- try :
36
- with Image .open (file_path ):
37
- pass
38
-
39
- image_extension = imghdr .what (file_path )
40
- if image_extension in ["jpeg" , ".jpg" , ".png" ]:
41
- return True
42
- return False
43
- except Exception as exc :
44
- print (exc )
45
- return False
18
+ matched_faces = {}
19
+ matched_faces = {
20
+ face ["userid" ]: round (face ["confidence" ] * 100 , 1 )
21
+ for face in predictions
22
+ if not face ["userid" ] == "unknown"
23
+ }
24
+ return matched_faces
46
25
47
26
48
27
def post_image (url : str , image : bytes ):
49
28
"""Post an image to the classifier."""
50
- try :
51
- response = requests .post (url , files = {"image" : image }, timeout = TIMEOUT )
52
- return response
53
- except requests .exceptions .ConnectionError :
54
- print ("ConnectionError: Is %s running?" , CLASSIFIER )
55
- return None
56
- except requests .exceptions .Timeout :
57
- print ("Timeout error from %s" , CLASSIFIER )
58
- return None
29
+ response = requests .post (url , files = {"image" : image }, timeout = TIMEOUT )
30
+ return response
59
31
60
32
61
33
class DeepstackFace :
@@ -72,9 +44,8 @@ def __init__(self, ip_address: str, port: str):
72
44
73
45
def process_file (self , file_path : str ):
74
46
"""Process an image file."""
75
- if is_valid_image (file_path ):
76
- with open (file_path , "rb" ) as image_bytes :
77
- self .process_image_bytes (image_bytes )
47
+ with open (file_path , "rb" ) as image_bytes :
48
+ self .process_image_bytes (image_bytes )
78
49
79
50
def process_image_bytes (self , image_bytes : bytes ):
80
51
"""Process an image."""
0 commit comments