首页 > 解决方案 > 有什么方法可以处理错误是目录吗?

问题描述

#Defining the directories to the file
train_image_path = "/content/drive/My Drive/Skin_Disease/train_images"#Path(".../Skin_Disease/train_images")
train_annotation_path = "/content/drive/My Drive/Skin_Disease/train"#Path(".../Skin_Disease/train")

test_image_path = Path("./Skin_Disease/test_images")
test_annotation_path = Path("./Skin_Disease/test")

valid_image_path = Path("./Skin_Disease/valid_images")
valid_annotation_path = Path("./Skin_disease/valid")

#walking through the training directory to get list files

def filelist(root, file_type):
    return [os.path.join(directory_path, f) for directory_path, directory_name,
            files in os.walk(root) for f in files if f.endswith(file_type)]

#creating a dataframe to view the read files 
def generate_train_df (anno_path):
    annotations = filelist(anno_path, '.xml')
    anno_list = []
    for annotation in annotations:
        root = ET.parse(anno_path).getroot()
        anno = {}
        anno["filename"] = Path(str(train_image_path) + '/' + root.find("./filename").text)
        anno["width"] = root.find("./size/width").text
        anno["height"] = root.find("./size/height").text
        anno["xmin"] = int(root.find("./object/bndbox/xmin").text)
        anno["ymin"] = int(root.find("./object/bndbox/ymin").text)
        anno["xmax"] = int(root.find("./object/bndbox/xmax").text)
        anno["ymax"] = int(root.find("./object/bndbox/ymax").text)
        anno_list.append(anno)
        #print(anno)
    return pd.DataFrame(anno_list)


df_train = generate_train_df(train_annotation_path)
print(df_train.shape)
df_train.head()
#print(df_train)

错误得到:

IsADirectoryError                         Traceback (most recent call last)
<ipython-input-36-b80f473b0929> in <module>()
----> 1 df_train = generate_train_df(train_annotation_path)
      2 print(df_train.shape)
      3 df_train.head()
      4 #print(df_train)

2 frames
/usr/lib/python3.6/xml/etree/ElementTree.py in parse(self, source, parser)
    584         close_source = False
    585         if not hasattr(source, "read"):
--> 586             source = open(source, "rb")
    587             close_source = True
    588         try:

IsADirectoryError: [Errno 21] Is a directory: '/content/drive/My Drive/Skin_Disease/train'

我正在尝试使用 pytorch 进行边界框预测。我的注释是 xml 格式,它们被放置在一个文件夹中。在上面的代码中,我试图遍历文件夹并选择我需要的注释中的元素并将其附加到列表中以便在数据框中查看,但出现上述错误。我认为错误是路径的结果,但我很好地引用了它,但也许它不是我不知道该怎么做的公认路径。

标签: python-3.xmachine-learningcomputer-visionpytorchartificial-intelligence

解决方案


是的,您的训练文件列表中的条目之一是目录。

您可以通过简单地从训练文件列表中排除目录路径来修复此错误:

def filelist(root, file_type):
    flist = [os.path.join(directory_path, f) for directory_path, directory_name,
            files in os.walk(root) for f in files if f.endswith(file_type)]

    return [path for path in flist if not os.path.isdir(path)]

推荐阅读