0

我对 fastai 非常陌生,我正在尝试调整以前的模型。只有当文件在磁盘上时,我才能加载模型并使用推理。由于我正在尝试将文件处理为视频流,因此我正在保存每个文件并一次处理一个文件,例如。

import cv2, imageio 
from fastai.vision import *

# ... load the model here

stream = cv2.VideoCapture('Die.Hard.1988.mkv')

while True:
    _, frame = stream.read()

    with tempfile.NamedTemporaryFile(suffix='.jpg') as FOUT:
        imageio.imwrite(FOUT.name, frame)
        FOUT.flush()

        x = open_image(FOUT.name)
        preds_num = learn.predict(x)[2].numpy()

这似乎可行,但将图像保存到磁盘似乎很浪费。该库似乎使用了一些转换,这意味着我不能直接将图像推送给“学习者”。这是它的加载方式:

from fastai.vision import *

f_model = "shot-type-classifier"
path = "shot_type_classifier/"

data = ImageDataBunch.from_folder(
    path,
    "train",
    "valid",
    size=(375, 666),
    ds_tfms=get_tfms(),
    bs=1,
    resize_method=ResizeMethod.SQUISH,
    num_workers=0,
).normalize(imagenet_stats)

learn = cnn_learner(data, models.resnet50, metrics=[accuracy], pretrained=True)
learn = learn.to_fp16()
learn.load(f_model)

这里似乎定义了转换。我真的只想将图像直接传送到预处理器,然后传送到模型,而不必将它们保存到磁盘。有没有办法使用fastai做到这一点?

4

1 回答 1

1

尝试将帧转换为枕头图像,然后使用 pil2tensor:

from PIL import Image as PImage
from fastai.vision import *

frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
pil_im = PImage.fromarray(frame) 
x = pil2tensor(pil_im ,np.float32)
preds_num = learn.predict(Image(x))[2].numpy()
于 2019-09-09T00:08:52.803 回答