Skip to content

Commit 21c1c9a

Browse files
committed
Adds usage
1 parent c14412f commit 21c1c9a

File tree

5 files changed

+171
-9
lines changed

5 files changed

+171
-9
lines changed
191 Bytes
Binary file not shown.
2.9 KB
Binary file not shown.

deepstack/core.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Deepstack cpre.
3+
"""
4+
import imghdr
5+
import requests
6+
from PIL import Image
7+
8+
## Const
9+
CLASSIFIER = 'deepstack'
10+
HTTP_OK = 200
11+
HTTP_BAD_REQUEST = 400
12+
HTTP_UNAUTHORIZED = 401
13+
TIMEOUT = 30 # seconds
14+
15+
16+
def get_matched_faces(predictions: dict):
17+
"""
18+
Get the predicted faces and their confidence.
19+
"""
20+
try:
21+
matched_faces = {face['userid']: round(face['confidence']*100, 1)
22+
for face in predictions if not face['userid'] == 'unknown'}
23+
return matched_faces
24+
except:
25+
return {}
26+
27+
28+
def is_valid_image(file_path: str):
29+
"""
30+
Check file_path is valid image, using PIL then imghdr.
31+
"""
32+
try:
33+
with Image.open(file_path):
34+
pass
35+
36+
image_extension = imghdr.what(file_path)
37+
if image_extension in ['jpeg', '.jpg', '.png']:
38+
return True
39+
return False
40+
except Exception as exc:
41+
print(exc)
42+
return False
43+
44+
45+
def post_image(url: str, image: bytes):
46+
"""Post an image to the classifier."""
47+
try:
48+
response = requests.post(
49+
url,
50+
files={"image": image},
51+
timeout=TIMEOUT
52+
)
53+
return response
54+
except requests.exceptions.ConnectionError:
55+
print("ConnectionError: Is %s running?", CLASSIFIER)
56+
return None
57+
except requests.exceptions.Timeout:
58+
print("Timeout error from %s", CLASSIFIER)
59+
return None
60+
61+
62+
class DeepstackFace():
63+
"""Work with faces."""
64+
65+
def __init__(self, ip_address: str, port: str):
66+
67+
self._url_check = "http://{}:{}/v1/vision/face/recognize".format(
68+
ip_address, port)
69+
70+
self._faces = None
71+
self._matched = {}
72+
73+
def process_file(self, file_path: str):
74+
"""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)
78+
79+
def process_image_bytes(self, image_bytes: bytes):
80+
"""Process an image."""
81+
response = post_image(
82+
self._url_check, image_bytes)
83+
if response:
84+
if response.status_code == HTTP_OK:
85+
predictions_json = response.json()["predictions"]
86+
self._faces = len(predictions_json)
87+
self._matched = get_matched_faces(predictions_json)
88+
89+
else:
90+
self._faces = None
91+
self._matched = {}
92+
93+
@property
94+
def attributes(self):
95+
"""Return the classifier attributes."""
96+
return {
97+
'faces': self._faces,
98+
'matched_faces': self._matched,
99+
'total_matched_faces': len(self._matched),
100+
}

development/development.ipynb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
},
114114
{
115115
"cell_type": "code",
116-
"execution_count": 6,
116+
"execution_count": 13,
117117
"metadata": {},
118118
"outputs": [],
119119
"source": [
@@ -131,7 +131,7 @@
131131
" def process_file(self, file_path : str):\n",
132132
" \"\"\"Process an image file.\"\"\"\n",
133133
" if is_valid_image(file_path):\n",
134-
" with open(image_path, \"rb\") as image_bytes:\n",
134+
" with open(file_path, \"rb\") as image_bytes:\n",
135135
" self.process_image_bytes(image_bytes)\n",
136136
" \n",
137137
" def process_image_bytes(self, image_bytes : bytes):\n",
@@ -167,7 +167,7 @@
167167
},
168168
{
169169
"cell_type": "code",
170-
"execution_count": 7,
170+
"execution_count": 14,
171171
"metadata": {},
172172
"outputs": [],
173173
"source": [
@@ -176,7 +176,7 @@
176176
},
177177
{
178178
"cell_type": "code",
179-
"execution_count": 8,
179+
"execution_count": 15,
180180
"metadata": {},
181181
"outputs": [
182182
{
@@ -185,7 +185,7 @@
185185
"{'faces': None, 'matched_faces': {}, 'total_matched_faces': 0}"
186186
]
187187
},
188-
"execution_count": 8,
188+
"execution_count": 15,
189189
"metadata": {},
190190
"output_type": "execute_result"
191191
}
@@ -196,16 +196,17 @@
196196
},
197197
{
198198
"cell_type": "code",
199-
"execution_count": 9,
199+
"execution_count": 16,
200200
"metadata": {},
201201
"outputs": [
202202
{
203203
"name": "stdout",
204204
"output_type": "stream",
205205
"text": [
206-
"{'faces': 2, 'matched_faces': {}, 'total_matched_faces': 0}\n",
207-
"CPU times: user 368 ms, sys: 95.9 ms, total: 464 ms\n",
208-
"Wall time: 17.3 s\n"
206+
"Timeout error from %s deepstack_face\n",
207+
"{'faces': None, 'matched_faces': {}, 'total_matched_faces': 0}\n",
208+
"CPU times: user 393 ms, sys: 91.4 ms, total: 485 ms\n",
209+
"Wall time: 20.6 s\n"
209210
]
210211
},
211212
{

usage.ipynb

Lines changed: 61 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)