File size: 1,826 Bytes
b41c1b7
09bd5d6
 
 
 
 
b41c1b7
09bd5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
from PIL import Image
import numpy as np
import cv2
import os
import random

# Assuming overlay_image function is defined elsewhere in your code as provided previously

def process_image_with_model(image_path):
    # Load the Gradio model for closed eyes detection
    model = gr.load("models/dima806/closed_eyes_image_detection")
    
    # Load the user input image
    image = Image.open(image_path)
    image = image.rotate(-90, expand=True)
    image_array = np.array(image)
    
    # Predict using the Gradio model (adjust according to the model's input and output format)
    predictions = model(image_array)

    # Placeholder loop for overlaying images on detected regions (adjust based on actual predictions format)
    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)  # Load with alpha channel if present

        if random_eye_image is None:
            print(f"Failed to load image from {random_eye_image_path}")
            continue

        # Overlay the image and update image_array with the result
        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

# Setup the Gradio interface
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()