11"""
22Deepstack core.
33"""
4- import imghdr
54import requests
65from PIL import Image
76
87## Const
9- CLASSIFIER = "deepstack"
108HTTP_OK = 200
119HTTP_BAD_REQUEST = 400
1210HTTP_UNAUTHORIZED = 401
@@ -17,45 +15,19 @@ def get_matched_faces(predictions: dict):
1715 """
1816 Get the predicted faces and their confidence.
1917 """
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
4625
4726
4827def post_image (url : str , image : bytes ):
4928 """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
5931
6032
6133class DeepstackFace :
@@ -72,9 +44,8 @@ def __init__(self, ip_address: str, port: str):
7244
7345 def process_file (self , file_path : str ):
7446 """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 )
7849
7950 def process_image_bytes (self , image_bytes : bytes ):
8051 """Process an image."""
0 commit comments