supArs's picture
Update main.py
44e15f3
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.Image(), outputs=gr.Image())
iface.launch()