是否有我可以处理的错误是一个目录


#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格式的,它们被放在一个文件夹中。在上面的代码中,我试图遍历文件夹,在注释中选择我需要的元素,并将其附加到列表中,以便在数据帧中查看,但出现了上述错误。我认为错误是路径的结果,但我很好地引用了它,但也许它不是可接受的路径,我不知道该怎么办。

是的,培训文件列表中的一个条目是目录。

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

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)]

相关内容

最新更新