-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_webcam_tracking.py
53 lines (40 loc) · 1.54 KB
/
example_webcam_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
import cv2
import torch
import numpy as np
import tapnet.utils as utils
from tapnet.tapir_inference import TapirInference
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if __name__ == '__main__':
input_size = 480
num_points = 900
num_iters = 4 # Use 1 for faster inference, and 4 for better results
# Initialize camera
cap = cv2.VideoCapture(0)
# Initialize model
tapir = TapirInference('models/causal_bootstapir_checkpoint.pt', (input_size, input_size), num_iters, device)
# Initialize query features
query_points = utils.sample_grid_points(input_size, input_size, num_points)
point_colors = utils.get_colors(num_points)
ret, frame = cap.read()
tapir.set_points(frame, query_points)
# Reset video to the beginning
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
track_length = 30
tracks = np.zeros((num_points, track_length, 2), dtype=object)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Run the model
points, visibles = tapir(frame)
# Record visible points [num_points, 0, 2]
tracks = np.roll(tracks, 1, axis=1)
tracks[visibles, 0] = points[visibles]
tracks[~visibles, 0] = -1
# Draw the results
frame = utils.draw_tracks(frame, tracks, point_colors)
frame = utils.draw_points(frame, points, visibles, point_colors)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break