尝试加载数据时出现keyerror

时间:2018-09-28 20:24:49

标签: parsing annotations

我正尝试使用下面的代码来解析图像和注释中的数据集。我在Windows机器中使用带有Spyder IDE的anaconda:

DATA_DIR = 'input'
# Directory to save logs and trained model
ROOT_DIR = 'working'
train_dicom_dir = os.path.join(DATA_DIR, 'stage_1_train_images')
test_dicom_dir = os.path.join(DATA_DIR, 'stage_1_test_images')
print(train_dicom_dir)
print(test_dicom_dir)
def get_dicom_fps(dicom_dir):
    dicom_fps = glob.glob(dicom_dir+'/'+'*.jpg')
    return list(set(dicom_fps))
def parse_dataset(dicom_dir, anns): 
    image_fps = get_dicom_fps(dicom_dir)
    image_annotations = {fp: [] for fp in image_fps}
    for index, row in anns.iterrows(): 
        fp = os.path.join(dicom_dir, row['patientId']+'.jpg')
        image_annotations[fp].append(row)
    return image_fps, image_annotations

image_fps, image_annotations = parse_dataset(train_dicom_dir, anns=anns)

运行代码时,出现以下错误:

Traceback (most recent call last):
  File "<ipython-input-21-fe2564bb2360>", line 1, in <module>
    image_fps, image_annotations = parse_dataset(train_dicom_dir, anns=anns)
  File "<ipython-input-12-a5d26437db38>", line 6, in parse_dataset
    image_annotations[fp].append(row)
KeyError: 'input\\stage_1_train_images\\1.2.276.0.7230010.3.1.4.8323329.10001.1517874346.163716.jpg'

我当前的工作目录是'C:\ Users \ rajaramans2 \ codes \ input'

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试处理.dcm(DICOM格式)而不是.jpg的图像,因此如果将“ .jpg”替换为“ .dcm”,您的代码应该可以工作,希望对您有所帮助。