加载方式一
将视频数据按顺序保存成图片数据
优点:好像没有啥优点
缺点:繁琐、小文件特别多,索引速度慢
加载方式二
使用python或者pytorch提供的一些接口直接加载视频数据,这里比较推荐pytorch的接口。
优点:简单方便,硬盘空间消耗最少
缺点:加载的数据需要做解码,会偏慢
加载方式三
将视频数据转换成numpy数组,并保存成npy的格式
优点:加载速度快
缺点:步骤多,占用硬盘空间大
使用pytorch官方的API加载范例
代码来源,代码功能:
1.使用pytorch提供的视频加载接口加载数据
2.将加载的数据喂入预训练的R3D模型中
3.获得输出分类和得分
from torchvision.io.video import read_video
from torchvision.models.video import r3d_18, R3D_18_Weights
vid, _, _ = read_video("test/assets/videos/v_SoccerJuggling_g23_c01.avi", output_format="TCHW")
vid = vid[:32] # optionally shorten duration
# Step 1: Initialize model with the best available weights
weights = R3D_18_Weights.DEFAULT
model = r3d_18(weights=weights)
model.eval()
# Step 2: Initialize the inference transforms
preprocess = weights.transforms()
# Step 3: Apply inference preprocessing transforms
batch = preprocess(vid).unsqueeze(0)
# Step 4: Use the model and print the predicted category
prediction = model(batch).squeeze(0).softmax(0)
label = prediction.argmax().item()
score = prediction[label].item()
category_name = weights.meta["categories"][label]
print(f"{category_name}: {100 * score}%")
结尾
还有没有更多的加载方式呢?有的话请@我去试试的