-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobject_tracking.py
214 lines (140 loc) · 7.19 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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().system('python --version')
# In[2]:
import ultralytics
ultralytics.__version__
# In[4]:
import torch
torch.__version__
# In[5]:
torch.cuda.get_device_name(0)
# # Detect, track and count Persons
# In[1]:
get_ipython().run_line_magic('cd', 'yolov8_DeepSORT')
# In[2]:
from ultralytics import YOLO
import time
import torch
import cv2
import torch.backends.cudnn as cudnn
from PIL import Image
import colorsys
import numpy as np
# Load a model
model = YOLO("/iccp/jenish/Object_Tracking/runs/segment/train/weights/best.pt") # load a pretrained model (recommended for training)
results = model("images/person.jpg", save=True)
class_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
for result in results:
boxes = result.boxes # Boxes object for bbox outputs
probs = result.probs # Class probabilities for classification outputs
cls = boxes.cls.tolist() # Convert tensor to list
xyxy = boxes.xyxy
xywh = boxes.xywh # box with xywh format, (N, 4)
conf = boxes.conf
print(cls)
for class_index in cls:
class_name = class_names[int(class_index)]
print("Class:", class_name)
# # DeepSORT
# In[3]:
from deep_sort.utils.parser import get_config
from deep_sort.deep_sort import DeepSort
from deep_sort.sort.tracker import Tracker
deep_sort_weights = 'deep_sort/deep/checkpoint/ckpt.t7'
tracker = DeepSort(model_path=deep_sort_weights, max_age=70)
# In[4]:
# Define the video path
video_path = 'test_videos/3.mp4'
cap = cv2.VideoCapture(video_path)
# Get the video properties
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_path = 'output.mp4'
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# In[5]:
frames = []
unique_track_ids = set()
# In[ ]:
model = YOLO("/iccp/jenish/Object_Tracking/runs/segment/train/weights/best.pt") # load a pretrained model (recommended for training)
i = 0
counter, fps, elapsed = 0, 0, 0
start_time = time.perf_counter()
while cap.isOpened():
ret, frame = cap.read()
if ret:
og_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = og_frame.copy()
# model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
results = model(frame, device=0, classes=0, conf=0.8)
class_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
for result in results:
boxes = result.boxes # Boxes object for bbox outputs
probs = result.probs # Class probabilities for classification outputs
cls = boxes.cls.tolist() # Convert tensor to list
xyxy = boxes.xyxy
conf = boxes.conf
xywh = boxes.xywh # box with xywh format, (N, 4)
for class_index in cls:
class_name = class_names[int(class_index)]
#print("Class:", class_name)
pred_cls = np.array(cls)
conf = conf.detach().cpu().numpy()
xyxy = xyxy.detach().cpu().numpy()
bboxes_xywh = xywh
bboxes_xywh = xywh.cpu().numpy()
bboxes_xywh = np.array(bboxes_xywh, dtype=float)
print("Coordinates: ", bboxes_xywh)
tracks = tracker.update(bboxes_xywh, conf, og_frame)
for track in tracker.tracker.tracks:
track_id = track.track_id
hits = track.hits
x1, y1, x2, y2 = track.to_tlbr() # Get bounding box coordinates in (x1, y1, x2, y2) format
w = x2 - x1 # Calculate width
h = y2 - y1 # Calculate height
print("Coordinates in tracking: ",[x1, y1, w, h])
# Set color values for red, blue, and green
red_color = (0, 0, 255) # (B, G, R)
blue_color = (255, 0, 0) # (B, G, R)
green_color = (0, 255, 0) # (B, G, R)
# Determine color based on track_id
color_id = track_id % 3
if color_id == 0:
color = red_color
elif color_id == 1:
color = blue_color
else:
color = green_color
cv2.rectangle(og_frame, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), color, 2)
text_color = (0, 0, 0) # Black color for text
cv2.putText(og_frame, f"Pig-{track_id}", (int(x1) + 10, int(y1) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)
# Add the track_id to the set of unique track IDs
unique_track_ids.add(track_id)
# Update the person count based on the number of unique track IDs
person_count = len(unique_track_ids)
# Update FPS and place on frame
current_time = time.perf_counter()
elapsed = (current_time - start_time)
counter += 1
if elapsed > 1:
fps = counter / elapsed
counter = 0
start_time = current_time
# Draw person count on frame
cv2.putText(og_frame, f"Pig Count: {person_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Append the frame to the list
frames.append(og_frame)
# Write the frame to the output video file
out.write(cv2.cvtColor(og_frame, cv2.COLOR_RGB2BGR))
# Show the frame
#cv2.imshow("Video", og_frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
cap.release()
out.release()
cv2.destroyAllWindows()