File size: 2,223 Bytes
e3f0630
 
 
 
 
64e5215
 
 
e3f0630
64e5215
 
 
 
 
e3f0630
 
 
64e5215
 
e3f0630
 
64e5215
 
 
 
 
 
e3f0630
64e5215
 
 
 
 
 
 
 
e3f0630
64e5215
 
 
 
 
 
 
 
 
 
e3f0630
64e5215
 
 
e3f0630
64e5215
 
 
 
 
 
 
 
 
 
 
 
 
 
e3f0630
 
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
65
66
67
68
69
import gradio as gr
from ultralytics import YOLO
import numpy as np
import cv2

# Scarica e carica il modello specifico per balloon
# Questo modello è addestrato su 8000 immagini di fumetti
model = YOLO('https://huggingface.co/ogkalu/comic-speech-bubble-detector-yolov8m/resolve/main/best.pt')

def detect_balloons(image, confidence):
    if image is None:
        return None, {"error": "Nessuna immagine"}
    
    # Detection - ora rileva SOLO balloon, non persone
    results = model(image, conf=confidence)
    
    output = {
        'num_balloons': 0,
        'detections': []
    }
    
    if results[0].boxes is not None and len(results[0].boxes) > 0:
        output['num_balloons'] = len(results[0].boxes)
        
        for i in range(len(results[0].boxes)):
            box = results[0].boxes.xyxy[i].cpu().numpy().tolist()
            conf = float(results[0].boxes.conf[i].cpu().numpy())
            
            # Se il modello ha le mask (segmentation)
            if results[0].masks is not None:
                mask = results[0].masks.data[i].cpu().numpy()
                h, w = image.shape[:2]
                mask_resized = cv2.resize(mask, (w, h))
                mask_list = (mask_resized * 255).astype(np.uint8).tolist()
            else:
                mask_list = None
            
            output['detections'].append({
                'box': {
                    'x1': box[0],
                    'y1': box[1],
                    'x2': box[2],
                    'y2': box[3]
                },
                'mask': mask_list,
                'confidence': conf
            })
    
    # Disegna i risultati
    annotated = results[0].plot()
    return annotated, output

# Interface Gradio
demo = gr.Interface(
    fn=detect_balloons,
    inputs=[
        gr.Image(type="numpy", label="📷 Immagine Fumetto"),
        gr.Slider(0.1, 1.0, value=0.3, label="🎯 Confidenza (più basso = più balloon)")
    ],
    outputs=[
        gr.Image(label="✅ Balloon Rilevati"),
        gr.JSON(label="📊 Dati JSON")
    ],
    title="🎈 Balloon Detection - Solo Nuvolette",
    description="Rileva SOLO i balloon (nuvolette di dialogo) nei fumetti, non le persone"
)

demo.launch()