Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,49 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
+
# Assuming overlay_image function is defined elsewhere in your code as provided previously
|
| 9 |
+
|
| 10 |
+
def process_image_with_model(image_path):
|
| 11 |
+
# Load the Gradio model for closed eyes detection
|
| 12 |
+
model = gr.load("models/dima806/closed_eyes_image_detection")
|
| 13 |
+
|
| 14 |
+
# Load the user input image
|
| 15 |
+
image = Image.open(image_path)
|
| 16 |
+
image = image.rotate(-90, expand=True)
|
| 17 |
+
image_array = np.array(image)
|
| 18 |
+
|
| 19 |
+
# Predict using the Gradio model (adjust according to the model's input and output format)
|
| 20 |
+
predictions = model(image_array)
|
| 21 |
+
|
| 22 |
+
# Placeholder loop for overlaying images on detected regions (adjust based on actual predictions format)
|
| 23 |
+
for bbox in predictions:
|
| 24 |
+
x, y, w, h = bbox
|
| 25 |
+
random_eye_image_path = os.path.join("result", random.choice(os.listdir("result")))
|
| 26 |
+
random_eye_image = cv2.imread(random_eye_image_path, cv2.IMREAD_UNCHANGED) # Load with alpha channel if present
|
| 27 |
+
|
| 28 |
+
if random_eye_image is None:
|
| 29 |
+
print(f"Failed to load image from {random_eye_image_path}")
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
# Overlay the image and update image_array with the result
|
| 33 |
+
image_array = overlay_image(image_array, random_eye_image, x, y, w, h, alpha=0.50)
|
| 34 |
+
|
| 35 |
+
return Image.fromarray(image_array)
|
| 36 |
+
|
| 37 |
+
def gr_interface(image):
|
| 38 |
+
processed_image = process_image_with_model(image)
|
| 39 |
+
return processed_image
|
| 40 |
+
|
| 41 |
+
# Setup the Gradio interface
|
| 42 |
+
demo = gr.Interface(fn=gr_interface,
|
| 43 |
+
inputs=gr.Image(type="filepath", label="Upload Image"),
|
| 44 |
+
outputs="image",
|
| 45 |
+
title="Closed Eyes Image Detection",
|
| 46 |
+
description="Upload an image and the model will detect closed eyes.")
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|