Spaces:
Sleeping
Sleeping
Update helper_fns.py
Browse files- helper_fns.py +36 -54
helper_fns.py
CHANGED
@@ -1,54 +1,36 @@
|
|
1 |
-
import io
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
image_one_results['
|
26 |
-
image_one_results['
|
27 |
-
image_one_results['
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
image_two_results['
|
33 |
-
image_two_results['
|
34 |
-
image_two_results['
|
35 |
-
|
36 |
-
|
37 |
-
return Image.fromarray(cropped_image_one).resize((480, 480)), Image.fromarray(cropped_image_two).resize((480, 480))
|
38 |
-
|
39 |
-
def read_image_from_webcam():
|
40 |
-
#initialize webcam
|
41 |
-
cap = cv2.VideoCapture(0)
|
42 |
-
|
43 |
-
if not cap.isOpened():
|
44 |
-
print(f'Error could not open webcam')
|
45 |
-
return None
|
46 |
-
else:
|
47 |
-
print(f'Webcam opened successfully!')
|
48 |
-
#wait for 5 seconds
|
49 |
-
time.sleep(5)
|
50 |
-
_, frame = cap.read()
|
51 |
-
#Image.fromarray
|
52 |
-
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
53 |
-
cap.release()
|
54 |
-
return rgb_frame
|
|
|
1 |
+
import io
|
2 |
+
import time
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
|
6 |
+
# Function to convert PIL image to binary
|
7 |
+
def pil_to_binary(img):
|
8 |
+
with io.BytesIO() as output:
|
9 |
+
img.save(output, format="PNG")
|
10 |
+
return output.getvalue()
|
11 |
+
|
12 |
+
|
13 |
+
def binary_to_pil(binary):
|
14 |
+
return Image.open(io.BytesIO(binary))
|
15 |
+
|
16 |
+
|
17 |
+
def crop_image(image_array, x, y, w, h):
|
18 |
+
return image_array[y: y + h, x: x + w]
|
19 |
+
|
20 |
+
|
21 |
+
def extract_faces(image_one, image_two, results):
|
22 |
+
image_one_results = results['facial_areas']['img1']
|
23 |
+
cropped_image_one = crop_image(image_one,
|
24 |
+
image_one_results['x'],
|
25 |
+
image_one_results['y'],
|
26 |
+
image_one_results['w'],
|
27 |
+
image_one_results['h'],)
|
28 |
+
|
29 |
+
image_two_results = results['facial_areas']['img2']
|
30 |
+
cropped_image_two = crop_image(image_two,
|
31 |
+
image_two_results['x'],
|
32 |
+
image_two_results['y'],
|
33 |
+
image_two_results['w'],
|
34 |
+
image_two_results['h'],)
|
35 |
+
|
36 |
+
return Image.fromarray(cropped_image_one).resize((480, 480)), Image.fromarray(cropped_image_two).resize((480, 480))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|