图像加载函数实现报错及结果不符问题求助
load_data for Plate Image Channels Problem Description
I need to implement a load_data function that reads 3 plate images (corresponding to B, G, R channels, top to bottom) from a directory and returns a list of these images. The plates directory is located alongside my Notebook in the "Deep Learning in Computer Vision" folder.
My first attempt at the code threw a FileNotFoundError saying it couldn't find the directory 'C':
def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'): im_list=[] for i in dir_name: im=np.load(i) im_list.append(im) return im_list pass plates = load_data()
I realized this was because I was iterating over each character in the directory path string instead of the files inside the directory.
My second attempt didn't work either—it returned a list of path characters instead of the image data I expected (I was hoping for a list of loaded image arrays, but got a mess of individual characters from file paths):
import os def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'): im_list=[] for f in os.listdir(dir_name): fpath = os.path.join(dir_name, f) for im in fpath: im_list.append(im) print(im_list) plates = load_data()
I need help fixing this function to correctly load the images into a usable list.
Solution
The core issue in both attempts was incorrect iteration: you were looping over strings (the directory path or file path) instead of loading the actual image files. Here's the corrected function that properly loads your numpy image files:
import os import numpy as np def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'): im_list = [] # Get sorted list of files to ensure consistent B/G/R channel order sorted_files = sorted(os.listdir(dir_name)) for filename in sorted_files: # Skip non-numpy files (adjust extension if your images use a different format) if filename.endswith('.npy'): full_file_path = os.path.join(dir_name, filename) # Load the image file and add it to the list image = np.load(full_file_path) im_list.append(image) return im_list # Load the plate images plates = load_data() # Verify the result print(f"Successfully loaded {len(plates)} images")
Key Fixes Explained:
- Correct File Iteration: We loop over the filenames returned by
os.listdir(dir_name)instead of iterating over characters in the path string. - Valid Path Construction:
os.path.join(dir_name, filename)creates a cross-platform compatible full path to each image, avoiding path errors. - Consistent Order:
sorted(os.listdir(dir_name))ensures we load the images in a predictable sequence (critical for maintaining B/G/R channel alignment). - Filter Valid Files: The
endswith('.npy')check skips any unrelated files in the directory (like hidden system files) that could cause loading errors. - Proper Image Loading: We use
np.load(full_file_path)to actually load the numpy image data and append it to the list, instead of adding path characters.
This will return a list of your 3 channel images, ready for further computer vision processing.
内容的提问来源于stack exchange,提问作者Rim Sleimi

