Spaces:
Sleeping
Sleeping
Delete utils.py
Browse files
utils.py
DELETED
@@ -1,116 +0,0 @@
|
|
1 |
-
import base64
|
2 |
-
import os
|
3 |
-
import requests
|
4 |
-
|
5 |
-
import cv2
|
6 |
-
import numpy as np
|
7 |
-
from PIL import Image
|
8 |
-
|
9 |
-
from .exceptions import InvalidImage
|
10 |
-
from .emotionsmultilanguage import emotions_dict
|
11 |
-
|
12 |
-
|
13 |
-
def draw_annotations(
|
14 |
-
frame: np.ndarray,
|
15 |
-
faces: list,
|
16 |
-
boxes=True,
|
17 |
-
scores=True,
|
18 |
-
color: tuple = (0, 155, 255),
|
19 |
-
lang: str = "en",
|
20 |
-
size_multiplier: int = 1,
|
21 |
-
) -> np.ndarray:
|
22 |
-
"""Draws boxes around detected faces. Faces is a list of dicts with `box` and `emotions`."""
|
23 |
-
if not len(faces):
|
24 |
-
return frame
|
25 |
-
|
26 |
-
for face in faces:
|
27 |
-
x, y, w, h = face["box"]
|
28 |
-
emotions = face["emotions"]
|
29 |
-
|
30 |
-
if boxes:
|
31 |
-
cv2.rectangle(
|
32 |
-
frame,
|
33 |
-
(x, y, w, h),
|
34 |
-
color,
|
35 |
-
2,
|
36 |
-
)
|
37 |
-
|
38 |
-
if scores:
|
39 |
-
frame = draw_scores(frame, emotions, (x, y, w, h), lang, size_multiplier)
|
40 |
-
return frame
|
41 |
-
|
42 |
-
|
43 |
-
def loadBase64Img(uri):
|
44 |
-
encoded_data = uri.split(",")[1]
|
45 |
-
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
|
46 |
-
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
47 |
-
return img
|
48 |
-
|
49 |
-
|
50 |
-
def pil_to_bgr(pil_image):
|
51 |
-
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
52 |
-
|
53 |
-
|
54 |
-
def load_image(img):
|
55 |
-
"""Modified from github.com/serengil/deepface. Returns bgr (opencv-style) numpy array."""
|
56 |
-
is_exact_image = is_base64_img = is_url_img = False
|
57 |
-
|
58 |
-
if type(img).__module__ == np.__name__:
|
59 |
-
is_exact_image = True
|
60 |
-
elif img is None:
|
61 |
-
raise InvalidImage("Image not valid.")
|
62 |
-
elif len(img) > 11 and img[0:11] == "data:image/":
|
63 |
-
is_base64_img = True
|
64 |
-
elif len(img) > 11 and img.startswith("http"):
|
65 |
-
is_url_img = True
|
66 |
-
|
67 |
-
if is_base64_img:
|
68 |
-
img = loadBase64Img(img)
|
69 |
-
elif is_url_img:
|
70 |
-
img = pil_to_bgr(Image.open(requests.get(img, stream=True).raw))
|
71 |
-
elif not is_exact_image: # image path passed as input
|
72 |
-
if not os.path.isfile(img):
|
73 |
-
raise ValueError(f"Confirm that {img} exists")
|
74 |
-
img = cv2.imread(img)
|
75 |
-
|
76 |
-
if img is None or not hasattr(img, "shape"):
|
77 |
-
raise InvalidImage("Image not valid.")
|
78 |
-
|
79 |
-
return img
|
80 |
-
|
81 |
-
|
82 |
-
def draw_scores(
|
83 |
-
frame: np.ndarray,
|
84 |
-
emotions: dict,
|
85 |
-
bounding_box: dict,
|
86 |
-
lang: str = "en",
|
87 |
-
size_multiplier: int = 1,
|
88 |
-
) -> np.ndarray:
|
89 |
-
"""Draw scores for each emotion under faces."""
|
90 |
-
GRAY = (211, 211, 211)
|
91 |
-
GREEN = (0, 255, 0)
|
92 |
-
x, y, w, h = bounding_box
|
93 |
-
|
94 |
-
for idx, (emotion, score) in enumerate(emotions.items()):
|
95 |
-
color = GRAY if score < 0.01 else GREEN
|
96 |
-
|
97 |
-
if lang != "en":
|
98 |
-
emotion = emotions_dict[emotion][lang]
|
99 |
-
|
100 |
-
emotion_score = "{}: {}".format(
|
101 |
-
emotion, "{:.2f}".format(score) if score >= 0.01 else ""
|
102 |
-
)
|
103 |
-
cv2.putText(
|
104 |
-
frame,
|
105 |
-
emotion_score,
|
106 |
-
(
|
107 |
-
x,
|
108 |
-
y + h + (15 * size_multiplier) + idx * (15 * size_multiplier),
|
109 |
-
),
|
110 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
111 |
-
0.5 * size_multiplier,
|
112 |
-
color,
|
113 |
-
1 * size_multiplier,
|
114 |
-
cv2.LINE_AA,
|
115 |
-
)
|
116 |
-
return frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|