gpt4 book ai didi

python-3.x - 将 mp4 文件保存到 csv 以训练数据

转载 作者:行者123 更新时间:2023-12-04 23:21:06 24 4
gpt4 key购买 nike

我对计算机视觉领域非常陌生,我正在尝试训练我的模型,作为工作的开始,我使用标签编码器为我正在使用的事件标记我的视频。在这里,我有两个事件是意外事件和非意外事件。
图片的文件夹结构:

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
有人可以告诉我如何修改代码以读取视频剪辑而不是图像吗?

最佳答案

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

关于python-3.x - 将 mp4 文件保存到 csv 以训练数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66044967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com