Ronny56 commited on
Commit
64e5215
·
verified ·
1 Parent(s): e3f0630

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -47
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
- # Carica il modello YOLOv8 segmentation
8
- model = YOLO('yolov8n-seg.pt') # Usa il modello nano per velocità su CPU
 
9
 
10
- def detect_balloons(image, confidence=0.5):
11
- """
12
- Rileva balloon nell'immagine e restituisce le mask
13
- """
14
- # Esegui prediction
15
  results = model(image, conf=confidence)
16
 
17
- # Estrai risultati
18
  output = {
19
- 'masks': [],
20
- 'boxes': [],
21
- 'polygons': []
22
  }
23
 
24
- if results[0].masks is not None:
25
- for i, mask in enumerate(results[0].masks):
26
- # Mask bitmap (array 2D)
27
- mask_array = mask.data[0].cpu().numpy()
28
- output['masks'].append(mask_array.tolist())
 
29
 
30
- # Bounding box
31
- box = results[0].boxes.xyxy[i].cpu().numpy()
32
- output['boxes'].append(box.tolist())
 
 
 
 
 
33
 
34
- # Poligono (coordinate x,y)
35
- polygon = mask.xy[0]
36
- output['polygons'].append(polygon.tolist())
37
-
38
- # Disegna i risultati sull'immagine
39
- annotated_image = results[0].plot()
 
 
 
 
40
 
41
- return annotated_image, output
 
 
42
 
43
- # Crea interfaccia Gradio
44
- with gr.Blocks() as demo:
45
- gr.Markdown("# YOLOv8 Balloon Segmentation")
46
- gr.Markdown("Carica un'immagine di fumetto per rilevare automaticamente i balloon")
47
-
48
- with gr.Row():
49
- with gr.Column():
50
- input_image = gr.Image(type="pil", label="Immagine Input")
51
- confidence_slider = gr.Slider(0.1, 1.0, value=0.5, label="Confidence Threshold")
52
- detect_btn = gr.Button("Rileva Balloon")
53
-
54
- with gr.Column():
55
- output_image = gr.Image(label="Risultato")
56
- output_json = gr.JSON(label="Dati Segmentazione (masks, boxes, polygons)")
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()