【发布时间】: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