我正在尝试编写一个函数来加载和处理 NN 的数据。作为输入,我有一组不同尺寸的图片。图片应表示为具有 RGB 通道的 3D numpy 数组。我需要它们具有相同的大小(最大图片的大小)。
我已经尝试过np.pad,但似乎我不知道它应该如何工作。实际上,即使我得到了填充,我也不知道如何根据图片的大小来改变它。这是代码:
from PIL import Image
import numpy as np
import cv2
import os
def load_data(path):
aminoacids = ['Ala','Arg','Asn','Asp','Cys','Gln','Glu','Gly','His','Ile', 'Ini', 'Leu','Lys','Met','Phe','Pro','Pyr', 'Sec','Ser','Thr','Trp','Tyr','Val']
matrix = []
answer_labeled = []
names = os.listdir(path)
for i in names:
matrix = cv2.imread(path + i, 1)
matrix = np.pad(matrix, (0, 1), 'constant', constant_values=[255,255,255])
for y in aminoacids:
if y + "-" in i:
a = [matrix, y]
answer_labeled.append(a)
return answer_labeled
data_processed = load_data("/content/drive/My Drive/Thesis/dl/img/ans/")
我收到此错误:
ValueErrorTraceback (most recent call last)
<ipython-input-50-e021738e59ea> in <module>()
20 return answer_labeled
21
---> 22 data_processed = load_data("/content/drive/My Drive/Thesis/dl/img/ans/")
23
24 # print(len(os.listdir("/content/drive/My Drive/Thesis/dl/img/ans/")))
<ipython-input-50-e021738e59ea> in load_data(path)
13 for i in names:
14 matrix = cv2.imread(path + i, 1)
---> 15 matrix = np.pad(matrix, (0, 1), 'constant', constant_values=[255,255,255])
16 for y in aminoacids:
17 if y + "-" in i:
/usr/local/lib/python2.7/dist-packages/numpy/lib/arraypad.pyc in pad(array, pad_width, mode, **kwargs)
1208 kwargs[i] = _as_pairs(kwargs[i], narray.ndim, as_index=True)
1209 if i in ['end_values', 'constant_values']:
-> 1210 kwargs[i] = _as_pairs(kwargs[i], narray.ndim)
1211 else:
1212 # Drop back to old, slower np.apply_along_axis mode for user-supplied
当然,我试图用谷歌搜索这个错误,但没有找到对我有用或可以理解的东西(因为我真的是编程新手)。我将不胜感激任何帮助和想法。
繁星coding
米脂
相关分类