supArs commited on
Commit
36512f8
1 Parent(s): 0d91dd9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -27
main.py CHANGED
@@ -1,39 +1,38 @@
1
  import gradio as gr
2
  import dlib
3
  import cv2
4
- import numpy as np
5
-
6
 
7
  # Load the pre-trained face detector and facial landmarks predictor
8
  detector = dlib.get_frontal_face_detector()
9
- predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # You'll need to download this file
10
 
11
  # Function to detect eyes in the image
12
  def detect_eyes(image):
13
-   image = cv2.imdecode(np.frombuffer(image.read(), np.uint8), -1)
14
-   gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
15
-
16
-   # Detect faces in the image
17
-   faces = detector(gray)
18
-
19
-   # Loop through detected faces
20
-   for face in faces:
21
-     landmarks = predictor(gray, face)
22
-
23
-     # Define regions of interest for left and right eyes
24
-     left_eye_region = [(36, 37, 38, 39, 40, 41)]
25
-     right_eye_region = [(42, 43, 44, 45, 46, 47)]
26
-
27
-     # Draw rectangles around eyes
28
-     for region in left_eye_region + right_eye_region:
29
-       for i in region:
30
-         x = landmarks.part(i).x
31
-         y = landmarks.part(i).y
32
-         cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
33
-
34
-   # Encode the image back to bytes
35
-   _, buffer = cv2.imencode('.jpg', image)
36
-   return buffer.tobytes()
37
 
38
  # Create a Gradio interface
39
  iface = gr.Interface(fn=detect_eyes)
 
1
  import gradio as gr
2
  import dlib
3
  import cv2
4
+ import numpy as np
 
5
 
6
  # Load the pre-trained face detector and facial landmarks predictor
7
  detector = dlib.get_frontal_face_detector()
8
+ predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # You'll need to download this file
9
 
10
  # Function to detect eyes in the image
11
  def detect_eyes(image):
12
+ image = cv2.imdecode(np.frombuffer(image.read(), np.uint8), -1)
13
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
14
+
15
+ # Detect faces in the image
16
+ faces = detector(gray)
17
+
18
+ # Loop through detected faces
19
+ for face in faces:
20
+ landmarks = predictor(gray, face)
21
+
22
+ # Define regions of interest for left and right eyes
23
+ left_eye_region = [(36, 37, 38, 39, 40, 41)]
24
+ right_eye_region = [(42, 43, 44, 45, 46, 47)]
25
+
26
+ # Draw rectangles around eyes
27
+ for region in left_eye_region + right_eye_region:
28
+ for i in region:
29
+ x = landmarks.part(i).x
30
+ y = landmarks.part(i).y
31
+ cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
32
+
33
+ # Encode the image back to bytes
34
+ _, buffer = cv2.imencode('.jpg', image)
35
+ return buffer.tobytes()
36
 
37
  # Create a Gradio interface
38
  iface = gr.Interface(fn=detect_eyes)