Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,38 +1,47 @@
|
|
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")
|
9 |
|
10 |
# Function to detect eyes in the image
|
11 |
def detect_eyes(image):
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
iface.launch()
|
|
|
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)
|
39 |
+
|
40 |
+
# Add an image input to the interface
|
41 |
+
iface.add_input("Image", gr.inputs.Image())
|
42 |
+
|
43 |
+
# Add an image output to the interface
|
44 |
+
iface.add_output("Detected Eyes", gr.outputs.Image())
|
45 |
+
|
46 |
+
# Launch the interface
|
47 |
iface.launch()
|