Reputation:
I have a dataset for the self-driving car. My X
values are the names of the images. Example would be
array([['img_2.png'],
['img_3.png'],
['img_4.png'],
...,
['img_6405.png'],
['img_6406.png'],
['img_6407.png']], dtype=object)
I found out that model performs good if we have some kind of batch_generator
. I found that template code.
def batch_generator(image_paths, steering_ang, batch_size, istraining):
while True:
batch_img = []
batch_steering = []
for i in range(batch_size):
random_index = random.randint(0, len(image_paths) - 1)
if istraining:
im = random_augment(image_paths[random_index])
steering = steering_ang[random_index]
else:
im = mpimg.imread(image_paths[random_index])
steering = steering_ang[random_index]
im = img_preprocess(im)
batch_img.append(im)
batch_steering.append(steering)
yield (np.asarray(batch_img), np.asarray(batch_steering))
I changed this function to for my use but when i apply it.
x_train_gen, y_train_gen = next(batch_generator(X_train, y_train, 1, 1))
x_valid_gen, y_valid_gen = next(batch_generator(X_valid, y_valid, 1, 0))
I get the following error TypeError: Object does not appear to be a 8-bit string path or a Python file-like object
. I understand the error, image is not an array but a string. How can i convert string of the image path to the array
Upvotes: 0
Views: 563
Reputation: 11
It's because at some point you converted X_train
and y_train
into numpy arrays instead of image paths.
That's why python is complaining. You were probably doing something else with the code which needed you to convert the entire training dataset, but now you don't need that because you have imread()
in the batch_generator
function. I'd go back to earlier in the code and recreate X_train
and y_train
as the file paths to the images, and then re-run this portion of the code.
Upvotes: 1
Reputation: 2171
I don't know what you're doing in the img_preprocess()
function but from what I see there are 2 possible problems:
You have to append the path to the image to the image name: path_to_image = path_to_image_dir + '/' + image
You have to actually open the image to get it's array. You can use Pillow or OpenCV:
PIL.Image.open(path_to_image)
or cv2.imread(path_to_image)
Upvotes: 0