首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在android中对pytorch的视频进行预处理

在android中对pytorch的视频进行预处理
EN

Stack Overflow用户
提问于 2021-05-05 05:24:49
回答 1查看 153关注 0票数 1

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

在训练过程中,模型从手机上获取原始镜头,并进行预处理,以(1)灰度,(2)压缩到我指定的特定较小的分辨率,(3)转换为张量器以馈送到神经网络(或潜在地将压缩的视频发送到远程服务器)。我使用OpenCV来做这件事,但我想知道在Android Kotlin中做这件事最简单的方法是什么?

Python代码参考:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-05-13 16:28:55

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

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

代码语言:javascript
运行
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67392409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档