-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoResultsViewer.py
More file actions
364 lines (292 loc) · 13.3 KB
/
Copy pathVideoResultsViewer.py
File metadata and controls
364 lines (292 loc) · 13.3 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
VideoResultsViewer.py - Visualizza i risultati della pipeline con bounding box e ID database
"""
import cv2
import numpy as np
from pathlib import Path
from dataset import ObjectDatabase
import json
from datetime import datetime
import os
# Set OpenCV FFmpeg read attempts to avoid warnings with multi-stream videos
os.environ['OPENCV_FFMPEG_READ_ATTEMPTS'] = '5000000'
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
class VideoResultsViewer:
"""Classe per visualizzare i risultati della detection su video con ID database"""
def __init__(self, db_path="detections.db", crops_dir="crops_db"):
self.db = ObjectDatabase(db_path=db_path, feature_dir="features_db", crops_dir=crops_dir)
self.colors = {}
def get_class_color(self, class_name):
"""Ottiene un colore univoco per ogni classe"""
if class_name not in self.colors:
# Genera colore basato sull'hash del nome classe
np.random.seed(hash(class_name) % 2**32)
self.colors[class_name] = tuple(map(int, np.random.randint(50, 255, 3)))
return self.colors[class_name]
def show_detection_results(self, video_path, mapping_file="detection_mapping.json",
output_path=None, show_live=True, fps_override=None):
"""
Mostra il video con i bounding box e gli ID del database
Args:
video_path: Percorso del video originale
mapping_file: File JSON con il mapping frame->detections->db_id
output_path: Se specificato, salva il video annotato
show_live: Se True, mostra il video in tempo reale
fps_override: FPS per il video di output (None = usa FPS originale)
"""
print(f"🎬 Avvio visualizzazione risultati...")
print(f" Video: {video_path}")
print(f" Mapping: {mapping_file}")
print(f" Output: {output_path}")
print(f" Show live: {show_live}")
# Carica il mapping
if not Path(mapping_file).exists():
print(f"❌ File mapping non trovato: {mapping_file}")
print(" Assicurati di aver eseguito la pipeline con save_mapping=True")
return False
try:
with open(mapping_file, 'r') as f:
mapping = json.load(f)
print(f"✅ Mapping caricato: {len(mapping)} frame con detection")
except Exception as e:
print(f"❌ Errore nel caricare mapping: {e}")
return False
# Verifica video
if not Path(video_path).exists():
print(f"❌ Video non trovato: {video_path}")
return False
# Apri il video
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"❌ Impossibile aprire il video: {video_path}")
return False
# Proprietà video
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = fps_override if fps_override else cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"📹 Video aperto correttamente")
print(f" Risoluzione: {width}x{height}")
print(f" FPS: {fps}")
print(f" Frame totali: {total_frames}")
# Setup writer se richiesto
writer = None
if output_path:
# Crea la directory se non esiste
output_dir = Path(output_path).parent
output_dir.mkdir(parents=True, exist_ok=True)
print(f"✅ Directory creata: {output_dir}")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
if not writer.isOpened():
print(f"❌ Impossibile creare writer per: {output_path}")
cap.release()
return False
print(f"✅ Writer video creato per: {output_path}")
frame_idx = 0
frames_written = 0
detections_drawn = 0
print(f"\n▶️ Elaborazione video...")
if show_live:
print(" Premi 'q' per uscire, 'p' per pausa, SPACE per frame singolo")
paused = False
while True:
if not paused:
ret, frame = cap.read()
if not ret:
break
frame_idx += 1
# Crea copia per annotazioni
annotated_frame = frame.copy()
# Cerca detections per questo frame
frame_key = str(frame_idx)
if frame_key in mapping:
detections = mapping[frame_key]
for det in detections:
db_id = det.get('db_id')
class_name = det.get('class_name')
bbox = det.get('bbox')
confidence = det.get('confidence', 0)
if not bbox or db_id is None:
continue
# Estrai coordinate
x1 = int(bbox.get('x1', 0))
y1 = int(bbox.get('y1', 0))
x2 = int(bbox.get('x2', 0))
y2 = int(bbox.get('y2', 0))
# Colore per la classe
color = self.get_class_color(class_name)
# Disegna bounding box
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), color, 2)
# Prepara label
label = f"ID:{db_id} {class_name}"
conf_text = f"{confidence:.2f}" if confidence > 0 else ""
# Background per il testo
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
conf_size, _ = cv2.getTextSize(conf_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
# Rettangolo background per label principale
cv2.rectangle(annotated_frame,
(x1, y1 - label_size[1] - 8),
(x1 + label_size[0] + 4, y1),
color, -1)
# Testo label
cv2.putText(annotated_frame, label,
(x1 + 2, y1 - 4),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# Confidence in basso
if conf_text:
cv2.rectangle(annotated_frame,
(x1, y2),
(x1 + conf_size[0] + 4, y2 + conf_size[1] + 4),
color, -1)
cv2.putText(annotated_frame, conf_text,
(x1 + 2, y2 + conf_size[1]),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
detections_drawn += 1
# Info frame
info_text = f"Frame: {frame_idx}/{total_frames}"
cv2.putText(annotated_frame, info_text, (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Salva frame se richiesto
if writer:
writer.write(annotated_frame)
frames_written += 1
# Mostra frame se richiesto
if show_live:
cv2.imshow('Risultati Detection - q:esci p:pausa SPACE:step', annotated_frame)
key = cv2.waitKey(1 if not paused else 0) & 0xFF
if key == ord('q'):
print("\n⏹️ Interrotto dall'utente")
break
elif key == ord('p'):
paused = not paused
print(f"{'⏸️ Pausa' if paused else '▶️ Riprendi'}")
elif key == ord(' '):
paused = True
# Cleanup
cap.release()
if writer:
writer.release()
print(f"✅ Writer rilasciato")
if show_live:
cv2.destroyAllWindows()
print(f"\n✅ Elaborazione completata!")
print(f" Frame elaborati: {frame_idx}")
print(f" Frame salvati: {frames_written}")
print(f" Bounding box disegnati: {detections_drawn}")
if output_path:
if Path(output_path).exists():
size_mb = Path(output_path).stat().st_size / (1024*1024)
print(f"💾 Video salvato: {output_path}")
print(f" Dimensione: {size_mb:.2f} MB")
if size_mb < 0.1:
print(f"⚠️ ATTENZIONE: File molto piccolo, potrebbe essere vuoto")
if detections_drawn == 0:
print(f"⚠️ ATTENZIONE: Nessun bounding box disegnato!")
print(f" Verifica che il mapping contenga db_id validi")
else:
print(f"❌ ERRORE: File non creato: {output_path}")
return False
return True
def create_results_summary_video(self, video_path, output_path="results_summary.mp4"):
"""
Crea un video riassuntivo con tutte le detections dal database
Args:
video_path: Video originale
output_path: Dove salvare il video riassuntivo
"""
# Ottieni tutte le classi e oggetti
classes = self.db.get_all_classes()
if not classes:
print("❌ Nessun oggetto nel database")
return
# Apri video
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"❌ Impossibile aprire: {video_path}")
return
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# Writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
print(f"🎬 Creazione video riassuntivo...")
print(f" Classi trovate: {len(classes)}")
# Crea frame riassuntivo
summary_frame = np.zeros((height, width, 3), dtype=np.uint8)
y_offset = 50
for cls in classes:
class_name = cls['class_name']
count = cls['object_count']
color = self.get_class_color(class_name)
text = f"{class_name}: {count} oggetti"
cv2.putText(summary_frame, text, (50, y_offset),
cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
y_offset += 50
# Scrivi frame riassuntivo per 3 secondi
for _ in range(int(fps * 3)):
writer.write(summary_frame)
# Processa video originale
frame_idx = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_idx += 1
writer.write(frame)
cap.release()
writer.release()
print(f"✅ Video riassuntivo creato: {output_path}")
def close(self):
"""Chiude il database"""
self.db.close()
def show_pipeline_results(video_path, mapping_file="detection_mapping.json",
output_video=None, show_live=True):
"""
Funzione helper per mostrare i risultati della pipeline
Args:
video_path: Video originale
mapping_file: File JSON con mapping delle detections
output_video: Path per salvare video annotato (opzionale)
show_live: Se mostrare in tempo reale
"""
viewer = VideoResultsViewer()
viewer.show_detection_results(
video_path=video_path,
mapping_file=mapping_file,
output_path=output_video,
show_live=show_live
)
viewer.close()
if __name__ == "__main__":
# Esempio: mostra risultati del video più recente
import glob
from datetime import datetime
# Trova l'ultimo video registrato
videos = glob.glob("video/webcam_*.mp4")
if videos:
latest_video = max(videos, key=lambda x: Path(x).stat().st_mtime)
print(f"📹 Visualizzazione risultati per: {latest_video}")
# Genera nome file unico con timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
video_name = Path(latest_video).stem
output_video_path = f"runs/resultsWebcam/{video_name}_annotated_{timestamp}.mp4"
show_pipeline_results(
video_path=latest_video,
mapping_file="detection_mapping.json",
output_video=output_video_path,
show_live=True
)
else:
print("❌ Nessun video trovato")
print("💡 Esempio con video esistente:")
# Genera nome file unico con timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_video_path = f"runs/resultsWebcam/walletKeyGlasses_annotated_{timestamp}.mp4"
show_pipeline_results(
video_path="video/walletKeyGlasses.mp4",
mapping_file="detection_mapping.json",
output_video=output_video_path,
show_live=True
)