-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_high_res.py
More file actions
103 lines (77 loc) · 2.8 KB
/
Copy pathtest_high_res.py
File metadata and controls
103 lines (77 loc) · 2.8 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
import cv2
import torch
import numpy as np
import time
from tqdm import tqdm
from ultralytics import YOLO
# Chargement du modèle sur GPU
#model = YOLO("model_cls.pt").to("cuda")
#model = YOLO("model_honly.pt").to("cuda")
model = YOLO("yolov8s.pt").to("cuda")
# Chargement de la vidéo
cap = cv2.VideoCapture("1080p.mp4")
# Informations vidéo
width = 1920
height = 1080
fps = cap.get(cv2.CAP_PROP_FPS)
# Création du fichier de sortie
out = cv2.VideoWriter("sliding_output.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
# Paramètres de sliding window
window_size = 640
xstride = 480
ystride = 440
def get_windows(img, size=640, xstride=480, ystride=480):
H, W = img.shape[:2]
windows, coords = [], []
for y in range(0, H - size + 1, ystride):
for x in range(0, W - size + 1, xstride):
patch = img[y:y+size, x:x+size]
windows.append(img[y:y+size, x:x+size])
coords.append((x, y))
return windows, coords
def draw_sliding_windows(img, size=640, xstride=480, ystride=480):
count = 0
H, W = img.shape[:2]
for y in range(0, H - size + 1, ystride):
for x in range(0, W - size + 1, xstride):
cv2.rectangle(img, (x, y), (x + size, y + size), (0, 255, 0), 2)
cv2.putText(img, f"{count}", (x + 5, y + 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
count += 1
#print(f"Nombre total de tuiles : {count}")
return img
# FPS tracking
frame_count = 0
total_start = time.time()
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
for _ in tqdm(range(total_frames), desc="Traitement de la vidéo"):
ret, frame = cap.read()
if not ret:
break
start = time.time()
frame = cv2.resize(frame, (1920, 1080))
# Dessine les fenêtres sur l'image originale
frame_with_windows = draw_sliding_windows(frame.copy(), window_size, xstride, ystride)
patches, positions = get_windows(frame, window_size, xstride, ystride)
batch = [torch.from_numpy(patch).permute(2, 0, 1).float() / 255 for patch in patches]
batch = torch.stack(batch).to("cuda")
with torch.no_grad():
results = model(batch, verbose=False)
for i, result in enumerate(results):
annotated = result.plot()
x, y = positions[i]
frame_with_windows[y:y+window_size, x:x+window_size] = annotated
out.write(frame_with_windows)
end = time.time()
frame_count += 1
current_fps = 1 / (end - start)
#print(f"Frame {frame_count} — FPS instantané : {current_fps:.2f}", end='\r')
# FPS moyen
total_end = time.time()
total_time = total_end - total_start
avg_fps = frame_count / total_time
print(f"\nVidéo enregistrée : sliding_output.mp4")
print(f"FPS moyen global : {avg_fps:.2f} sur {frame_count} frames")
# Nettoyage
cap.release()
out.release()