機械学習用のトレーニングデータを作成しようとしています。
numpyで、複数画像の行列を作ろうとしているのですが、うまくいきません。
ひとまず、下記のようなコードを書いてみました。
意図は、
1.DBに記録されているコードに対応する画像を拾いx_train_tempに代入。
2.x_train_tempをx_trainに追加。
各変数や動きについてですが、次のとおりです。
- res:データベースから取得した値で、0001〜9999の値が入っています。
- img_dir:画像が格納されているディレクトリです。ここに 変数resの値 にファイル名が対応する画像ファイルがあります。0001.png〜9999.png
ここでは、img_dirにある画像を次々と取って、numpyの配列に入れようとしています。
- x_train_temp:は画像一枚を一時的に格納しています。x_train_temp.shapeとやると(720, 720, 3)となります。
- x_train:x_train_tempを追加して、(画像の枚数, 720, 720, 3)の配列を作りたいのですが、ここで躓いています。
python3
1x_train = np.array([]) 2for row in res: 3 img_path = img_dir + row['id'] + '.png' 4 x_train_temp = cv2.imread(img_path) 5 x_train = np.append(x_train,x_train_temp,axis=0)
実行しますと、次のようなエラーになります。
python3
1--------------------------------------------------------------------------- 2ValueError Traceback (most recent call last) 3<ipython-input-20-e099c89ccb9c> in <module>() 4 4 img_path = img_dir + row['id'] + '.png' 5 5 x_train_temp= cv2.imread(img_path) 6----> 6 x_train = np.append(x_train,x_train_temp,axis=0) 7 7 i += 1 8 9~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in append(arr, values, axis) 10 5001 values = ravel(values) 11 5002 axis = arr.ndim-1 12-> 5003 return concatenate((arr, values), axis=axis) 13 14ValueError: all the input arrays must have same number of dimensions
画像のサイズは720 x 720 pixなので、(画像の枚数,720,720,3)の配列を作りたいのですが、どのようにすればよろしいでしょうか。
書き方を変えてみるとつぎのようになります。
python3
1import cv2 2import numpy as np 3 4x_train_1= cv2.imread('/home/kwdr/dcvol/Script/SNN/html/stock_img/1301.png') 5x_train_1.shape 6(720, 720, 3) 7 8x_train_2= cv2.imread('/home/kwdr/dcvol/Script/SNN/html/stock_img/1332.png') 9x_train_2.shape 10(720, 720, 3)
x_train_1とx_train_2を追加したx_trainの配列を作りたいのですが、どのようにすれば良いでしょうか。
↓作ろうとしているもの。
python3
1x_train.shape 2(2, 720, 720, 3)
回答2件
あなたの回答
tips
プレビュー