我有一個包含影像標簽和路徑的 csv 檔案,我有另一個檔案夾包含所有影像,所以我想將每個標簽的影像保存在它自己的檔案夾中,這里是 csv 的樣子,我很感激任何幫助
在此處輸入影像描述
我沒有找到這個的任何代碼
uj5u.com熱心網友回復:
您必須使用pandas用于讀取 csv,os用于創建檔案夾 eshutil用于復制檔案。
import os
import shutil
import pandas as pd
# read the file
csv_file = pd.read_csv('file.csv', dtype=str)
# create the folders
labels = csv_file['label']
for label in labels:
os.makedirs(label, exist_ok=True)
# iterate rows and copy images
for _, row in csv_file.iterrows():
label = row['label']
path = row['path']
img_name = os.path.split(path)[-1]
new_path = os.path.join(label, img_name)
shutil.copy(path, new_path)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/536695.html
