-
Notifications
You must be signed in to change notification settings - Fork 1
/
lite_deepsort_solutions_demo.py
134 lines (100 loc) · 3.74 KB
/
lite_deepsort_solutions_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from deep_sort.detection import Detection
from application_util import preprocessing
from deep_sort.tracker import Tracker
from deep_sort import nn_matching
from ultralytics import YOLO
import numpy as np
import cv2
from opts import opt
import os
from ultralytics import YOLO, solutions
def process_video(video_path):
# Open the video file or camera
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error: Could not open video.")
return
model = YOLO("yolov8m.pt")
print(model.info(verbose=True))
nms_max_overlap = 1.0
metric = nn_matching.NearestNeighborDistanceMetric(
'cosine',
0.3,
100
)
tracker = Tracker(metric)
frame_number = 0 # Initialize frame number
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Define region points
# region_points = [(750, 700), (1200, 700), (1250, 400), (1150, 300)]
region_points = [(850, 700), (1250, 300)]
# Path to json file, that created with above point selection app
polygon_json_path = "videos/bounding_boxes.json"
output_dir = "demo_output_videos"
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"{opt.solution}.mp4")
video_writer = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
counter = solutions.ObjectCounter(
view_img=True,
reg_pts=region_points,
names=model.names,
draw_tracks=True,
line_thickness=2,
)
heatmap_obj = solutions.Heatmap(
colormap=cv2.COLORMAP_JET,
view_img=True,
shape="circle",
names=model.names,
)
management = solutions.ParkingManagement(
model_path="yolov8m.pt"
)
while True:
ret, frame = cap.read()
if not ret:
break
frame_number += 1
# Process each frame
classes = [2] if opt.solution == "parking_management" else [0]
yolo_results = model.predict(
frame, classes=classes, verbose=False, imgsz=1280, appearance_feature_layer='layer0', conf=.25)
boxes = yolo_results[0].boxes.data.cpu().numpy()
appearance_features = yolo_results[0].appearance_features.cpu().numpy()
detections = []
for box, feature in zip(boxes, appearance_features):
xmin, ymin, xmax, ymax, conf, _ = box
x_tl = xmin
y_tl = ymin
width = xmax - xmin
height = ymax - ymin
bbox = (x_tl, y_tl, width, height)
detection = Detection(bbox, conf, feature)
detections.append(detection)
# Run non-maxima suppression.
boxes = np.array([d.tlwh for d in detections])
scores = np.array([d.confidence for d in detections])
indices = preprocessing.non_max_suppression(
boxes, nms_max_overlap, scores)
detections = [detections[i] for i in indices]
tracker.predict()
tracker.update(detections)
if opt.solution == "object_counter":
frame = counter.start_counting(frame, tracker.tracks)
elif opt.solution == "heatmap":
frame = heatmap_obj.generate_heatmap(frame, tracker.tracks)
elif opt.solution == "parking_management":
json_data = management.parking_regions_extraction(polygon_json_path)
management.process_data(json_data, frame, tracker.tracks)
management.display_frames(frame)
video_writer.write(frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
video_writer.release()
cv2.destroyAllWindows()
def main():
process_video(opt.source)
if __name__ == "__main__":
main()