bisoye commited on
Commit
09abb1f
1 Parent(s): 3dfac81

Update helper_fns.py

Browse files
Files changed (1) hide show
  1. helper_fns.py +36 -54
helper_fns.py CHANGED
@@ -1,54 +1,36 @@
1
- import io
2
- import cv2
3
- import time
4
- from PIL import Image
5
-
6
-
7
- # Function to convert PIL image to binary
8
- def pil_to_binary(img):
9
- with io.BytesIO() as output:
10
- img.save(output, format="PNG")
11
- return output.getvalue()
12
-
13
-
14
- def binary_to_pil(binary):
15
- return Image.open(io.BytesIO(binary))
16
-
17
-
18
- def crop_image(image_array, x, y, w, h):
19
- return image_array[y: y + h, x: x + w]
20
-
21
-
22
- def extract_faces(image_one, image_two, results):
23
- image_one_results = results['facial_areas']['img1']
24
- cropped_image_one = crop_image(image_one,
25
- image_one_results['x'],
26
- image_one_results['y'],
27
- image_one_results['w'],
28
- image_one_results['h'],)
29
-
30
- image_two_results = results['facial_areas']['img2']
31
- cropped_image_two = crop_image(image_two,
32
- image_two_results['x'],
33
- image_two_results['y'],
34
- image_two_results['w'],
35
- image_two_results['h'],)
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))