Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image, ImageOps, ImageEnhance
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
# Load your model
|
| 8 |
+
model = YOLO("model/best.pt")
|
| 9 |
+
|
| 10 |
+
def preprocess(image):
|
| 11 |
+
"""Safe preprocessing for PIL or numpy input."""
|
| 12 |
+
if isinstance(image, np.ndarray):
|
| 13 |
+
image = Image.fromarray(image)
|
| 14 |
+
|
| 15 |
+
image = ImageOps.exif_transpose(image).convert("RGB")
|
| 16 |
+
|
| 17 |
+
# Optional resize for performance
|
| 18 |
+
w, h = image.size
|
| 19 |
+
max_dim = max(w, h)
|
| 20 |
+
if max_dim > 1024:
|
| 21 |
+
scale = 1024 / max_dim
|
| 22 |
+
image = image.resize((int(w * scale), int(h * scale)), Image.LANCZOS)
|
| 23 |
+
|
| 24 |
+
# Light contrast enhancement
|
| 25 |
+
image = ImageEnhance.Contrast(image).enhance(1.05)
|
| 26 |
+
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def detect(image, conf=0.5, iou=0.5):
|
| 31 |
+
"""Run YOLO detection on a single model Space."""
|
| 32 |
+
image = preprocess(image)
|
| 33 |
+
|
| 34 |
+
results = model.predict(image, conf=conf, iou=iou)
|
| 35 |
+
boxes = results[0].boxes
|
| 36 |
+
|
| 37 |
+
# Convert YOLO output to numpy RGB
|
| 38 |
+
output = results[0].plot()[:, :, ::-1] # BGR → RGB
|
| 39 |
+
|
| 40 |
+
if len(boxes) > 0:
|
| 41 |
+
diagnosis = "⚠️ Redness detected."
|
| 42 |
+
else:
|
| 43 |
+
diagnosis = "🟢 No redness detected."
|
| 44 |
+
|
| 45 |
+
return [output, diagnosis]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=detect,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 53 |
+
gr.Slider(0, 1, value=0.5, step=0.05, label="Confidence Threshold"),
|
| 54 |
+
gr.Slider(0, 1, value=0.5, step=0.05, label="NMS IoU Threshold"),
|
| 55 |
+
],
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Image(label="Redness Detection Result"),
|
| 58 |
+
gr.Textbox(label="Diagnosis")
|
| 59 |
+
],
|
| 60 |
+
title="Redness Detection"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
interface.launch()
|