# coding: utf-8 #主要将子文件下大量图片进行路径编号,并保存到csv文件当中。方便直接从文件读取图片路径以及其他图片信息。 #我做的是图像分割,所以存在三类分割区域:["large_bowel", "small_bowel", "stomach"]。 #文件路径:train\case*\case*_day*\scans\* from glob import glob import os import pandas as pd def generate_sub(case): LABELS = ["large_bowel", "small_bowel", "stomach"] # preds = [] for i in range(len(case)): cases = case[i] case_day = glob(os.path.join(cases, "*")) for j in range(len(case_day)): scan = case_day[j] case_day_name = scan.split("\\")[-1] scans = glob(os.path.join(os.path.join(scan, "scans"), "*")) for k in range(len(scans)): file_name_slice = scans[k].split("\\")[-1].split("_")[1] fileN = case_day_name+"_slice_"+ file_name_slice print(fileN) preds += [{"id": fileN, "class": LABELS[i], "predicted": None} for i in range(len(LABELS))] df_pred = pd.DataFrame(preds) df_pred.to_csv("sample_submission.csv", index=False) if __name__ == "__main__": case = glob(r"D:\compation\kaggle\traines\*") generate_sub(case)
结果: