-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathot.py
More file actions
596 lines (496 loc) · 27.6 KB
/
ot.py
File metadata and controls
596 lines (496 loc) · 27.6 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import cv2
from ultralytics import YOLO
import argparse
from tqdm import tqdm
import torch
import os
import sys
import subprocess
import threading
import time
import queue
import platform
# Attempt to import psutil for CPU/Memory monitoring
try:
import psutil
PSUTIL_AVAILABLE = True
except ImportError:
PSUTIL_AVAILABLE = False
print("⚠️ [WARNING] psutil library not found. RAM usage limiting and CPU/Memory utilization display will not be available.")
# Attempt to import GPUtil for GPU monitoring
try:
import GPUtil
GPUTIL_AVAILABLE = True
except (ImportError, Exception):
GPUTIL_AVAILABLE = False
# --- FFMPEG Check ---
FFMPEG_AVAILABLE = False
def check_ffmpeg():
global FFMPEG_AVAILABLE
try:
subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True, check=True)
print("[INFO] ffmpeg found and seems to be working.")
FFMPEG_AVAILABLE = True
except Exception:
print("⚠️ [WARNING] ffmpeg command not found or not working. Audio processing will be skipped.")
FFMPEG_AVAILABLE = False
return FFMPEG_AVAILABLE
# --- Configuration & Setup ---
DEFAULT_MODEL_PATH = "yolov8m.pt"
DEFAULT_OUTPUT_VIDEO_PATH_MARKER = "auto"
DEFAULT_ALLOWED_CLASSES = [
"person", "car", "truck", "bus", "motorcycle", "bicycle", "airplane", "bird", "cat", "dog",
"train", "boat", "bench", "backpack", "umbrella", "handbag", "suitcase", "sports ball",
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl",
"chair", "couch", "potted plant", "bed", "dining table",
"tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "refrigerator", "book", "clock", "vase", "scissors"
]
DEFAULT_CONFIDENCE_THRESHOLD = 0.25
TEMP_VIDEO_BASENAME = "temp_video_processed_silent.mp4"
OUTPUT_SUBDIRECTORY = "output"
FRAME_QUEUE_SIZE = 30
UTILIZATION_UPDATE_INTERVAL = 1.0
MAX_RAM_USAGE_PERCENT = 85.0
# Watermark Configuration
ENABLE_WATERMARK = True
WATERMARK_TEXT = "Processed by projectglyphmotion.studio"
# Simplified OpenCV watermark settings
WATERMARK_SCALE_FACTOR = 1.0 # Base scale
WATERMARK_THICKNESS = 1
WATERMARK_MARGIN_FRACTION = 0.02
# User Configuration
ENABLE_PREVIEW_IN_SCRIPT = False
USE_GPU_IN_SCRIPT = True
TARGET_PROCESSING_WIDTH = None
def get_system_utilization(device_to_use):
util_stats = {}
if PSUTIL_AVAILABLE:
util_stats['cpu'] = psutil.cpu_percent()
mem_info = psutil.virtual_memory()
util_stats['mem_used_gb'] = mem_info.used / (1024**3)
util_stats['mem_total_gb'] = mem_info.total / (1024**3)
util_stats['mem'] = mem_info.percent
if GPUTIL_AVAILABLE and device_to_use == "cuda":
try:
gpus = GPUtil.getGPUs()
if gpus:
gpu = gpus[0]
util_stats['gpu_load'] = gpu.load * 100
util_stats['gpu_mem'] = gpu.memoryUtil * 100
except Exception: pass
return util_stats
def format_utilization_string(stats):
parts = []
if 'cpu' in stats: parts.append(f"CPU:{stats['cpu']:.1f}%")
if 'mem' in stats:
parts.append(f"Mem:{stats['mem']:.1f}% ({stats.get('mem_used_gb',0):.1f}/{stats.get('mem_total_gb',0):.1f}GB)")
if 'gpu_load' in stats: parts.append(f"GPU-L:{stats['gpu_load']:.1f}%")
if 'gpu_mem' in stats: parts.append(f"GPU-M:{stats['gpu_mem']:.1f}%")
return " | ".join(parts) if parts else "Stats N/A"
def frame_reader_thread_func(cap, frame_input_queue, stop_event, target_width=None):
print("[INFO] Frame reader thread started.")
count = 0
original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
processing_width = original_width
processing_height = original_height
do_resize = False
if target_width and target_width > 0 and target_width < original_width:
aspect_ratio = original_height / original_width
processing_width = target_width
processing_height = int(processing_width * aspect_ratio)
if processing_height % 2 != 0: processing_height +=1
do_resize = True
print(f"[INFO] Frame reader: Resizing frames from {original_width}x{original_height} to {processing_width}x{processing_height}.")
else:
print(f"[INFO] Frame reader: Processing at native resolution {original_width}x{original_height}.")
while not stop_event.is_set() and cap.isOpened():
if PSUTIL_AVAILABLE:
current_ram_usage = psutil.virtual_memory().percent
ram_check_loops = 0
while current_ram_usage > MAX_RAM_USAGE_PERCENT and not stop_event.is_set():
if ram_check_loops % 5 == 0:
print(f"⚠️ [WARNING] [FRAME_READER] High RAM usage: {current_ram_usage:.1f}%. Pausing frame reading for 1s...")
time.sleep(1.0)
current_ram_usage = psutil.virtual_memory().percent
ram_check_loops +=1
if stop_event.is_set():
print("[INFO] [FRAME_READER] Stop event received during RAM pause.")
if not frame_input_queue.full(): frame_input_queue.put((False, None, None, None))
return
if frame_input_queue.qsize() < FRAME_QUEUE_SIZE:
ret, frame = cap.read()
if not ret:
print(f"[INFO] Frame reader: End of video or cannot read frame after {count} frames.")
frame_input_queue.put((False, None, None, None))
break
if do_resize:
try:
processed_frame = cv2.resize(frame, (processing_width, processing_height), interpolation=cv2.INTER_AREA)
except Exception as e:
print(f"❌ [ERROR] [FRAME_READER] Failed to resize frame: {e}")
processed_frame = frame
else:
processed_frame = frame
frame_input_queue.put((True, processed_frame, processing_width, processing_height))
count += 1
else:
time.sleep(0.005)
if not frame_input_queue.full():
frame_input_queue.put((False, None, None, None))
print(f"[INFO] Frame reader thread finished after reading {count} frames.")
def frame_writer_thread_func(video_writer, frame_output_queue, stop_event):
print("[INFO] Frame writer thread started.")
count = 0
while not stop_event.is_set():
try:
ret, frame_to_write = frame_output_queue.get(timeout=0.1)
if not ret:
print(f"[INFO] Frame writer: End signal received after writing {count} frames.")
break
if frame_to_write is not None:
video_writer.write(frame_to_write)
count +=1
frame_output_queue.task_done()
except queue.Empty:
if stop_event.is_set() and frame_output_queue.empty():
print("[INFO] Frame writer: Stop event set and queue empty.")
break
continue
except Exception as e:
print(f"❌ [ERROR] [FRAME_WRITER] Error writing frame: {e}")
break
while not frame_output_queue.empty():
try:
ret, frame_to_write = frame_output_queue.get_nowait()
if ret and frame_to_write is not None:
video_writer.write(frame_to_write)
count +=1
frame_output_queue.task_done()
except queue.Empty: break
print(f"[INFO] Frame writer thread finished. Total frames written: {count}")
def parse_arguments():
parser = argparse.ArgumentParser(description="Object Tracking: YOLOv8, Threaded I/O, GPU/CPU, Progress, ffmpeg Audio, Utilization")
parser.add_argument("--model",type=str,default=DEFAULT_MODEL_PATH,help="Path to YOLOv8 model.")
parser.add_argument("--output_video",type=str,default=DEFAULT_OUTPUT_VIDEO_PATH_MARKER, help="Path for final video. Default: 'auto' (derived from input name, saved in 'output/' subdir).")
parser.add_argument("--allowed_classes",nargs="+",default=DEFAULT_ALLOWED_CLASSES,help=f"Classes to track.")
parser.add_argument("--confidence_threshold",type=float,default=DEFAULT_CONFIDENCE_THRESHOLD,help=f"Min confidence.")
parser.add_argument("--box_color", type=str, default="0,255,0", help="Color for bounding box as R,G,B (default: green)")
# --- NEW ROI FLAG ---
parser.add_argument("-r", "--select_roi", action="store_true", help="Manually select Region of Interest (ROI) for tracking.")
return parser.parse_args()
def process_audio_ffmpeg(video_source_path, temp_silent_video_abs_path, final_output_video_path, target_fps):
"""
Merges audio from source to processed video.
Uses 'copy' codec for video to preserve quality and speed.
"""
if not FFMPEG_AVAILABLE:
print("⚠️ [WARNING] [AUDIO_THREAD] ffmpeg not available. Skipping audio merge.")
if os.path.exists(temp_silent_video_abs_path) and not os.path.exists(final_output_video_path):
try: os.rename(temp_silent_video_abs_path, final_output_video_path); print(f"✅ [SUCCESS] Silent video saved as '{final_output_video_path}'.")
except OSError as e: print(f"❌ [ERROR] Could not rename temp file: {e}.")
return False
if not os.path.exists(temp_silent_video_abs_path):
print(f"❌ [ERROR] [AUDIO_THREAD] Temporary silent video '{temp_silent_video_abs_path}' not found. Cannot merge audio.")
return False
print(f"\n[AUDIO_THREAD] Merging audio (Stream Copy).")
if os.path.exists(final_output_video_path): print(f"⚠️ [WARNING] Output file {final_output_video_path} exists. Overwriting.")
# Using -c:v copy is CRITICAL here.
# It takes the video stream we just created with OpenCV (which now has the watermark burned in)
# and copies it directly without re-encoding, avoiding all complex filter issues.
ffmpeg_command = [
"ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-stats",
"-i", temp_silent_video_abs_path,
"-i", video_source_path,
"-c:v", "copy",
"-c:a", "aac",
"-map", "0:v:0",
"-map", "1:a:0?",
"-shortest",
final_output_video_path
]
try:
print(f"[AUDIO_THREAD] Executing: {' '.join(ffmpeg_command)}")
subprocess.run(ffmpeg_command, check=True)
print(f"\n✅ [SUCCESS] [AUDIO_THREAD] ffmpeg successfully processed. Output: '{final_output_video_path}'")
return True
except subprocess.CalledProcessError as e:
print(f"❌ [ERROR] [AUDIO_THREAD] ffmpeg failed (code {e.returncode}).")
except Exception as e_ffmpeg: print(f"❌ [ERROR] [AUDIO_THREAD] ffmpeg error: {e_ffmpeg}")
return False
def main():
start_time_total = time.time()
temp_video_file_abs_path = os.path.abspath(TEMP_VIDEO_BASENAME)
# --- PyTorch CPU Threading ---
try:
cpu_cores = os.cpu_count()
if cpu_cores:
num_threads_to_set = max(1, cpu_cores // 2)
torch.set_num_threads(num_threads_to_set)
print(f"[INFO] Suggested {num_threads_to_set} threads for PyTorch CPU operations.")
else:
print("[INFO] Could not determine CPU core count for PyTorch thread setting.")
except Exception as e:
print(f"⚠️ [WARNING] Could not set PyTorch CPU threads: {e}")
print("--- FFMPEG Check ---"); check_ffmpeg(); print("--------------------")
if PSUTIL_AVAILABLE: print("[DEBUG] Priming psutil.cpu_percent()..."); psutil.cpu_percent()
if GPUTIL_AVAILABLE:
print("[DEBUG] Attempting to prime GPUtil.getGPUs()...")
try: GPUtil.getGPUs(); print("[DEBUG] GPUtil.getGPUs() primed.")
except Exception as e: print(f"[DEBUG] Error GPUtil priming: {e}")
args = parse_arguments()
try:
box_color = tuple(int(c) for c in args.box_color.split(','))[::-1]
if len(box_color) != 3 or not all(0 <= c <= 255 for c in box_color):
raise ValueError
except:
print("❌ [ERROR] Invalid --box_color format. Use R,G,B with values 0-255. Defaulting to green.")
box_color = (0, 255, 0)
box_color = (0, 255, 0)
input_video_path_interactive = input("➡️ Please enter the path to the input video file: ").strip()
if input_video_path_interactive.startswith("'") and input_video_path_interactive.endswith("'"):
input_video_path_interactive = input_video_path_interactive[1:-1]
if input_video_path_interactive.startswith('"') and input_video_path_interactive.endswith('"'):
input_video_path_interactive = input_video_path_interactive[1:-1]
if not input_video_path_interactive:
print("❌ [ERROR] No input video path provided. Exiting."); return
if not os.path.exists(input_video_path_interactive) or not os.path.isfile(input_video_path_interactive):
print(f"❌ [ERROR] Input video file not found or is not a file: {input_video_path_interactive}"); return
current_input_video = input_video_path_interactive
show_preview = ENABLE_PREVIEW_IN_SCRIPT
use_gpu = USE_GPU_IN_SCRIPT
target_processing_width = TARGET_PROCESSING_WIDTH
os.makedirs(OUTPUT_SUBDIRECTORY, exist_ok=True)
final_output_video_path = args.output_video
if args.output_video == DEFAULT_OUTPUT_VIDEO_PATH_MARKER:
input_basename_only = os.path.basename(current_input_video)
name, ext = os.path.splitext(input_basename_only)
base_name = f"{name}_processed{ext}"
final_output_video_path = os.path.join(OUTPUT_SUBDIRECTORY, base_name)
else:
if not os.path.isabs(args.output_video):
final_output_video_path = os.path.join(OUTPUT_SUBDIRECTORY, os.path.basename(args.output_video))
print(f"[INFO] Final output video will be saved as: {final_output_video_path}")
# Notice: Copy mode is conditional now based on watermark
print("[INFO] ffmpeg will use direct video stream copy (fast/safe). Watermark is applied by OpenCV.")
device_to_use = "cpu"
if use_gpu and torch.cuda.is_available(): device_to_use = "cuda"; print("✅ [SUCCESS] CUDA GPU available. Using GPU.")
elif use_gpu: print("⚠️ [WARNING] CUDA GPU not found. Falling back to CPU.")
else: print("ℹ️ [INFO] Using CPU.")
print(f"[INFO] Loading model: {args.model} on {device_to_use}")
try: model = YOLO(args.model); model.to(device_to_use); print(f"✅ [SUCCESS] Model loaded.")
except Exception as e: print(f"❌ [ERROR] Failed to load model: {e}"); return
print(f"[INFO] Tracking classes: {args.allowed_classes}")
video_source_path = current_input_video
video_capture_source = current_input_video
cap = cv2.VideoCapture(video_capture_source)
if not cap.isOpened(): print(f"❌ [ERROR] Failed to open input video file: {video_capture_source}"); return
original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f"[INFO] Input video dimensions: {original_width}x{original_height}")
output_width = original_width
output_height = original_height
effective_target_width_for_reader = None
if target_processing_width and target_processing_width > 0:
if target_processing_width < output_width:
effective_target_width_for_reader = target_processing_width
aspect_ratio = output_height / output_width
output_width = effective_target_width_for_reader
output_height = int(output_width * aspect_ratio)
if output_height % 2 != 0: output_height +=1
print(f"[INFO] Target processing width set to {output_width}. Final output video will be resized to {output_width}x{output_height}")
else:
print(f"[INFO] Target processing width ({target_processing_width}) is not less than the effective video width ({output_width}). Processing at effective resolution.")
# ROI Selection Logic
roi_bbox = None
if args.select_roi:
print("[INFO] ROI selection requested. Reading first frame...")
ret, first_frame = cap.read()
if ret:
# Must replicate the preprocessing logic (resize) used in reader thread
preview_frame = first_frame
if effective_target_width_for_reader:
try:
preview_frame = cv2.resize(preview_frame, (output_width, output_height), interpolation=cv2.INTER_AREA)
except: pass
# Dynamic Resizing for Selection Window
MAX_WINDOW_H = 900
MAX_WINDOW_W = 1600
h, w = preview_frame.shape[:2]
scale_factor = 1.0
if h > MAX_WINDOW_H or w > MAX_WINDOW_W:
scale_factor = min(MAX_WINDOW_H / h, MAX_WINDOW_W / w)
new_w = int(w * scale_factor)
new_h = int(h * scale_factor)
display_frame = cv2.resize(preview_frame, (new_w, new_h))
print(f"[INFO] Resizing selection window to {new_w}x{new_h} (Scale: {scale_factor:.2f})")
else:
display_frame = preview_frame
print("---------------------------------------------------------------")
print("📣 ACTION REQUIRED: Select the Region of Interest (ROI)")
print(" 1. Draw a box around the area you want to track.")
print(" 2. Press SPACE or ENTER to confirm.")
print(" 3. Press 'c' to cancel ROI selection.")
print("---------------------------------------------------------------")
r = cv2.selectROI("Select ROI", display_frame, fromCenter=False, showCrosshair=True)
cv2.destroyWindow("Select ROI")
if int(r[2]) > 0 and int(r[3]) > 0:
# Map the coordinates back to the full resolution frame
real_x = int(r[0] / scale_factor)
real_y = int(r[1] / scale_factor)
real_w = int(r[2] / scale_factor)
real_h = int(r[3] / scale_factor)
roi_bbox = (real_x, real_y, real_w, real_h)
print(f"✅ [SUCCESS] ROI selected: {roi_bbox}")
else:
print("⚠️ [WARNING] ROI selection cancelled or empty. Processing full frame.")
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
else:
print("❌ [ERROR] Could not read first frame for ROI selection.")
fps=cap.get(cv2.CAP_PROP_FPS)
if fps==0.0 or fps is None: fps=30.0; print(f"⚠️ [WARNING] FPS not readable, defaulting to {fps} FPS.")
print(f"[INFO] Input video (FPS for output writer): {fps:.2f} FPS")
fourcc=cv2.VideoWriter_fourcc(*"mp4v")
print(f"[INFO] Using FourCC: MP4V for temporary video writer.")
temp_out_silent_video_writer=cv2.VideoWriter(temp_video_file_abs_path, fourcc,float(fps),(output_width,output_height))
if not temp_out_silent_video_writer.isOpened():
print(f"❌ [ERROR] Failed to open temp video writer with MP4V: {temp_video_file_abs_path}"); cap.release(); return
print(f"[INFO] Temp silent video will be {output_width}x{output_height} at {temp_video_file_abs_path}")
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
stop_event=threading.Event()
frame_input_queue=queue.Queue(maxsize=FRAME_QUEUE_SIZE)
reader_thread=threading.Thread(target=frame_reader_thread_func,args=(cap,frame_input_queue,stop_event, effective_target_width_for_reader))
reader_thread.daemon = True
reader_thread.start()
frame_output_queue=queue.Queue(maxsize=FRAME_QUEUE_SIZE)
writer_thread=threading.Thread(target=frame_writer_thread_func,args=(temp_out_silent_video_writer,frame_output_queue,stop_event))
writer_thread.daemon = True
writer_thread.start()
pbar_desc=f"Processing Frames ({device_to_use.upper()})"
if total_frames > 0:
pbar_format = "{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]"
pbar = tqdm(total=total_frames, desc=pbar_desc, unit="frame", bar_format=pbar_format, mininterval=UTILIZATION_UPDATE_INTERVAL)
else:
pbar_format = "{l_bar}{bar}| {n_fmt} frames [{elapsed}, {rate_fmt}{postfix}]"
pbar = tqdm(desc=pbar_desc, unit="frame", bar_format=pbar_format, mininterval=UTILIZATION_UPDATE_INTERVAL)
print(f"🚀 Starting video frame processing on {device_to_use.upper()}...")
frame_count=0; processing_loop_active=True; last_util_update_time=time.time()
try:
while processing_loop_active:
try:
ret, frame_to_process, current_proc_w, current_proc_h = frame_input_queue.get(timeout=1.0)
if not ret: processing_loop_active=False; break
frame_input_queue.task_done()
except queue.Empty:
if not reader_thread.is_alive() and frame_input_queue.empty(): processing_loop_active=False; break
continue
frame_count += 1
# ROI PRE-PROCESSING
inference_frame = frame_to_process
offset_x, offset_y = 0, 0
if roi_bbox is not None:
rx, ry, rw, rh = roi_bbox
# Basic bounds checking
frame_h, frame_w = frame_to_process.shape[:2]
rx = max(0, min(rx, frame_w))
ry = max(0, min(ry, frame_h))
rw = max(1, min(rw, frame_w - rx))
rh = max(1, min(rh, frame_h - ry))
inference_frame = frame_to_process[ry:ry+rh, rx:rx+rw]
offset_x, offset_y = rx, ry
results = model.track(inference_frame, persist=True, verbose=False, conf=args.confidence_threshold)
annotated_frame = frame_to_process.copy()
if results and results[0].boxes:
for box in results[0].boxes:
if box.id is None: continue
cls_id=int(box.cls[0]); class_name=model.names[cls_id]; track_id=int(box.id[0])
if class_name in args.allowed_classes:
x1,y1,x2,y2=map(int,box.xyxy[0])
conf=box.conf[0]
# ROI COORDINATE ADJUSTMENT
# Map coordinates from the cropped inference frame back to the full frame
x1 += offset_x
x2 += offset_x
y1 += offset_y
y2 += offset_y
label=f"ID:{track_id} {class_name} {conf:.2f}"
cv2.rectangle(annotated_frame,(x1,y1),(x2,y2),box_color,2)
(tw,th),_=cv2.getTextSize(label,cv2.FONT_HERSHEY_SIMPLEX,0.5,1)
cv2.rectangle(annotated_frame,(x1,y1-th-10),(x1+tw,y1-5),box_color,-1)
cv2.putText(annotated_frame,label,(x1,y1-5),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1)
# OPENCV WATERMARKING
if ENABLE_WATERMARK:
h, w = annotated_frame.shape[:2]
# Dynamic scaling: 1.0 scale for 1000px height roughly
font_scale = min(w, h) / 1000.0 * 0.8
thickness = max(1, int(font_scale * 1.5))
(text_w, text_h), baseline = cv2.getTextSize(WATERMARK_TEXT, cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness)
margin_x = int(w * WATERMARK_MARGIN_FRACTION)
margin_y = int(h * WATERMARK_MARGIN_FRACTION)
# Bottom-Right Position
x_pos = w - text_w - margin_x
y_pos = h - margin_y
# Draw Shadow (Black outline)
cv2.putText(annotated_frame, WATERMARK_TEXT, (x_pos+1, y_pos+1), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0,0,0), thickness+1, cv2.LINE_AA)
# Draw Text (White)
cv2.putText(annotated_frame, WATERMARK_TEXT, (x_pos, y_pos), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255,255,255), thickness, cv2.LINE_AA)
frame_output_queue.put((True, annotated_frame))
current_time = time.time()
if current_time - last_util_update_time >= UTILIZATION_UPDATE_INTERVAL:
util_stats = get_system_utilization(device_to_use)
pbar.set_postfix_str(format_utilization_string(util_stats), refresh=True)
last_util_update_time = current_time
if show_preview:
cv2.imshow("Object Tracking", annotated_frame)
if cv2.waitKey(1)&0xFF==ord("q"): processing_loop_active=False; break
pbar.update(1)
del results
except KeyboardInterrupt: print("\n[INFO] KeyboardInterrupt. Stopping..."); processing_loop_active = False
finally:
print("[INFO] Main loop finished. Signaling I/O threads to stop...")
if stop_event: stop_event.set()
if reader_thread is not None and reader_thread.is_alive():
print("[INFO] Waiting for frame reader thread..."); reader_thread.join(timeout=5.0)
if reader_thread.is_alive(): print("⚠️ [WARNING] Frame reader didn't finish in time.")
if frame_output_queue is not None:
frame_output_queue.put((False, None))
if writer_thread is not None and writer_thread.is_alive():
print("[INFO] Waiting for frame writer thread..."); writer_thread.join(timeout=10.0)
if writer_thread.is_alive(): print("⚠️ [WARNING] Frame writer didn't finish in time.")
pbar.close();
if cap.isOpened(): cap.release()
if temp_out_silent_video_writer.isOpened(): temp_out_silent_video_writer.release()
if show_preview: cv2.destroyAllWindows()
print(f"✅ [SUCCESS] Video frame processing complete. Silent video: '{temp_video_file_abs_path}'.")
print(f"[INFO] Total frames processed: {frame_count}")
audio_processing_was_successful = False
if FFMPEG_AVAILABLE:
print("[INFO] Starting audio processing thread...")
audio_processing_was_successful = process_audio_ffmpeg(video_source_path, temp_video_file_abs_path, final_output_video_path, float(fps))
else:
if os.path.exists(temp_video_file_abs_path):
try:
if os.path.exists(final_output_video_path): os.remove(final_output_video_path)
os.rename(temp_video_file_abs_path, final_output_video_path); print(f"✅ [SUCCESS] Silent video saved: '{final_output_video_path}'.")
audio_processing_was_successful = True
except OSError as e: print(f"❌ [ERROR] Could not rename temp file: {e}.")
if os.path.exists(temp_video_file_abs_path):
if audio_processing_was_successful and os.path.abspath(temp_video_file_abs_path) != os.path.abspath(final_output_video_path):
try:
os.remove(temp_video_file_abs_path)
print(f"[CLEANUP] Temp silent video '{temp_video_file_abs_path}' deleted.")
except OSError as e:
print(f"⚠️ [WARNING] Error deleting temporary file '{temp_video_file_abs_path}': {e}")
elif not audio_processing_was_successful and os.path.abspath(temp_video_file_abs_path) == os.path.abspath(final_output_video_path):
print(f"[INFO] Audio processing failed/skipped. Output is the processed silent video: '{final_output_video_path}'.")
elif not audio_processing_was_successful and os.path.exists(temp_video_file_abs_path):
print(f"[INFO] Audio processing failed/skipped. Temporary file '{temp_video_file_abs_path}' is kept.")
end_time_total = time.time()
total_processing_time = end_time_total - start_time_total
minutes = int(total_processing_time // 60)
seconds = int(total_processing_time % 60)
print(f"\n[DONE] Total script execution time: {minutes} minute(s) and {seconds} second(s).")
print(f"[INFO] Final output video is at: {final_output_video_path}")
if __name__ == "__main__":
main()