Skip to content

Commit 6f6aa17

Browse files
committed
Simplify
1 parent 39b3b8c commit 6f6aa17

File tree

3 files changed

+99
-61
lines changed

3 files changed

+99
-61
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "venv/bin/python3"
3+
}

deepstack/core.py

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""
22
Deepstack core.
33
"""
4-
import imghdr
54
import requests
65
from PIL import Image
76

87
## Const
9-
CLASSIFIER = "deepstack"
108
HTTP_OK = 200
119
HTTP_BAD_REQUEST = 400
1210
HTTP_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

4827
def 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

6133
class 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."""

usage.ipynb

Lines changed: 85 additions & 21 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)