forked from google-deepmind/tapnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_demo.py
221 lines (180 loc) · 6.21 KB
/
live_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
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
214
215
216
217
218
219
220
221
# Copyright 2024 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Live Demo for Online TAPIR."""
import time
import cv2
import jax
import jax.numpy as jnp
import numpy as np
from tapnet import tapir_model
from tapnet.utils import model_utils
NUM_POINTS = 8
def load_checkpoint(checkpoint_path):
ckpt_state = np.load(checkpoint_path, allow_pickle=True).item()
return ckpt_state["params"], ckpt_state["state"]
print("Loading checkpoint...")
# --------------------
# Load checkpoint and initialize
params, state = load_checkpoint(
"tapnet/checkpoints/causal_tapir_checkpoint.npy"
)
tapir = tapir_model.ParameterizedTAPIR(
params=params,
state=state,
tapir_kwargs=dict(
use_causal_conv=True, bilinear_interp_with_depthwise_conv=False
),
)
def online_model_init(frames, points):
feature_grids = tapir.get_feature_grids(frames, is_training=False)
features = tapir.get_query_features(
frames,
is_training=False,
query_points=points,
feature_grids=feature_grids,
)
return features
def online_model_predict(frames, features, causal_context):
"""Compute point tracks and occlusions given frames and query points."""
feature_grids = tapir.get_feature_grids(frames, is_training=False)
trajectories = tapir.estimate_trajectories(
frames.shape[-3:-1],
is_training=False,
feature_grids=feature_grids,
query_features=features,
query_points_in_video=None,
query_chunk_size=64,
causal_context=causal_context,
get_causal_context=True,
)
causal_context = trajectories["causal_context"]
del trajectories["causal_context"]
return {k: v[-1] for k, v in trajectories.items()}, causal_context
def get_frame(video_capture):
r_val, image = video_capture.read()
trunc = np.abs(image.shape[1] - image.shape[0]) // 2
if image.shape[1] > image.shape[0]:
image = image[:, trunc:-trunc]
elif image.shape[1] < image.shape[0]:
image = image[trunc:-trunc]
return r_val, image
print("Welcome to the TAPIR live demo.")
print("Please note that if the framerate is low (<~12 fps), TAPIR performance")
print("may degrade and you may need a more powerful GPU.")
print("Creating model...")
online_init_apply = jax.jit(online_model_init)
online_predict_apply = jax.jit(online_model_predict)
print("Initializing camera...")
# --------------------
# Start point tracking
vc = cv2.VideoCapture(0)
vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
if vc.isOpened(): # try to get the first frame
rval, frame = get_frame(vc)
else:
raise ValueError("Unable to open camera.")
pos = tuple()
query_frame = True
have_point = [False] * NUM_POINTS
query_features = None
causal_state = None
next_query_idx = 0
print("Compiling jax functions (this may take a while...)")
# --------------------
# Call one time to compile
query_points = jnp.zeros([NUM_POINTS, 3], dtype=jnp.float32)
_ = online_init_apply(
frames=model_utils.preprocess_frames(frame[None, None]),
points=query_points[None, 0:1],
)
jax.block_until_ready(query_features)
query_features = online_init_apply(
frames=model_utils.preprocess_frames(frame[None, None]),
points=query_points[None, :],
)
causal_state = tapir.construct_initial_causal_state(
NUM_POINTS, len(query_features.resolutions) - 1
)
prediction, causal_state = online_predict_apply(
frames=model_utils.preprocess_frames(frame[None, None]),
features=query_features,
causal_context=causal_state,
)
jax.block_until_ready(prediction["tracks"])
last_click_time = 0
def mouse_click(event, x, y, flags, param):
del flags, param
global pos, query_frame, last_click_time
# event fires multiple times per click sometimes??
if (time.time() - last_click_time) < 0.5:
return
if event == cv2.EVENT_LBUTTONDOWN:
pos = (y, frame.shape[1] - x)
query_frame = True
last_click_time = time.time()
cv2.namedWindow("Point Tracking")
cv2.setMouseCallback("Point Tracking", mouse_click)
t = time.time()
step_counter = 0
print("Press ESC to exit.")
while rval:
rval, frame = get_frame(vc)
if query_frame:
query_points = jnp.array((0,) + pos, dtype=jnp.float32)
init_query_features = online_init_apply(
frames=model_utils.preprocess_frames(frame[None, None]),
points=query_points[None, None],
)
query_frame = False
query_features, causal_state = tapir.update_query_features(
query_features=query_features,
new_query_features=init_query_features,
idx_to_update=np.array([next_query_idx]),
causal_state=causal_state,
)
have_point[next_query_idx] = True
next_query_idx = (next_query_idx + 1) % NUM_POINTS
if pos:
prediction, causal_state = online_predict_apply(
frames=model_utils.preprocess_frames(frame[None, None]),
features=query_features,
causal_context=causal_state,
)
track = prediction["tracks"][0, :, 0]
occlusion = prediction["occlusion"][0, :, 0]
expected_dist = prediction["expected_dist"][0, :, 0]
visibles = model_utils.postprocess_occlusions(occlusion, expected_dist)
track = np.round(track)
for i, _ in enumerate(have_point):
if visibles[i] and have_point[i]:
cv2.circle(
frame, (int(track[i, 0]), int(track[i, 1])), 5, (255, 0, 0), -1
)
if track[i, 0] < 16 and track[i, 1] < 16:
print((i, next_query_idx))
cv2.imshow("Point Tracking", frame[:, ::-1])
if pos:
step_counter += 1
if time.time() - t > 5:
print(f"{step_counter/(time.time()-t)} frames per second")
t = time.time()
step_counter = 0
else:
t = time.time()
key = cv2.waitKey(1)
if key == 27: # exit on ESC
break
cv2.destroyWindow("Point Tracking")
vc.release()