【问题标题】:Saving mp4 files into csv for training the data将 mp4 文件保存到 csv 以训练数据
【发布时间】:2021-05-08 17:09:00
【问题描述】:

我是计算机视觉领域的新手,我正在尝试训练我的模型,作为工作的开始,我使用标签编码器为我正在使用的事件标记我的视频。这里我有两个事件,一个是意外事件,一个是非意外事件。

图片的文件夹结构:

Colab_Notebooks
- accident(all the .jpg frames are here)
- nonaccident(all the .jpg frames are here)

所以我的 data.csv 文件如下所示,代码如下。

data.csv 
image_path,target
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000638.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/nonaccident/nonaccident_0002143.jpg,1.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000372.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000419.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/nonaccident/nonaccident_0001675.jpg,1.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000307.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_00001099.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000940.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000892.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000805.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000232.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000255.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000840.jpg,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000974.jpg,0.0

我用来生成data.csv的代码如下:

all_paths = os.listdir('/content/drive/MyDrive/Colab_Notebooks/')

folder_paths = [x for x in all_paths if os.path.isdir('/content/drive/MyDrive/Colab_Notebooks/' + x )]

print(f"Folder paths : {folder_paths}")
print (f"Number of folders: {len(folder_paths)}")
create_labels = ['accident','nonaccident']

data = pd.DataFrame()

image_formats = ['jpg']
labels = []
counter = 0
for i, folder_path in tqdm(enumerate(folder_paths), total = len(folder_paths)):
    if folder_path not in create_labels:
        continue
    image_paths = os.listdir('/content/drive/MyDrive/Colab_Notebooks/' + folder_path)
    label = folder_path

    for image_path in image_paths:
        if image_path.split('.')[-1] in image_formats:
            data.loc[counter,'image_path'] =  f"/content/drive/MyDrive/Colab_Notebooks/{folder_path}/{image_path}"
            labels.append(label)
            counter += 1
labels = np.array(labels)
# one-hot encode the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)

#print(labels)

# save as CSV file
data.to_csv('/content/drive/MyDrive/Colab_Notebooks/data.csv', index=False)

# pickle the binarized labels
print('Saving the binarized labels as pickled file')
joblib.dump(lb, '/content/drive/MyDrive/Colab_Notebooks/lb.pkl')

print(data.head(5))

我能够做到这一点,因为您在顶部看到的数据集是 jpg 图像的帧。但我想对视频做同样的事情。

Colab_Notebooks
- accident(all the .mp4 clips are here)
- nonaccident(all the .mp4 clips are here)

Expected output:
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000638.mp4,0.0
/content/drive/MyDrive/Colab_Notebooks/nonaccident/nonaccident_0002143.mp4,1.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000372.mp4,0.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000419.mp4,0.0
/content/drive/MyDrive/Colab_Notebooks/nonaccident/nonaccident_0001675.mp4,1.0
/content/drive/MyDrive/Colab_Notebooks/accident/accident_0000307.mp4,0.0

谁能告诉我如何修改代码以读取视频剪辑而不是图像?

【问题讨论】:

    标签: python-3.x ffmpeg deep-learning google-colaboratory


    【解决方案1】:

    我能够通过使用 mp4 作为格式代替 jpg 来解决这个问题。

    【讨论】:

      【解决方案2】:

      如果您希望重用现有设置,请使用 ffmpeg(从命令行或使用 python)提取视频帧,并将它们保存为带有标签 0 或 1 的图像,然后像以前一样构建分类器。 如果您希望在视频级别训练分类器,而不是像现在这样在图像级别训练分类器,您可以将每个视频的帧保存在单独的文件夹中。

      【讨论】:

      • 那不是我想做的。我期望的输出可以在上面看到。这个想法是我想在提取到帧之前将文件单独拆分为视频,否则我的结果将不正确。
      猜你喜欢
      • 2014-06-12
      • 1970-01-01
      • 2021-06-23
      • 2018-09-11
      • 2016-09-01
      • 2022-08-23
      • 1970-01-01
      • 2020-08-19
      • 2012-07-11
      相关资源
      最近更新 更多