2017-02-09 84 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)