2017-02-09 88 views
0

所以我试图导入一些图像。所有图像在.csv文件的第一列中都有各自的文件路径。以下是我迄今采取的方法。这个想法是制作路径列表,然后使用opencv将图像“imread”到列表“cv_img”基于路径列表导入图像

手头的问题是只有1个图像被添加到列表cv_img。任何想法为什么发生这种情况?

这是最好的方法吗?

#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 
+0

你应该张贴,你想在这里读的样本CSV。 –

+0

而在察觉出来 –

回答

0

我推测在for循环中有错误。我认为你的for循环只是读取列表中的第一个图像。

尝试修改为以下内容:

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

@indraforyou伟大的工作我注意到list1是一个熊猫数据框。btw。你测试了你的代码吗? –

+0

对不起,我以前的打印列表1 –

+0

这给了我一个错误。pandas.index.IndexEngine.get_loc中的pandas/index.pyx(pandas/index.c:4154)() pandas.index中的pandas/index.pyx。 IndexEngine.get_loc(熊猫/ index.c:4018)() 熊猫/ hashtable.pyx在pandas.hashtable.PyObjectHashTable.get_item(熊猫/ hashtable.c:12368)()中的熊猫 大熊猫/ hashtable.pyx。 hashtable.PyObjectHashTable.get_item(pandas/hashtable.c:12322)() KeyError:1 – Jake3991

0

所以我尝试一种不同的方法,仍然有一些问题。这里的目标是具有以下维度的数组。 (长度160,329,3)。正如你所看到的,我的重塑功能被注释掉了。 “print(images.shape)”一行返回(8037,)。不知道如何继续获得正确的数组尺寸。csv文件中的第一列是参考图像的路径列表。固定的错误,我用熊猫获得前。

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)