Skip to content

Commit 81b4845

Browse files
committed
add timer and save video and modify readme
1 parent 9e45f02 commit 81b4845

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ runs/*
1010
wandb/*
1111
track_result.txt
1212
.idea/
13-
tracker/results/*
13+
track_results/*
1414
*.mp4
1515
*.mkv
1616
temp.py

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ Compared to the previous version, this is an ***entirely new version (branch v2)
88

99
```bash
1010
git clone https://github.com/JackWoo0831/Yolov7-tracker.git
11+
git checkout v2 # change to v2 branch !!
1112
```
1213

1314
🙌 ***If you have any suggestions for adding trackers***, please leave a comment in the Issues section with the paper title or link! Everyone is welcome to contribute to making this repo better.
1415

16+
<div align="center">
1517

18+
**Language**: English | [简体中文](README_CN.md)
19+
20+
</div>
1621

1722
## ❤️ Introduction
1823

@@ -47,8 +52,8 @@ The highlights are:
4752
## 🗺️ Roadmap
4853

4954
- [ ] Add StrongSort
50-
- [ ] Add save video function
51-
- [ ] Add timer function to calculate fps
55+
- [ x ] Add save video function
56+
- [ x ] Add timer function to calculate fps
5257

5358
## 🔨 Installation
5459

README_CN.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
```bash
1010
git clone https://github.com/JackWoo0831/Yolov7-tracker.git
11+
git checkout v2 # change to v2 branch !!
1112
```
1213

1314
🙌 ***如果您有任何关于添加跟踪器的建议***,请在Issues部分留言并附上论文标题或链接!欢迎大家一起来让这个repo变得更好
@@ -47,8 +48,8 @@ REID模型支持:
4748
## 🗺️ 路线图
4849

4950
- [ ] Add StrongSort
50-
- [ ] Add save video function
51-
- [ ] Add timer function to calculate fps
51+
- [ x ] Add save video function
52+
- [ x ] Add timer function to calculate fps
5253

5354
## 🔨 安装
5455

File renamed without changes.

tracker/track.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
from tracking_utils.envs import select_device
1616
from tracking_utils.tools import *
17-
from tracking_utils.visualization import plot_img
17+
from tracking_utils.visualization import plot_img, save_video
18+
from my_timer import Timer
1819

1920
from tracker_dataloader import TestDataset
2021

@@ -187,6 +188,11 @@ def main(args, dataset_cfgs):
187188

188189

189190
"""4. Tracking"""
191+
192+
# set timer
193+
timer = Timer()
194+
seq_fps = []
195+
190196
for seq in seqs:
191197
logger.info(f'--------------tracking seq {seq}--------------')
192198

@@ -202,6 +208,10 @@ def main(args, dataset_cfgs):
202208
results = []
203209

204210
for frame_idx, (ori_img, img) in process_bar:
211+
212+
# start timing this frame
213+
timer.tic()
214+
205215
if args.detector == 'yolov8':
206216
img = img.squeeze(0).cpu().numpy()
207217

@@ -257,13 +267,28 @@ def main(args, dataset_cfgs):
257267

258268
results.append((frame_idx + 1, cur_id, cur_tlwh, cur_cls, cur_score))
259269

270+
timer.toc()
271+
260272
if args.save_images:
261273
plot_img(img=ori_img, frame_id=frame_idx, results=[cur_tlwh, cur_id, cur_cls],
262274
save_dir=os.path.join(save_dir, 'vis_results'))
263275

264-
save_results(folder_name=os.path.join(args.datasets, SPLIT),
276+
save_results(folder_name=os.path.join(args.dataset, SPLIT),
265277
seq_name=seq,
266278
results=results)
279+
280+
# show the fps
281+
seq_fps.append(frame_idx / timer.total_time)
282+
logger.info(f'fps of seq {seq}: {seq_fps[-1]}')
283+
timer.clear()
284+
285+
if args.save_videos:
286+
save_video(images_path=os.path.join(save_dir, 'vis_results'))
287+
logger.info(f'save video of {seq} done')
288+
289+
# show the average fps
290+
logger.info(f'average fps: {np.mean(seq_fps)}')
291+
267292

268293
if __name__ == '__main__':
269294

tracker/track_demo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from tracking_utils.envs import select_device
1616
from tracking_utils.tools import *
17-
from tracking_utils.visualization import plot_img
17+
from tracking_utils.visualization import plot_img, save_video
1818

1919
from tracker_dataloader import TestDataset, DemoDataset
2020

@@ -254,6 +254,10 @@ def main(args):
254254
save_results(folder_name=os.path.join(save_dir, 'txt_results'),
255255
seq_name='demo',
256256
results=results)
257+
258+
if args.save_videos:
259+
save_video(images_path=os.path.join(save_dir, 'vis_results'))
260+
logger.info(f'save video done')
257261

258262
if __name__ == '__main__':
259263

tracker/tracking_utils/visualization.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cv2
22
import os
33
import numpy as np
4+
from PIL import Image
45

56
def plot_img(img, frame_id, results, save_dir):
67
"""
@@ -43,4 +44,21 @@ def get_color(idx):
4344
idx = idx * 3
4445
color = ((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255)
4546

46-
return color
47+
return color
48+
49+
def save_video(images_path):
50+
"""
51+
save images (frames) to a video
52+
"""
53+
54+
images_list = sorted(os.listdir(images_path))
55+
save_video_path = os.path.join(images_path, images_path.split('/')[-1] + '.mp4')
56+
57+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
58+
59+
img0 = Image.open(os.path.join(images_path, images_list[0]))
60+
vw = cv2.VideoWriter(save_video_path, fourcc, 15, img0.size)
61+
62+
for image_name in images_list:
63+
image = cv2.imread(filename=os.path.join(images_path, image_name))
64+
vw.write(image)

0 commit comments

Comments
 (0)