|
|
import cv2 |
|
|
import torch |
|
|
import numpy as np |
|
|
from torch import Tensor |
|
|
import torch.nn.functional as F |
|
|
|
|
|
from imantics import Mask |
|
|
from typing import List |
|
|
|
|
|
|
|
|
|
|
|
def convert_ann_to_mask(ann: List, height: int, width: int): |
|
|
mask = np.zeros((height, width), dtype=np.uint8) |
|
|
poly = ann["segmentation"] |
|
|
|
|
|
for p in poly: |
|
|
p = np.array(p).reshape(-1, 2).astype(int) |
|
|
cv2.fillPoly(mask, [p], 1) |
|
|
return mask |
|
|
|
|
|
|
|
|
def convert_mask_to_ann(mask: np.ndarray): |
|
|
polygons = Mask(mask).polygons() |
|
|
return polygons.segmentation |
|
|
|
|
|
|
|
|
|
|
|
def list_of_strings(arg): |
|
|
return [float(thr) for thr in arg.split(',')] |
|
|
|
|
|
|
|
|
def video_interpolation(video: Tensor, frame_sample_rate: int): |
|
|
expanded_heatmap = [] |
|
|
for i in range(len(video) - 1): |
|
|
pre_heatmap, post_heatmap = video[i], video[i + 1] |
|
|
|
|
|
for j in range(frame_sample_rate): |
|
|
interpolated_heatmap = ((frame_sample_rate - j) / frame_sample_rate) * pre_heatmap \ |
|
|
+ (j / frame_sample_rate) * post_heatmap |
|
|
expanded_heatmap.append(interpolated_heatmap) |
|
|
expanded_heatmap.append(video[-1]) |
|
|
return torch.stack(expanded_heatmap).unsqueeze(1) |
|
|
|
|
|
|
|
|
|
|
|
def heatmap_interpolation(heatmap: Tensor, height: int, width: int): |
|
|
return F.interpolate(heatmap, size=(height, width), mode='bilinear', align_corners=False).squeeze().numpy() |