Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
def detect_balloons(image, confidence
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
results = model(image, conf=confidence)
|
| 16 |
|
| 17 |
-
# Estrai risultati
|
| 18 |
output = {
|
| 19 |
-
'
|
| 20 |
-
'
|
| 21 |
-
'polygons': []
|
| 22 |
}
|
| 23 |
|
| 24 |
-
if results[0].
|
| 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 |
-
detect_btn.click(
|
| 59 |
-
fn=detect_balloons,
|
| 60 |
-
inputs=[input_image, confidence_slider],
|
| 61 |
-
outputs=[output_image, output_json]
|
| 62 |
-
)
|
| 63 |
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import cv2
|
| 5 |
|
| 6 |
+
# Scarica e carica il modello specifico per balloon
|
| 7 |
+
# Questo modello è addestrato su 8000 immagini di fumetti
|
| 8 |
+
model = YOLO('https://huggingface.co/ogkalu/comic-speech-bubble-detector-yolov8m/resolve/main/best.pt')
|
| 9 |
|
| 10 |
+
def detect_balloons(image, confidence):
|
| 11 |
+
if image is None:
|
| 12 |
+
return None, {"error": "Nessuna immagine"}
|
| 13 |
+
|
| 14 |
+
# Detection - ora rileva SOLO balloon, non persone
|
| 15 |
results = model(image, conf=confidence)
|
| 16 |
|
|
|
|
| 17 |
output = {
|
| 18 |
+
'num_balloons': 0,
|
| 19 |
+
'detections': []
|
|
|
|
| 20 |
}
|
| 21 |
|
| 22 |
+
if results[0].boxes is not None and len(results[0].boxes) > 0:
|
| 23 |
+
output['num_balloons'] = len(results[0].boxes)
|
| 24 |
+
|
| 25 |
+
for i in range(len(results[0].boxes)):
|
| 26 |
+
box = results[0].boxes.xyxy[i].cpu().numpy().tolist()
|
| 27 |
+
conf = float(results[0].boxes.conf[i].cpu().numpy())
|
| 28 |
|
| 29 |
+
# Se il modello ha le mask (segmentation)
|
| 30 |
+
if results[0].masks is not None:
|
| 31 |
+
mask = results[0].masks.data[i].cpu().numpy()
|
| 32 |
+
h, w = image.shape[:2]
|
| 33 |
+
mask_resized = cv2.resize(mask, (w, h))
|
| 34 |
+
mask_list = (mask_resized * 255).astype(np.uint8).tolist()
|
| 35 |
+
else:
|
| 36 |
+
mask_list = None
|
| 37 |
|
| 38 |
+
output['detections'].append({
|
| 39 |
+
'box': {
|
| 40 |
+
'x1': box[0],
|
| 41 |
+
'y1': box[1],
|
| 42 |
+
'x2': box[2],
|
| 43 |
+
'y2': box[3]
|
| 44 |
+
},
|
| 45 |
+
'mask': mask_list,
|
| 46 |
+
'confidence': conf
|
| 47 |
+
})
|
| 48 |
|
| 49 |
+
# Disegna i risultati
|
| 50 |
+
annotated = results[0].plot()
|
| 51 |
+
return annotated, output
|
| 52 |
|
| 53 |
+
# Interface Gradio
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=detect_balloons,
|
| 56 |
+
inputs=[
|
| 57 |
+
gr.Image(type="numpy", label="📷 Immagine Fumetto"),
|
| 58 |
+
gr.Slider(0.1, 1.0, value=0.3, label="🎯 Confidenza (più basso = più balloon)")
|
| 59 |
+
],
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.Image(label="✅ Balloon Rilevati"),
|
| 62 |
+
gr.JSON(label="📊 Dati JSON")
|
| 63 |
+
],
|
| 64 |
+
title="🎈 Balloon Detection - Solo Nuvolette",
|
| 65 |
+
description="Rileva SOLO i balloon (nuvolette di dialogo) nei fumetti, non le persone"
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
demo.launch()
|