-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_detection_and_tracking.py
More file actions
177 lines (120 loc) · 3.26 KB
/
Copy pathobject_detection_and_tracking.py
File metadata and controls
177 lines (120 loc) · 3.26 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
# -*- coding: utf-8 -*-
"""Object detection_and_Tracking
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1u89Ak1oBNPtZ-KWsgcaJjIPHFUZT-EBP
"""
!pip install ultralytics
!pip install opencv-python
!pip install filterpy
!pip install lap
import cv2
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
from google.colab import files
uploaded = files.upload()
from google.colab.patches import cv2_imshow
cap = cv2.VideoCapture("sample_video.mp4")
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)
annotated_frame = results[0].plot()
cv2_imshow(annotated_frame)
break # Shows only first frame
cap.release()
out.release()
print("photo saved as output_photo")
import cv2
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("sample_video.mp4")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"output_video.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(width, height)
)
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)
annotated_frame = results[0].plot()
out.write(annotated_frame)
cap.release()
out.release()
print("Video saved as output_video.mp4")
from google.colab import files
files.download("output_video.mp4")
!git clone https://github.com/abewley/sort.git
!sed -i "s/matplotlib.use('TkAgg')/matplotlib.use('Agg')/g" /content/sort/sort.py
import sys
sys.path.append('/content/sort')
from sort import Sort
tracker = Sort()
import cv2
import numpy as np
from ultralytics import YOLO
from sort import Sort
model = YOLO("yolov8n.pt")
tracker = Sort()
cap = cv2.VideoCapture("sample_video.mp4")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"tracked_output.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(width, height)
)
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)
detections = []
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = box.xyxy[0]
conf = float(box.conf[0])
detections.append([
int(x1),
int(y1),
int(x2),
int(y2),
conf
])
if len(detections) > 0:
detections = np.array(detections)
tracks = tracker.update(detections)
for track in tracks:
x1, y1, x2, y2, track_id = track
cv2.rectangle(
frame,
(int(x1), int(y1)),
(int(x2), int(y2)),
(0,255,0),
2
)
cv2.putText(
frame,
f"ID:{int(track_id)}",
(int(x1), int(y1)-10),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0,255,0),
2
)
out.write(frame)
cap.release()
out.release()
print("Tracking completed!")
print("Saved as tracked_output.mp4")
from google.colab import files
files.download("tracked_output.mp4")