【问题标题】:Preprocessing a video in android for pytorch在 android 中为 pytorch 预处理视频
【发布时间】:2021-05-04 21:24:49
【问题描述】:

在 Android Kotlin 中预处理视频数据以准备输入 PyTorch Android 模型的最佳方法是什么?具体来说,我在 PyTorch 中有一个现成的模型,我已经将它转换为准备好用于PyTorch Mobile

在训练期间,模型从手机中获取原始素材并进行预处理,以 (1) 为灰度,(2) 压缩为我指定的特定较小分辨率,(3) 转换为张量以输入神经网络(或可能将压缩视频发送到远程服务器)。我为此使用 OpenCV,但我想知道在 Android Kotlin 中最简单的方法是什么?

Python代码供参考:


def save_video(filename):

    frames = []

    cap = cv2.VideoCapture(filename)
    frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    buf_c = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))
    buf = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))

    fc = 0
    ret = True

    # 9:16 ratio
    width = 121
    height = 216
    dim = (width, height)

    # Loop until the end of the video
    while fc < frameCount and ret:
        ret, buf_c[fc] = cap.read()

        # convert to greyscale
        buf[fc] = cv2.cvtColor(buf_c[fc], cv2.COLOR_BGR2GRAY)

        # reduce resolution
        resized = cv2.resize(buf[fc], dim, interpolation = cv2.INTER_AREA)

        frames.append(resized)
        fc += 1

    # release the video capture object
    cap.release()

    # Closes all the windows currently opened.
    cv2.destroyAllWindows()

    return frames

【问题讨论】:

    标签: android kotlin computer-vision pytorch video-processing


    【解决方案1】:

    您说您的模型已转换为可用于 PyTorch Mobile,因此我假设您使用 TorchScript 编写了模型脚本。

    使用 TorchScript,您可以使用 Torch 操作编写预处理逻辑,并将其保存在脚本模型中,如下所示:

    import torch
    import torch.nn.functional as F
    
    @torch.jit.script_method
    def preprocess(self,
                   image: torch.Tensor, # This should have format HxWx3
                   height: int,
                   width: int) -> torch.Tensor:
        img = image.to(self.device)
    
        # (1) Convert to Grayscale
        img = ((img[:, :, 0] + img[:, :, 1] + img[:, :, 2]) / 3).unsqueeze(-1)
    
        # (2) Resize to specified resolution
        # Mimic torchvision.transforms.ToTensor to use interpolate
        img = img.float()
        img = img.permute(2, 0, 1).unsqueeze(0)
        img = F.interpolate(img, size=(
            height, width), mode="bicubic", align_corners=False)
        img = img.squeeze(0).permute(1, 2, 0)
        # Then turn it back to normal image tensor
    
        # (3) Other normalization like mean substraction and convert to BxCxHxW format
        img -= self.mean_tensor  # mean substraction
        img = img.permute(2, 0, 1).unsqueeze(0)
        return img
    

    所以所有的预处理都将由libtorch 完成,而不是opencv

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2018-10-02
      • 2015-01-09
      • 1970-01-01
      • 2014-05-02
      • 2014-08-01
      • 1970-01-01
      相关资源
      最近更新 更多