colomboMk commited on
Commit
ecc68fd
·
verified ·
1 Parent(s): 2057d56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -1,6 +1,3 @@
1
-
2
- #"/home/mclm/phd/best.pt"
3
-
4
  import os
5
  import cv2
6
  import numpy as np
@@ -80,7 +77,7 @@ def _draw_boxes_rgb(image_rgb: np.ndarray, result, target_class: str):
80
 
81
  color_bgr = (0, 0, 255) if is_target else (0, 200, 0) # rosso per target, verde per altre
82
  cv2.rectangle(vis_bgr, (x1, y1), (x2, y2), color_bgr, 2)
83
- # Niente label testuali
84
 
85
  total_count += 1
86
  if is_target:
@@ -99,7 +96,7 @@ def infer_single_image(
99
  slice_w: int = 640,
100
  overlap_h: float = 0.10,
101
  overlap_w: float = 0.10,
102
- device: str = "cuda:0",
103
  target_class: str = "berry",
104
  ):
105
  """
@@ -107,9 +104,7 @@ def infer_single_image(
107
  con pesi YOLOv11 di instance segmentation ma trattati come detection:
108
  - SAHI usa AutoDetectionModel 'yolov8' (wrapper Ultralytics detection)
109
  - Le mask sono ignorate, si usano i bounding box (come da .boxes)
110
- - Si disegnano solo le bbox (senza label) e si riporta il conteggio per la classe target
111
-
112
- Ritorna: (immagine_annotata_RGB, testo_contatori)
113
  """
114
  if image is None:
115
  raise gr.Error("Devi caricare un'immagine.")
@@ -118,16 +113,23 @@ def infer_single_image(
118
  raise gr.Error(f"File pesi non trovato: {weights_path}")
119
 
120
  image_rgb = image.copy()
121
-
122
- # SAHI accetta solo detection; usiamo il wrapper Ultralytics
123
  model_type = "yolov8"
124
 
 
 
 
 
 
 
 
 
 
125
  try:
126
  detection_model = AutoDetectionModel.from_pretrained(
127
  model_type=model_type,
128
  model_path=weights_path,
129
  confidence_threshold=conf,
130
- device=device,
131
  )
132
  except Exception:
133
  detection_model = AutoDetectionModel.from_pretrained(
@@ -157,7 +159,7 @@ def build_app():
157
  gr.Markdown(
158
  "## YOLOv11 Instance Segmentation usata come Detection con SAHI\n"
159
  "- Carica un'immagine e lancia l'inferenza con pesi .pt Ultralytics (YOLOv11 segment).\n"
160
- "- SAHI effettua slicing/merging ma tratta il modello come detection: vengono usati i bounding box (le mask sono ignorate).\n"
161
  "- Plot: solo box, senza etichette; scarta box con lato > 70 px."
162
  )
163
 
@@ -166,16 +168,16 @@ def build_app():
166
  img_in = gr.Image(label="Immagine", type="numpy")
167
  weights = gr.Textbox(
168
  label="Percorso pesi (.pt)",
169
- value="/home/mclm/phd/best.pt",
170
- placeholder="es. src/scripts/best.pt",
171
  )
172
  target = gr.Textbox(label="Classe target", value="berry")
173
 
174
  with gr.Row():
175
  conf = gr.Slider(0.0, 1.0, value=0.35, step=0.01, label="Confidence")
176
  device = gr.Dropdown(
177
- ["cuda:0", "cpu"],
178
- value="cuda:0",
179
  label="Device",
180
  )
181
 
@@ -203,5 +205,6 @@ def build_app():
203
 
204
 
205
  if __name__ == "__main__":
206
- app = build_app()
207
- app.launch(server_name="0.0.0.0", server_port=7860, inbrowser=False, show_api=False)
 
 
 
 
 
1
  import os
2
  import cv2
3
  import numpy as np
 
77
 
78
  color_bgr = (0, 0, 255) if is_target else (0, 200, 0) # rosso per target, verde per altre
79
  cv2.rectangle(vis_bgr, (x1, y1), (x2, y2), color_bgr, 2)
80
+ # Nessuna label testuale
81
 
82
  total_count += 1
83
  if is_target:
 
96
  slice_w: int = 640,
97
  overlap_h: float = 0.10,
98
  overlap_w: float = 0.10,
99
+ device: str = "auto",
100
  target_class: str = "berry",
101
  ):
102
  """
 
104
  con pesi YOLOv11 di instance segmentation ma trattati come detection:
105
  - SAHI usa AutoDetectionModel 'yolov8' (wrapper Ultralytics detection)
106
  - Le mask sono ignorate, si usano i bounding box (come da .boxes)
107
+ - Si disegnano solo le bbox (senza label), scartando box con lato > MAX_SIDE_PX
 
 
108
  """
109
  if image is None:
110
  raise gr.Error("Devi caricare un'immagine.")
 
113
  raise gr.Error(f"File pesi non trovato: {weights_path}")
114
 
115
  image_rgb = image.copy()
 
 
116
  model_type = "yolov8"
117
 
118
+ # Scelta automatica device se 'auto'
119
+ chosen_device = device
120
+ if device == "auto":
121
+ try:
122
+ import torch
123
+ chosen_device = "cuda:0" if torch.cuda.is_available() else "cpu"
124
+ except Exception:
125
+ chosen_device = "cpu"
126
+
127
  try:
128
  detection_model = AutoDetectionModel.from_pretrained(
129
  model_type=model_type,
130
  model_path=weights_path,
131
  confidence_threshold=conf,
132
+ device=chosen_device,
133
  )
134
  except Exception:
135
  detection_model = AutoDetectionModel.from_pretrained(
 
159
  gr.Markdown(
160
  "## YOLOv11 Instance Segmentation usata come Detection con SAHI\n"
161
  "- Carica un'immagine e lancia l'inferenza con pesi .pt Ultralytics (YOLOv11 segment).\n"
162
+ "- SAHI effettua slicing/merging ma tratta il modello come detection: si usano i bounding box (le mask sono ignorate).\n"
163
  "- Plot: solo box, senza etichette; scarta box con lato > 70 px."
164
  )
165
 
 
168
  img_in = gr.Image(label="Immagine", type="numpy")
169
  weights = gr.Textbox(
170
  label="Percorso pesi (.pt)",
171
+ value="weights/best.pt",
172
+ placeholder="es. weights/best.pt",
173
  )
174
  target = gr.Textbox(label="Classe target", value="berry")
175
 
176
  with gr.Row():
177
  conf = gr.Slider(0.0, 1.0, value=0.35, step=0.01, label="Confidence")
178
  device = gr.Dropdown(
179
+ ["auto", "cuda:0", "cpu"],
180
+ value="auto",
181
  label="Device",
182
  )
183
 
 
205
 
206
 
207
  if __name__ == "__main__":
208
+ demo = build_app()
209
+ # Su Spaces non è necessario specificare server_name o share
210
+ demo.launch()