-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
113 lines (96 loc) · 2.89 KB
/
demo.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
import random
import cv2
from deep_sort_realtime.deepsort_tracker import DeepSort
from ultralytics import YOLO
object_tracker = DeepSort(
n_init=3,
max_age=30,
nms_max_overlap=1.0,
max_cosine_distance=0.5,
max_iou_distance=0.5,
embedder="mobilenet",
# half=True,
bgr=True,
embedder_gpu=False,
nn_budget=None,
gating_only_position=True,
)
model = YOLO("./models/yolov8n.pt")
classes = model.names
colors = [
(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for j in range(10)
]
cap = cv2.VideoCapture(
"/Users/3i-a1-2021-15/Developer/projects/pivo-tracking/videos/2.mp4"
)
ret, image = cap.read()
cap_out = cv2.VideoWriter(
"./results/out2.mp4",
cv2.VideoWriter_fourcc(*"MP4V"),
cap.get(cv2.CAP_PROP_FPS),
(image.shape[1], image.shape[0]),
)
while ret:
results = model(image)
for result in results:
detections = []
boxes = result.boxes
for r in result.boxes.data.tolist():
x1, y1, x2, y2 = r[:4]
w, h = x2 - x1, y2 - y1
coordinates = list((int(x1), int(y1), int(w), int(h)))
conf = r[4]
clsId = int(r[5])
cls = classes[clsId]
if cls == "person" and conf > 0.7:
detections.append((coordinates, conf, cls))
print("detections: ", detections)
tracks = object_tracker.update_tracks(detections, frame=image)
for track in tracks:
if not track.is_confirmed():
continue
track_id = track.track_id
bbox = track.to_ltrb()
color = colors[int(track_id) % len(colors)]
cv2.rectangle(
image,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
color,
thickness=4,
)
display_text = "ID: " + str(track_id)
(text_width, text_height), _ = cv2.getTextSize(
display_text, cv2.FONT_HERSHEY_SIMPLEX, 0.75, 2
)
cv2.rectangle(
image,
(int(bbox[0]), int(bbox[1]) - 30),
(
int(bbox[0]) + int(text_width),
int(bbox[1]),
),
color,
-1,
)
text_x = int(bbox[0]) + 5
text_y = int(bbox[1]) + text_height + 5
cv2.putText(
image,
display_text,
(int(bbox[0]), int(bbox[1]) - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.75,
(255, 255, 255),
2,
)
cv2.namedWindow("Result", cv2.WINDOW_NORMAL)
cv2.imshow("Result", image)
cap_out.write(image)
ret, image = cap.read()
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cap_out.release()
cv2.destroyAllWindows()