我有一個檔案夾(exp),在另一個檔案夾(labels)中有 800 個影像和 800 個文本檔案。每個影像都有文本檔案。如何創建具有影像和相應文本檔案的串列?
該檔案夾如下所示:
-exp
-labels
FrameVid2_178.txt
Frame_1103.txt
Frame_856.txt
...
...
FrameVid2_178.JPG
Frame_1103.JPG
Frame_856.JPG
...
...
該串列應如下所示:
[['../exp/FrameVid2_178.JPG', 'FrameVid2_178.txt'],
['../exp/Frame_1103.JPG', 'Frame_1103.txt'],
['../exp/Frame_856.JPG', 'Frame_856.txt'],
.....]
我的代碼:
def __init__(self):
self.imgs_path = "../exp/"
file_list = glob.glob(self.imgs_path "*")
self.data = []
for class_path in file_list:
class_name = class_path.split("/")[-1]
for img_path in glob.glob(class_path):
self.data.append([img_path, class_name])
print(self.data)
self.img_dim = (416, 416)
uj5u.com熱心網友回復:
你的代碼應該可以作業,不確定你在哪里遇到了問題,但它可以使用一些檢查/處理(注意:你也可以做更多的路徑驗證/檢查,pathlib.Path 非常適合)。
def __init__(self, img_path):
self.imgs_path = "../exp/"
file_list = glob.glob(self.imgs_path "*.JPG")
self.data = []
for class_path in file_list:
class_name = class_path.split("/")[-1]
matching_labels = list(glob.glob(class_path "/labels/" class_name[:-3] "*"))
if len(matching_labels) != 1:
raise IOError("Matching issue", len(matching_labels))
self.data.append([img_path, matching_labels[0].split("/")[-1])
print(self.data)
self.img_dim = (416, 416)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/350204.html
