Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
# Carica il modello locale (già caricato nei Files)
|
| 7 |
+
print("Caricamento modello...")
|
| 8 |
+
model = YOLO('best.pt')
|
| 9 |
+
print("Modello caricato con successo!")
|
| 10 |
+
|
| 11 |
+
def detect_balloons(image, confidence):
|
| 12 |
+
if image is None:
|
| 13 |
+
return None, {"error": "Nessuna immagine"}
|
| 14 |
+
|
| 15 |
+
print(f"Rilevamento con confidenza: {confidence}")
|
| 16 |
+
|
| 17 |
+
# Detection - rileva SOLO balloon
|
| 18 |
+
results = model(image, conf=confidence, verbose=False)
|
| 19 |
+
|
| 20 |
+
output = {
|
| 21 |
+
'num_balloons': 0,
|
| 22 |
+
'detections': []
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Controlla se ci sono detection
|
| 26 |
+
if results[0].boxes is not None and len(results[0].boxes) > 0:
|
| 27 |
+
output['num_balloons'] = len(results[0].boxes)
|
| 28 |
+
|
| 29 |
+
print(f"Trovati {len(results[0].boxes)} balloon")
|
| 30 |
+
|
| 31 |
+
for i in range(len(results[0].boxes)):
|
| 32 |
+
box = results[0].boxes.xyxy[i].cpu().numpy().tolist()
|
| 33 |
+
conf = float(results[0].boxes.conf[i].cpu().numpy())
|
| 34 |
+
|
| 35 |
+
detection_data = {
|
| 36 |
+
'box': {
|
| 37 |
+
'x1': int(box[0]),
|
| 38 |
+
'y1': int(box[1]),
|
| 39 |
+
'x2': int(box[2]),
|
| 40 |
+
'y2': int(box[3])
|
| 41 |
+
},
|
| 42 |
+
'confidence': round(conf, 3)
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Aggiungi mask se disponibile (segmentation)
|
| 46 |
+
if hasattr(results[0], 'masks') and results[0].masks is not None:
|
| 47 |
+
try:
|
| 48 |
+
mask = results[0].masks.data[i].cpu().numpy()
|
| 49 |
+
h, w = image.shape[:2]
|
| 50 |
+
mask_resized = cv2.resize(mask, (w, h))
|
| 51 |
+
# Converti in lista di coordinate dei pixel bianchi (più leggero del full array)
|
| 52 |
+
white_pixels = np.argwhere(mask_resized > 0.5).tolist()
|
| 53 |
+
detection_data['mask_pixels'] = white_pixels[:1000] # Limita a 1000 pixel per performance
|
| 54 |
+
detection_data['has_mask'] = True
|
| 55 |
+
except:
|
| 56 |
+
detection_data['has_mask'] = False
|
| 57 |
+
else:
|
| 58 |
+
detection_data['has_mask'] = False
|
| 59 |
+
|
| 60 |
+
output['detections'].append(detection_data)
|
| 61 |
+
else:
|
| 62 |
+
print("Nessun balloon rilevato - prova ad abbassare la confidenza")
|
| 63 |
+
|
| 64 |
+
# Disegna i risultati
|
| 65 |
+
annotated = results[0].plot()
|
| 66 |
+
|
| 67 |
+
return annotated, output
|
| 68 |
+
|
| 69 |
+
# Interface Gradio
|
| 70 |
+
with gr.Blocks(title="Balloon Detection") as demo:
|
| 71 |
+
gr.Markdown("# 🎈 Rilevamento Balloon nei Fumetti")
|
| 72 |
+
gr.Markdown("**Modello addestrato specificamente su balloon di fumetti** (manga, webtoon, western comics)")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
with gr.Column():
|
| 76 |
+
input_image = gr.Image(type="numpy", label="📷 Carica Fumetto")
|
| 77 |
+
confidence = gr.Slider(
|
| 78 |
+
minimum=0.1,
|
| 79 |
+
maximum=1.0,
|
| 80 |
+
value=0.25, # Più basso per rilevare più balloon
|
| 81 |
+
step=0.05,
|
| 82 |
+
label="🎯 Confidenza (più basso = più sensibile)"
|
| 83 |
+
)
|
| 84 |
+
detect_btn = gr.Button("🔍 Rileva Balloon", variant="primary", size="lg")
|
| 85 |
+
|
| 86 |
+
with gr.Column():
|
| 87 |
+
output_image = gr.Image(label="✅ Risultato")
|
| 88 |
+
output_json = gr.JSON(label="📊 Dati API")
|
| 89 |
+
|
| 90 |
+
gr.Markdown("""
|
| 91 |
+
### 💡 Suggerimenti:
|
| 92 |
+
- Se **non rileva balloon**: abbassa la confidenza a 0.15-0.20
|
| 93 |
+
- Se **rileva troppe cose**: alza la confidenza a 0.4-0.5
|
| 94 |
+
- Il modello è addestrato su 8000 immagini di fumetti vari
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
detect_btn.click(
|
| 98 |
+
fn=detect_balloons,
|
| 99 |
+
inputs=[input_image, confidence],
|
| 100 |
+
outputs=[output_image, output_json]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
print("App avviata!")
|
| 104 |
+
demo.launch()
|