glob 文件名模式匹配,不用遍历整个目录判断每个文件是不是符合。

    1. import glob
    2. import cv2
    3. car_images = glob.glob('OriginalDataset/vehicles/**/*.png', recursive=True)
    4. notcar_images = glob.glob('OriginalDataset/non-vehicles/**/*.png', recursive=True)
    5. for car in car_images:
    6. cars.append(cv2.imread(car))
    7. for ncar in notcar_images:
    8. notcars.append(cv2.imread(ncar))