File size: 1,636 Bytes
94a098d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdfb1fb
94a098d
fdfb1fb
94a098d
 
 
 
 
 
 
 
 
fdfb1fb
94a098d
 
 
fdfb1fb
94a098d
 
fdfb1fb
94a098d
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
from ultralytics import YOLO
from PIL import Image, ImageOps, ImageEnhance
import numpy as np
import tempfile

# Load your model
model = YOLO("model/best.pt")

def preprocess(image):
    """Safe preprocessing for PIL or numpy input."""
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)

    image = ImageOps.exif_transpose(image).convert("RGB")

    # Optional resize for performance
    w, h = image.size
    max_dim = max(w, h)
    if max_dim > 1024:
        scale = 1024 / max_dim
        image = image.resize((int(w * scale), int(h * scale)), Image.LANCZOS)

    # Light contrast enhancement
    image = ImageEnhance.Contrast(image).enhance(1.05)

    return image


def detect(image, conf=0.4, iou=0.5):
    """Run YOLO detection on a single model Space."""
    image = preprocess(image)

    results = model.predict(image, conf=conf, iou=iou)
    boxes = results[0].boxes

    # Convert YOLO output to numpy RGB
    output = results[0].plot()[:, :, ::-1]  # BGR → RGB

    if len(boxes) > 0:
        diagnosis = "⚠️ Swelling detected."
    else:
        diagnosis = "🟢 No swelling detected."

    return [output, diagnosis]


# Gradio Interface
interface = gr.Interface(
    fn=detect,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Slider(0, 1, value=0.5, step=0.05, label="Confidence Threshold"),
        gr.Slider(0, 1, value=0.5, step=0.05, label="NMS IoU Threshold"),
    ],
    outputs=[
        gr.Image(label="Swelling Detection Result"),
        gr.Textbox(label="Diagnosis")
    ],
    title="Swelling Detection"
)

interface.launch()