-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest5.py
160 lines (125 loc) · 6.32 KB
/
test5.py
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
import cv2
import torch
import numpy as np
from pathlib import Path
import argparse
from deep_sort_realtime.deepsort_tracker import DeepSort
import time
def load_yolo_model():
model = torch.hub.load('./yolov5', 'yolov5s', source='local', pretrained=True)
return model
def process_video(video_path, output_path, model, default_confidence):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Không thể mở video {video_path}")
return
deep_sort = DeepSort(max_age=30)
original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
new_width = 1280
new_height = int(original_height * (new_width / original_width))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, cap.get(cv2.CAP_PROP_FPS), (new_width, new_height))
vehicle_classes = ['car', 'bus', 'truck', 'motorcycle']
pedestrian_classes = ['person']
class_to_index = {name: idx for idx, name in model.names.items()}
vehicle_indices = [class_to_index[c] for c in vehicle_classes if c in class_to_index]
pedestrian_indices = [class_to_index[c] for c in pedestrian_classes if c in class_to_index]
interested_indices = vehicle_indices + pedestrian_indices
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Khởi tạo bộ đếm
current_vehicle_count = 0
total_vehicle_count = 0
current_pedestrian_count = 0
total_pedestrian_count = 0
tracked_ids = set()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_resized = cv2.resize(frame, (new_width, new_height))
results = model(frame_resized)
detections = results.xyxy[0].cpu().numpy()
valid_detections = detections[
(detections[:, 4] >= default_confidence) &
np.isin(detections[:, 5], interested_indices)
]
# Reset current counts for each frame
current_vehicle_count = 0
current_pedestrian_count = 0
if len(valid_detections) > 0:
track_boxes = valid_detections[:, :4]
track_scores = valid_detections[:, 4]
track_class_ids = valid_detections[:, 5]
detections_for_deepsort = [
([x1, y1, x2 - x1, y2 - y1], score, class_id)
for (x1, y1, x2, y2), score, class_id in zip(track_boxes, track_scores, track_class_ids)
]
tracks = deep_sort.update_tracks(detections_for_deepsort, frame=frame_resized)
for track in tracks:
if track.is_confirmed():
ltrb = track.to_ltrb()
x1, y1, x2, y2 = map(int, ltrb)
class_id = track.get_det_class()
label = 'Vehicle' if class_id in vehicle_indices else 'Person'
color = (0, 255, 0) if label == 'Vehicle' else (0, 0, 255)
cv2.rectangle(frame_resized, (x1, y1), (x2, y2), color, 2)
cv2.putText(frame_resized, f"{label} ID:{track.track_id}", (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# Đếm đối tượng
if label == 'Vehicle':
current_vehicle_count += 1
if track.track_id not in tracked_ids:
tracked_ids.add(track.track_id)
total_vehicle_count += 1
else:
current_pedestrian_count += 1
if track.track_id not in tracked_ids:
tracked_ids.add(track.track_id)
total_pedestrian_count += 1
# Hiển thị số lượng đối tượng trên video
cv2.putText(frame_resized, f"Current Vehicles: {current_vehicle_count}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.putText(frame_resized, f"Total Vehicles: {total_vehicle_count}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.putText(frame_resized, f"Current People: {current_pedestrian_count}", (10, 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.putText(frame_resized, f"Total People: {total_pedestrian_count}", (10, 120),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
out.write(frame_resized)
# Hiển thị tiến độ xử lý
current_frame = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
progress = (current_frame / frame_count) * 100
print(f"\rProcessing video... {current_frame}/{frame_count} frames ({progress:.2f}%)", end="")
cap.release()
out.release()
print(f"\nVideo đầu ra đã được lưu tại {output_path}")
def process_multiple_videos(video_paths, output_dir, confidence):
start_time = time.time()
print(f"Thời gian bắt đầu: {time.strftime('%H:%M:%S', time.localtime(start_time))}")
Path(output_dir).mkdir(parents=True, exist_ok=True)
model = load_yolo_model()
total_videos = len(video_paths)
for idx, video_path in enumerate(video_paths, 1):
if not Path(video_path).exists():
print(f"Error: Video file {video_path} does not exist.")
continue
video_name = Path(video_path).name
output_path = Path(output_dir) / f"result_{video_name}"
print(f"Processing {video_path} ({idx}/{total_videos})...")
process_video(video_path, str(output_path), model, confidence)
end_time = time.time()
print(f"Thời gian kết thúc: {time.strftime('%H:%M:%S', time.localtime(end_time))}")
completion_time = end_time - start_time
hours, rem = divmod(completion_time, 3600)
minutes, seconds = divmod(rem, 60)
print(f"Thời gian hoàn thành: {int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Video Object Detection")
parser.add_argument('--confidence', type=float, default=0.6, help='Default confidence threshold for YOLOv5')
args = parser.parse_args()
video_paths = [
"video/test31.mp4",
]
output_dir = "result_videos"
process_multiple_videos(video_paths, output_dir, args.confidence)