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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.

# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

import warnings
import numpy as np
import cv2

import torch
import torch.nn.functional as F

from cotracker.models.core.model_utils import smart_cat, get_points_on_a_grid
from cotracker.models.build_cotracker import build_cotracker


def gen_gaussian_heatmap(imgSize=200):
    circle_img = np.zeros((imgSize, imgSize), np.float32)
    circle_mask = cv2.circle(circle_img, (imgSize//2, imgSize//2), imgSize//2, 1, -1)

    isotropicGrayscaleImage = np.zeros((imgSize, imgSize), np.float32)

    # Guass Map
    for i in range(imgSize):
        for j in range(imgSize):
            isotropicGrayscaleImage[i, j] = 1 / 2 / np.pi / (40 ** 2) * np.exp(
                -1 / 2 * ((i - imgSize / 2) ** 2 / (40 ** 2) + (j - imgSize / 2) ** 2 / (40 ** 2)))

    isotropicGrayscaleImage = isotropicGrayscaleImage * circle_mask
    isotropicGrayscaleImage = (isotropicGrayscaleImage / np.max(isotropicGrayscaleImage)).astype(np.float32)
    isotropicGrayscaleImage = (isotropicGrayscaleImage / np.max(isotropicGrayscaleImage)*255).astype(np.uint8)

    # isotropicGrayscaleImage = cv2.resize(isotropicGrayscaleImage, (40, 40))
    return isotropicGrayscaleImage


def draw_heatmap(img, center_coordinate, heatmap_template, side, width, height):
    x1 = max(center_coordinate[0] - side, 1)
    x2 = min(center_coordinate[0] + side, width - 1)
    y1 = max(center_coordinate[1] - side, 1)
    y2 = min(center_coordinate[1] + side, height - 1)
    x1, x2, y1, y2 = int(x1), int(x2), int(y1), int(y2)

    if (x2 - x1) < 1 or (y2 - y1) < 1:
        print(center_coordinate, "x1, x2, y1, y2", x1, x2, y1, y2)
        return img

    need_map = cv2.resize(heatmap_template, (x2-x1, y2-y1))

    img[y1:y2,x1:x2] = need_map

    return img


def generate_gassian_heatmap(pred_tracks, pred_visibility=None, image_size=None, side=20):
    width, height = image_size
    num_frames, num_points = pred_tracks.shape[:2]

    point_index_list = [point_idx for point_idx in range(num_points)]
    heatmap_template = gen_gaussian_heatmap()


    image_list = []
    for frame_idx in range(num_frames):
        
        img = np.zeros((height, width), np.float32)
        for point_idx in point_index_list:
            px, py = pred_tracks[frame_idx, point_idx]

            if px < 0 or py < 0 or px >= width or py >= height:
                continue

            if pred_visibility is not None:
                if (not pred_visibility[frame_idx, point_idx]):
                    continue

            img = draw_heatmap(img, (px, py), heatmap_template, side, width, height)

        img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_GRAY2RGB)
        img = torch.from_numpy(img).permute(2, 0, 1).contiguous()
        image_list.append(img)
    
    video_gaussion_map = torch.stack(image_list, dim=0)

    return video_gaussion_map


# TODO: need further check and investigation
def sample_trajectories(
        pred_tracks, pred_visibility, 
        max_points=10,
        motion_threshold=1,
        vis_threshold=5,

    ):
    # pred_tracks: (b, f, num_points, 2)
    # pred_visibility: (b, f, num_points)
    batch_size, num_frames, num_points = pred_visibility.shape

    # 1. Remove points with low visibility
    mask = pred_visibility.sum(dim=1) > vis_threshold
    mask = mask.unsqueeze(1).repeat(1, num_frames, 1)
    pred_tracks = pred_tracks[mask].view(batch_size, num_frames, -1, 2)
    pred_visibility = pred_visibility[mask].view(batch_size, num_frames, -1)

    # 2. Thresholding: remove points with too small motions
    # compute the motion of each point
    diff = pred_tracks[:, 1:] - pred_tracks[:, :-1]
    # (b, f-1, num_points), sqrt(x^2 + y^2)
    motion = torch.norm(diff, dim=-1)
    # (b, num_points), mean motion for each point
    motion = torch.mean(motion, dim=1)
    # apply threshold
    mask = motion > motion_threshold  # (b, num_points)
    assert mask.shape[0] == 1
    num_keeped = mask.sum()
    if num_keeped < max_points:
        indices = torch.argsort(motion, dim=-1, descending=True)[:, :max_points]  # (bs, max_points)
        mask = torch.zeros_like(mask)  # (bs, num_points)
        # set mask to 1 for the top max_points
        mask[0, indices] = 1
        num_keeped = mask.sum()  # note sometimes mask.sum() < max_points

    motion = motion[mask].view(batch_size, num_keeped)
    # keep shape
    mask = mask.unsqueeze(1).repeat(1, num_frames, 1)
    pred_tracks = pred_tracks[mask].view(batch_size, num_frames, num_keeped, 2)
    pred_visibility = pred_visibility[mask].view(batch_size, num_frames, num_keeped)


    # 3. Sampling with larger prob for large motions
    num_points = min(max_points, num_keeped)
    if num_points == 0:
        warnings.warn("No points left after filtering")
        return None, None

    prob = motion / motion.max()
    prob = prob / prob.sum()
    sampled_indices = torch.multinomial(prob, num_points, replacement=False)

    sampled_indices = sampled_indices.squeeze(0)  # (num_points, )
    pred_tracks_sampled = pred_tracks[:, :, sampled_indices]
    pred_visibility_sampled = pred_visibility[:, :, sampled_indices]

    return pred_tracks_sampled, pred_visibility_sampled
def sample_trajectories_with_ref(
        pred_tracks, pred_visibility, coords0,
        max_points=10,
        motion_threshold=1,
        vis_threshold=5,
    ):

 

    batch_size, num_frames, num_points = pred_visibility.shape


    visibility_sum = pred_visibility.sum(dim=1)
    vis_mask = visibility_sum > vis_threshold  # (batch_size, num_points)



    pred_tracks = pred_tracks * vis_mask.unsqueeze(1).unsqueeze(-1)  # (batch_size, num_frames, num_points, 2)
    pred_visibility = pred_visibility * vis_mask.unsqueeze(1)

   
    indices = vis_mask.nonzero(as_tuple=False)  # (num_visible_points, 2)
    if indices.size(0) == 0:
        warnings.warn("No points left after visibility filtering")
        return None, None, None

    batch_indices, point_indices = indices[:, 0], indices[:, 1]

    coords0_filtered = coords0[batch_indices, point_indices]  # (num_visible_points, 2)


    diff = pred_tracks[:, 1:] - pred_tracks[:, :-1]  # (batch_size, num_frames-1, num_points, 2)
    motion = torch.norm(diff, dim=-1).mean(dim=1)  # (batch_size, num_points)

    motion_mask = motion > motion_threshold
    combined_mask = vis_mask & motion_mask  # (batch_size, num_points)


    indices = combined_mask.nonzero(as_tuple=False)
    if indices.size(0) == 0:
        warnings.warn("No points left after motion filtering")
        return None, None, None

    batch_indices, point_indices = indices[:, 0], indices[:, 1]

    pred_tracks_filtered = pred_tracks[batch_indices, :, point_indices, :]  # (num_filtered_points, num_frames, 2)
    pred_visibility_filtered = pred_visibility[batch_indices, :, point_indices]  # (num_filtered_points, num_frames)
    coords0_filtered = coords0[batch_indices, point_indices, :]  # (num_filtered_points, 2)
    motion_filtered = motion[batch_indices, point_indices]  # (num_filtered_points)


    num_keeped = motion_filtered.size(0)
    num_points_sampled = min(max_points, num_keeped)
    if num_points_sampled == 0:
        warnings.warn("No points left after filtering")
        return None, None, None

    prob = motion_filtered / motion_filtered.max()
    prob = prob / prob.sum()
    sampled_indices = torch.multinomial(prob, num_points_sampled, replacement=False)

    pred_tracks_sampled = pred_tracks_filtered[sampled_indices]  # (num_points_sampled, num_frames, 2)
    pred_visibility_sampled = pred_visibility_filtered[sampled_indices]  # (num_points_sampled, num_frames)
    coords0_sampled = coords0_filtered[sampled_indices]  # (num_points_sampled, 2)


    pred_tracks_sampled = pred_tracks_sampled.view(batch_size, num_points_sampled, num_frames, 2).transpose(1, 2)
    pred_visibility_sampled = pred_visibility_sampled.view(batch_size, num_points_sampled, num_frames).transpose(1, 2)
    coords0_sampled = coords0_sampled.view(batch_size, num_points_sampled, 2)

    return pred_tracks_sampled, pred_visibility_sampled, coords0_sampled


class CoTrackerPredictor(torch.nn.Module):
    def __init__(
            self,
            checkpoint="./checkpoints/cotracker2.pth",
            shift_grid=False,
        ):

        super().__init__()
        self.support_grid_size = 6
        model = build_cotracker(checkpoint)
        self.interp_shape = model.model_resolution
        self.model = model
        self.model.eval()
        self.shift_grid = shift_grid

    @torch.no_grad()
    def forward(
        self,
        video,  # (B, T, 3, H, W)
        # input prompt types:
        # - None. Dense tracks are computed in this case. You can adjust *query_frame* to compute tracks starting from a specific frame.
        # *backward_tracking=True* will compute tracks in both directions.
        # - queries. Queried points of shape (B, N, 3) in format (t, x, y) for frame index and pixel coordinates.
        # - grid_size. Grid of N*N points from the first frame. if segm_mask is provided, then computed only for the mask.
        # You can adjust *query_frame* and *backward_tracking* for the regular grid in the same way as for dense tracks.
        queries: torch.Tensor = None,
        segm_mask: torch.Tensor = None,  # Segmentation mask of shape (B, 1, H, W)
        grid_size: int = 0,
        grid_query_frame: int = 0,  # only for dense and regular grid tracks
        backward_tracking: bool = False,
    ):
        if queries is None and grid_size == 0:
            tracks, visibilities = self._compute_dense_tracks(
                video,
                grid_query_frame=grid_query_frame,
                backward_tracking=backward_tracking,
            )
        else:
            tracks, visibilities = self._compute_sparse_tracks(
                video,
                queries,
                segm_mask,
                grid_size,
                add_support_grid=(grid_size == 0 or segm_mask is not None),
                grid_query_frame=grid_query_frame,
                backward_tracking=backward_tracking,
            )

        return tracks, visibilities

    def _compute_dense_tracks(self, video, grid_query_frame, grid_size=80, backward_tracking=False):
        *_, H, W = video.shape
        grid_step = W // grid_size
        grid_width = W // grid_step
        grid_height = H // grid_step
        tracks = visibilities = None
        grid_pts = torch.zeros((1, grid_width * grid_height, 3)).to(video.device)
        grid_pts[0, :, 0] = grid_query_frame
        for offset in range(grid_step * grid_step):
            print(f"step {offset} / {grid_step * grid_step}")
            ox = offset % grid_step
            oy = offset // grid_step
            grid_pts[0, :, 1] = torch.arange(grid_width).repeat(grid_height) * grid_step + ox
            grid_pts[0, :, 2] = (
                torch.arange(grid_height).repeat_interleave(grid_width) * grid_step + oy
            )
            tracks_step, visibilities_step = self._compute_sparse_tracks(
                video=video,
                queries=grid_pts,
                backward_tracking=backward_tracking,
            )
            tracks = smart_cat(tracks, tracks_step, dim=2)
            visibilities = smart_cat(visibilities, visibilities_step, dim=2)

        return tracks, visibilities

    def _compute_sparse_tracks(
        self,
        video,
        queries,
        segm_mask=None,
        grid_size=0,
        add_support_grid=False,
        grid_query_frame=0,
        backward_tracking=False,
    ):
        B, T, C, H, W = video.shape

        video = video.reshape(B * T, C, H, W)
        video = F.interpolate(video, tuple(self.interp_shape), mode="bilinear", align_corners=True)
        video = video.reshape(B, T, 3, self.interp_shape[0], self.interp_shape[1])

        if queries is not None:
            B, N, D = queries.shape
            assert D == 3
            queries = queries.clone()
            queries[:, :, 1:] *= queries.new_tensor(
                [
                    (self.interp_shape[1] - 1) / (W - 1),
                    (self.interp_shape[0] - 1) / (H - 1),
                ]
            )
        elif grid_size > 0:
            grid_pts = get_points_on_a_grid(grid_size, self.interp_shape, device=video.device, shift_grid=self.shift_grid)
            if segm_mask is not None:
                segm_mask = F.interpolate(segm_mask, tuple(self.interp_shape), mode="nearest")
                point_mask = segm_mask[0, 0][
                    (grid_pts[0, :, 1]).round().long().cpu(),
                    (grid_pts[0, :, 0]).round().long().cpu(),
                ].bool()
                grid_pts = grid_pts[:, point_mask]

            queries = torch.cat(
                [torch.ones_like(grid_pts[:, :, :1]) * grid_query_frame, grid_pts],
                dim=2,
            ).repeat(B, 1, 1)

        if add_support_grid:
            grid_pts = get_points_on_a_grid(
                self.support_grid_size, self.interp_shape, device=video.device, shift_grid=self.shift_grid,
            )
            grid_pts = torch.cat([torch.zeros_like(grid_pts[:, :, :1]), grid_pts], dim=2)
            grid_pts = grid_pts.repeat(B, 1, 1)
            queries = torch.cat([queries, grid_pts], dim=1)

        tracks, visibilities, __ = self.model.forward(video=video, queries=queries, iters=6)

        if backward_tracking:
            tracks, visibilities = self._compute_backward_tracks(
                video, queries, tracks, visibilities
            )
            if add_support_grid:
                queries[:, -self.support_grid_size**2 :, 0] = T - 1
        if add_support_grid:
            tracks = tracks[:, :, : -self.support_grid_size**2]
            visibilities = visibilities[:, :, : -self.support_grid_size**2]
        thr = 0.9
        visibilities = visibilities > thr

        # correct query-point predictions
        # see https://github.com/facebookresearch/co-tracker/issues/28

        # TODO: batchify
        for i in range(len(queries)):
            queries_t = queries[i, : tracks.size(2), 0].to(torch.int64)
            arange = torch.arange(0, len(queries_t))

            # overwrite the predictions with the query points
            tracks[i, queries_t, arange] = queries[i, : tracks.size(2), 1:]

            # correct visibilities, the query points should be visible
            visibilities[i, queries_t, arange] = True

        tracks *= tracks.new_tensor(
            [(W - 1) / (self.interp_shape[1] - 1), (H - 1) / (self.interp_shape[0] - 1)]
        )
        return tracks, visibilities

    def _compute_backward_tracks(self, video, queries, tracks, visibilities):
        inv_video = video.flip(1).clone()
        inv_queries = queries.clone()
        inv_queries[:, :, 0] = inv_video.shape[1] - inv_queries[:, :, 0] - 1

        inv_tracks, inv_visibilities, __ = self.model(video=inv_video, queries=inv_queries, iters=6)

        inv_tracks = inv_tracks.flip(1)
        inv_visibilities = inv_visibilities.flip(1)
        arange = torch.arange(video.shape[1], device=queries.device)[None, :, None]

        mask = (arange < queries[:, None, :, 0]).unsqueeze(-1).repeat(1, 1, 1, 2)

        tracks[mask] = inv_tracks[mask]
        visibilities[mask[:, :, :, 0]] = inv_visibilities[mask[:, :, :, 0]]
        return tracks, visibilities


class CoTrackerOnlinePredictor(torch.nn.Module):
    def __init__(self, checkpoint="./checkpoints/cotracker2.pth"):
        super().__init__()
        self.support_grid_size = 6
        model = build_cotracker(checkpoint)
        self.interp_shape = model.model_resolution
        self.step = model.window_len // 2
        self.model = model
        self.model.eval()

    @torch.no_grad()
    def forward(
        self,
        video_chunk,
        is_first_step: bool = False,
        queries: torch.Tensor = None,
        grid_size: int = 10,
        grid_query_frame: int = 0,
        add_support_grid=False,
    ):
        B, T, C, H, W = video_chunk.shape
        # Initialize online video processing and save queried points
        # This needs to be done before processing *each new video*
        if is_first_step:
            self.model.init_video_online_processing()
            if queries is not None:
                B, N, D = queries.shape
                assert D == 3
                queries = queries.clone()
                queries[:, :, 1:] *= queries.new_tensor(
                    [
                        (self.interp_shape[1] - 1) / (W - 1),
                        (self.interp_shape[0] - 1) / (H - 1),
                    ]
                )
            elif grid_size > 0:
                grid_pts = get_points_on_a_grid(
                    grid_size, self.interp_shape, device=video_chunk.device
                )
                queries = torch.cat(
                    [torch.ones_like(grid_pts[:, :, :1]) * grid_query_frame, grid_pts],
                    dim=2,
                )
            if add_support_grid:
                grid_pts = get_points_on_a_grid(
                    self.support_grid_size, self.interp_shape, device=video_chunk.device
                )
                grid_pts = torch.cat([torch.zeros_like(grid_pts[:, :, :1]), grid_pts], dim=2)
                queries = torch.cat([queries, grid_pts], dim=1)
            self.queries = queries
            return (None, None)

        video_chunk = video_chunk.reshape(B * T, C, H, W)
        video_chunk = F.interpolate(
            video_chunk, tuple(self.interp_shape), mode="bilinear", align_corners=True
        )
        video_chunk = video_chunk.reshape(B, T, 3, self.interp_shape[0], self.interp_shape[1])

        tracks, visibilities, __ = self.model(
            video=video_chunk,
            queries=self.queries,
            iters=6,
            is_online=True,
        )
        thr = 0.9
        return (
            tracks
            * tracks.new_tensor(
                [
                    (W - 1) / (self.interp_shape[1] - 1),
                    (H - 1) / (self.interp_shape[0] - 1),
                ]
            ),
            visibilities > thr,
        )