-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tracker.py
More file actions
67 lines (48 loc) · 1.64 KB
/
Copy pathtest_tracker.py
File metadata and controls
67 lines (48 loc) · 1.64 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
#!/usr/bin/env python
import argparse
import os
import sys
import time
import cv2
import numpy as np
_CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(_CURRENT_DIR, "../"))
if True: # noqa F402
from mosse_tracker import MosseTracker
def get_args():
parser = argparse.ArgumentParser("")
parser.add_argument("--video_path", type=str, required=True)
parser.add_argument("--use_gpu", action="store_true")
return parser.parse_args()
def main():
args = get_args()
cap = cv2.VideoCapture(args.video_path)
shape = (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))
fps = cap.get(cv2.CAP_PROP_FPS)
ok, frame = cap.read()
if not ok:
print(f"cannot read: {args.video_path}")
sys.exit()
tracker = MosseTracker(img_shape=shape, use_gpu=args.use_gpu)
# bbox = cv2.selectROI(frame, False)
bbox = (210, 425, 122, 80)
if not tracker.init(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), bbox):
print("failed to initialize tracking")
sys.exit()
while cap.isOpened():
ok, frame = cap.read()
if not ok:
break
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
output_bbox = tracker.update(frame_gray)
if not output_bbox:
print("failed to track")
break
x_min, y_min, w, h = output_bbox
cv2.rectangle(frame, (x_min, y_min), (x_min + w, y_min + h), (255, 0, 0), 2, 1)
cv2.imshow("tracked result", frame)
k = cv2.waitKey(int(1000 / fps)) & 0xFF
if k == 27:
break
if __name__ == "__main__":
main()