Jake3991
Jake3991

Reputation: 101

Importing Images based on a list of paths

So I am trying to import some images. All the images have their respective file paths in the first column of a .csv file. Below is the approach I have taken so far. The idea is make a list of the paths then use opencv to "imread" the images to a list "cv_img"

The problem at hand is only 1 image gets added to the list cv_img. Any idea why this is happening?

Is this the best approach?

#Import Labels
import pandas as pd
df = pd.read_csv('File path.csv',usecols=[3])
labels=df

#Import features
#Problem area
cv_img = []
list1=pd.read_csv('File path.csv',usecols=[0])
for img in list1:
    n= cv2.imread(img)
    cv_img.append(n)

X_train=cv_img
y_train=labels
print(len(X_train)) #prints 1
print(len(y_train)) #prints 1136

Upvotes: 1

Views: 1334

Answers (2)

Jeru Luke
Jeru Luke

Reputation: 21203

I presume there is an error in the for loop. I think your for loop is just reading the first image in the list.

Try modifying it to the following:

import cv2
for img in range(len(list1)):
    n= cv2.imread(list1[img])
    cv_img.append(n)

Upvotes: 1

Jake3991
Jake3991

Reputation: 101

So I am trying a different approach and still having some issues. The goal here is to have an array of the following dimensions. (length, 160,329,3). As you can see my reshape function is commented out. The "print(images.shape) line returns (8037,). Not sure how to proceed to get the right array dimensions. For reference the 1st column in the csv file is a list of paths to the image in question. This method also fixed the error I was getting before with pandas.

import csv
import matplotlib.pyplot as plt
f = open('/Users/username/Desktop/data/driving_log.csv')
csv_f = csv.reader(f)

m=[]
for row in csv_f:
  n=(row)
  m.append(n)

images=[]
for i in range(len(m)):
    img=(m[i][1])
    img=img.lstrip()
    path='/Users/username/Desktop/data/'
    img=path+img
    image=cv2.imread(img)
    images.append(image)
item_num = len(images)
images=np.array(images)
#images=np.array(images).reshape(item_num, 160, 320, 3)
print(images.shape)

Upvotes: 0

Related Questions