-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject_tracking.py
230 lines (193 loc) · 8.12 KB
/
object_tracking.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
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
import cv2
import torch
import numpy as np
import argparse, os
from deep_sort_realtime.deepsort_tracker import DeepSort
import time
from ultralytics import YOLO
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--video",
type=str,
nargs="?",
default="content/highway.mp4",
help="Path to input video"
)
parser.add_argument(
"--output",
type=str,
nargs="?",
help="path to output video",
default="content/output.mp4"
)
parser.add_argument(
"--conf",
type=float,
default=0.50,
help="confidence threshold",
)
parser.add_argument(
"--blur_id",
type=int,
default=None,
help="class ID to apply Gaussian Blur",
)
parser.add_argument(
"--class_id",
type=int,
default=None,
help="class ID to track",
)
opt = parser.parse_args()
return opt
def draw_corner_rect(img, bbox, line_length=30, line_thickness=5, rect_thickness=1,
rect_color=(255, 0, 255), line_color=(0, 255, 0)):
x, y, w, h = bbox
x1, y1 = x + w, y + h
if rect_thickness != 0:
cv2.rectangle(img, bbox, rect_color, rect_thickness)
# Top Left x, y
cv2.line(img, (x, y), (x + line_length, y), line_color, line_thickness)
cv2.line(img, (x, y), (x, y + line_length), line_color, line_thickness)
# Top Right x1, y
cv2.line(img, (x1, y), (x1 - line_length, y), line_color, line_thickness)
cv2.line(img, (x1, y), (x1, y + line_length), line_color, line_thickness)
# Bottom Left x, y1
cv2.line(img, (x, y1), (x + line_length, y1), line_color, line_thickness)
cv2.line(img, (x, y1), (x, y1 - line_length), line_color, line_thickness)
# Bottom Right x1, y1
cv2.line(img, (x1, y1), (x1 - line_length, y1), line_color, line_thickness)
cv2.line(img, (x1, y1), (x1, y1 - line_length), line_color, line_thickness)
return img
def calculate_speed(distance, fps):
return (distance *fps)*3.6
def calculate_distance(p1, p2):
return np.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
def read_frames(cap):
while True:
ret, frame = cap.read()
if not ret:
break
yield frame
def main(_argv):
FRAME_WIDTH=30
FRAME_HEIGHT=100
SOURCE_POLYGONE = np.array([[18, 550], [1852, 608],[1335, 370], [534, 343]], dtype=np.float32)
BIRD_EYE_VIEW = np.array([[0, 0], [FRAME_WIDTH, 0], [FRAME_WIDTH, FRAME_HEIGHT],[0, FRAME_HEIGHT]], dtype=np.float32)
M = cv2.getPerspectiveTransform(SOURCE_POLYGONE, BIRD_EYE_VIEW)
# Initialize the video capture
video_input = opt.video
cap = cv2.VideoCapture(video_input)
if not cap.isOpened():
print('Error: Unable to open video source.')
return
frame_generator = read_frames(cap)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
pts = SOURCE_POLYGONE.astype(np.int32)
pts = pts.reshape((-1, 1, 2))
polygon_mask = np.zeros((frame_height, frame_width), dtype=np.uint8)
cv2.fillPoly(polygon_mask, [pts], 255)
# video writer objects
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter(opt.output, fourcc, fps, (frame_width, frame_height))
# Initialize the DeepSort tracker
tracker = DeepSort(max_age=50)
# Load YOLO model
model = YOLO("yolov10n.pt")
# Load the COCO class labels
classes_path = "configs/coco.names"
with open(classes_path, "r") as f:
class_names = f.read().strip().split("\n")
np.random.seed(42)
colors = np.random.randint(0, 255, size=(len(class_names), 3))
# FPS calculation variables
frame_count = 0
start_time = time.time()
prev_positions={}
speed_accumulator={}
while True:
try:
frame = next(frame_generator)
except StopIteration:
break
# Run model on each frame
with torch.no_grad():
results = model(frame)
detect = []
for pred in results:
for box in pred.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0] )
confidence = box.conf[0]
label = box.cls[0]
# Filter out weak detections by confidence threshold and class_id
if opt.class_id is None:
if confidence < opt.conf:
continue
else:
if class_id != opt.class_id or confidence < opt.conf:
continue
if polygon_mask[(y1 + y2) // 2, (x1 + x2) // 2] == 255:
detect.append([[x1, y1, x2 - x1, y2 - y1], confidence, int(label)])
tracks = tracker.update_tracks(detect, frame=frame)
for track in tracks:
if not track.is_confirmed():
continue
track_id = track.track_id
ltrb = track.to_ltrb()
class_id = track.get_det_class()
x1, y1, x2, y2 = map(int, ltrb)
if polygon_mask[(y1+y2)//2,(x1+x2)//2] == 0:
tracks.remove(track)
color = colors[class_id]
B, G, R = map(int, color)
text = f"{track_id} - {class_names[class_id]}"
center_pt = np.array([[(x1+x2)//2, (y1+y2)//2]], dtype=np.float32)
transformed_pt = cv2.perspectiveTransform(center_pt[None, :, :], M)
if track_id in prev_positions:
prev_position = prev_positions[track_id]
distance = calculate_distance(prev_position, transformed_pt[0][0])
speed = calculate_speed(distance, fps)
if track_id in speed_accumulator:
speed_accumulator[track_id].append(speed)
if len(speed_accumulator[track_id]) > 100:
speed_accumulator[track_id].pop(0)
else:
speed_accumulator[track_id] = []
speed_accumulator[track_id].append(speed)
prev_positions[track_id] = transformed_pt[0][0]
# Draw bounding box and text
frame = draw_corner_rect(frame, (x1, y1, x2 - x1, y2 - y1), line_length=15, line_thickness=3, rect_thickness=1, rect_color=(B, G, R), line_color=(R, G, B))
#cv2.rectangle(frame, (x1, y1), (x2, y2), (B, G, R), 2)
cv2.rectangle(frame, (x1 - 1, y1 - 20), (x1 + len(text) * 10, y1), (B, G, R), -1)
cv2.putText(frame, text, (x1 + 5, y1 - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
if track_id in speed_accumulator :
avg_speed = sum(speed_accumulator[track_id]) / len(speed_accumulator[track_id])
#print(avg_speed)
cv2.rectangle(frame, (x1 - 1, y1-40 ), (x1 + len(f"Speed: {avg_speed:.0f} km/h") * 10, y1-20), (0, 0, 255), -1)
cv2.putText(frame, f"Speed: {avg_speed:.0f} km/h", (x1, y1 - 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# Apply Gaussian Blur
if opt.blur_id is not None and class_id == opt.blur_id:
print("true")
if 0 <= x1 < x2 <= frame.shape[1] and 0 <= y1 < y2 <= frame.shape[0]:
frame[y1:y2, x1:x2] = cv2.GaussianBlur(frame[y1:y2, x1:x2], (99, 99), 3)
cv2.polylines(frame, [pts], isClosed=True, color=(255, 0, 0), thickness=2)
cv2.putText(frame, f"Height: {FRAME_HEIGHT}", (1500, 900), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.putText(frame, f"Width: {FRAME_WIDTH}", (1530, 930), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow('speed_estimation', frame)
writer.write(frame)
frame_count += 1
if frame_count % 10 == 0:
elapsed_time = time.time() - start_time
fps_calc = frame_count / elapsed_time
print(f"FPS: {fps_calc:.2f}")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
writer.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
opt = parse_args()
main(opt)