我有一個看起來像這樣的影像資料集:資料集
每個影像的時間步長為 15 分鐘(如您所見,時間戳在檔案名中)。
現在我想將這些影像分組為 3 小時長的序列,并將這些序列保存在分別包含 12 個影像(= 3 小時)的子檔案夾中。結果在理想情況下是這樣的: 序列
我嘗試os.walk在保存影像資料集的檔案夾中使用和回圈,然后我使用 Pandas 創建了一個資料框,因為我認為我可以更輕松地處理這些檔案,但我認為我在這里完全偏離了目標。
uj5u.com熱心網友回復:
既然你說你只需要 12 個檔案(考慮到所有檔案的時間戳都是一樣的,12 是你需要的確切數字,下面的代碼可以幫助你
import os
import shutil
output_location = "location where you want to save them" # better not to be in the same location with the dataset
dataset_path = "your data set"
files = [os.path.join(path, file) for path, subdirs, files in os.walk(dataset_path) for file in files]
nr_of_files = 0
folder_name = ""
for index in range(len(files)):
if nr_of_files == 0:
folder_name = os.path.join(output_location, files[index].split("\\")[-1].split(".")[0])
os.mkdir(folder_name)
shutil.copy(files[index], files[index].replace(dataset_path, folder_name))
nr_of_files = 1
elif nr_of_files == 11:
shutil.copy(files[index], files[index].replace(dataset_path, folder_name))
nr_of_files = 0
else:
shutil.copy(files[index], files[index].replace(dataset_path, folder_name))
nr_of_files = 1
解釋代碼:
files獲取dataset_path. 您設定此變數files并將包含所有檔案的完整路徑。
for回圈對整個長度進行互動files。
用于nr_of_files計算每 12 個檔案。如果它是 0,它將創建一個名稱為files[index]您設定為輸出位置的檔案夾,將復制檔案(用輸出路徑替換輸入路徑)
如果是 11(從 0 開始,index == 11 表示第 12 個檔案)將復制檔案并設定nr_of_files回 0 以創建另一個檔案夾
最后else將簡單地復制檔案并遞增nr_of_files
uj5u.com熱心網友回復:
每個影像的時間步長為 15 分鐘(如您所見,時間戳在檔案名中)。
現在我想將這些影像分組為 3 小時長的序列,并將這些序列保存在分別包含 12 個影像(= 3 小時)的子檔案夾中
我建議利用datetime內置庫來獲得所需的結果,對于您擁有的每個檔案
- 獲取保存時間戳的子字串
- 決議成
datetime.datetime使用實體datetime.datetime.strptime - 使用
.timestamp方法將所述實體轉換為自紀元以來的秒數 - 計算整數除法的秒數(
//)10800(3小時內的秒數) - 轉換您獲得的值
str并將其用作目標子檔案夾名稱
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/377465.html
上一篇:繪制多類問題的ROC曲線
