Spaces:
Sleeping
Sleeping
File size: 1,289 Bytes
d4a843c |
1 2 3 4 5 6 7 8 9 10 11 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 39 |
import gradio as gr
import dlib
import cv2
import numpy as np
# Load the pre-trained face detector and facial landmarks predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # You'll need to download this file
# Function to detect eyes in the image
def detect_eyes(image):
image = cv2.imdecode(np.frombuffer(image.read(), np.uint8), -1)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = detector(gray)
# Loop through detected faces
for face in faces:
landmarks = predictor(gray, face)
# Define regions of interest for left and right eyes
left_eye_region = [(36, 37, 38, 39, 40, 41)]
right_eye_region = [(42, 43, 44, 45, 46, 47)]
# Draw rectangles around eyes
for region in left_eye_region + right_eye_region:
for i in region:
x = landmarks.part(i).x
y = landmarks.part(i).y
cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
# Encode the image back to bytes
_, buffer = cv2.imencode('.jpg', image)
return buffer.tobytes()
iface = gr.Interface(fn=detect_eyes, inputs=gr.inputs.Imagebox(), outputs=gr.outputs.Imagebox())
iface.launch()
|