|
import gradio as gr |
|
from PIL import Image |
|
import numpy as np |
|
import cv2 |
|
import os |
|
import random |
|
|
|
|
|
|
|
def process_image_with_model(image_path): |
|
|
|
model = gr.load("models/dima806/closed_eyes_image_detection") |
|
|
|
|
|
image = Image.open(image_path) |
|
image = image.rotate(-90, expand=True) |
|
image_array = np.array(image) |
|
|
|
|
|
predictions = model(image_array) |
|
|
|
|
|
for bbox in predictions: |
|
x, y, w, h = bbox |
|
random_eye_image_path = os.path.join("result", random.choice(os.listdir("result"))) |
|
random_eye_image = cv2.imread(random_eye_image_path, cv2.IMREAD_UNCHANGED) |
|
|
|
if random_eye_image is None: |
|
print(f"Failed to load image from {random_eye_image_path}") |
|
continue |
|
|
|
|
|
image_array = overlay_image(image_array, random_eye_image, x, y, w, h, alpha=0.50) |
|
|
|
return Image.fromarray(image_array) |
|
|
|
def gr_interface(image): |
|
processed_image = process_image_with_model(image) |
|
return processed_image |
|
|
|
|
|
demo = gr.Interface(fn=gr_interface, |
|
inputs=gr.Image(type="filepath", label="Upload Image"), |
|
outputs="image", |
|
title="Closed Eyes Image Detection", |
|
description="Upload an image and the model will detect closed eyes.") |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|