【问题标题】:Loading images into a list and its corresponding lables [Python]将图像加载到列表及其相应的标签中 [Python]
【发布时间】:2021-02-26 18:17:23
【问题描述】:

在一个文件夹中加载图像并将其标签加载到另一个文件夹时出现问题。 这是我关于它的旧帖子: Reading images in multiple folders
我已将图像加载到变量 X_data 中,并将其标签加载到 Y_data 中。但是,它们的范围并不相同。此外,Y_data 中的元素为空。我应该怎么做才能确保每张图片都有正确的标签?

p = Path('Carla_Videos')
myFolderList = [f.path for f in os.scandir(path) if f.is_dir()]
X_data = []
Y_data = []
for files in p.glob('rgb/**/*.jpg'):
    #print(files)
    img = cv2.imread(str(files))
    X_data.append(img)
    #splitted_path = files.replace('.jpg','').sp
    # print(img_path)
for f in p.glob('annotation/**/*.txt'):
    labels = np.loadtxt(str(f))
    Y_data.append(labels)

【问题讨论】:

    标签: python opencv deep-learning


    【解决方案1】:

    根据您之前的问题给出这个示例目录结构:

    ├───annotation
    │   ├───00
    │   │       file1.txt
    │   │       file2.txt
    │   └───01
    │           file3.txt
    └───rgb
        ├───00
        │       file1.jpg
        │       file2.jpg
        └───01
                file3.jpg
                file4.jpg
    

    这段代码将定位.jpg文件,计算匹配的注解,并检查注解是否存在:

    from pathlib import Path
    
    p = Path('.')
    
    for image in p.glob('rgb/**/*.jpg'):
        text = 'annotation' / image.relative_to('rgb').with_suffix('.txt')
        print(image,text,text.is_file())
    

    输出:

    rgb\00\file1.jpg annotation\00\file1.txt True
    rgb\00\file2.jpg annotation\00\file2.txt True
    rgb\01\file3.jpg annotation\01\file3.txt True
    rgb\01\file4.jpg annotation\01\file4.txt False
    

    【讨论】:

    • 嗨,我试过了,但出现错误:ValueError: 'Carla_Videos\\rgb\\000\\00.jpg' does not start with 'rgb'
    • @HaiLuu 我从当前目录开始为p。将Carla_videos 设为当前目录或改用'Carla_videos/annotations' / image.relative_to('Carla_videos/rgb')。阅读pathlib 文档并了解这些方法的工作原理。
    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2014-01-06
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多