我遇到了以下问题,一些我正在运行的 python 代码。它应该只是遍历一个列表,但它似乎在做一些我真的无法弄清楚的奇怪而微妙的事情。
from skimage.io import imread
def addImageData(self):
for image in self.images:
print image.signatureId
for image in self.images:
print image.signatureId
imageNumber = str(image.signatureId).zfill(4)
filePath = self.imageDirectory + imageNumber + ".jpg"
image.construct(filePath)
def construct(self, filePath):
self.imageData = imread(filePath, as_grey=True)
imread 来自 skimage.io。addImageData 下的第一个 for 循环完美运行,打印出一系列从 1 到 ~600 的数字。然而,第二个循环,当添加构造方法时,只打印数字 1 直到遇到内存错误。老实说,我不知道是什么原因造成的。想法?
当使用键盘中断时,这是回溯:
File "rbm.py", line 22, in buildImages
self.addImageData()
File "rbm.py", line 41, in addImageData
image.construct(filePath)
File "rbm.py", line 61, in construct
self.imageData = imread(filePath, as_grey=True)
File "/usr/local/lib/python2.7/dist-packages/scikit_image-0.8.2-py2.7-linux-i686.egg/skimage/io/_io.py", line 142, in imread
img = rgb2grey(img)
File "/usr/local/lib/python2.7/dist-packages/scikit_image-0.8.2-py2.7-linux-i686.egg/skimage/color/colorconv.py", line 540, in rgb2gray
return _convert(gray_from_rgb, rgb[:, :, :3])[..., 0]
File "/usr/local/lib/python2.7/dist-packages/scikit_image-0.8.2-py2.7-linux-i686.egg/skimage/color/colorconv.py", line 339, in _convert
out = np.dot(matrix, arr)
在下面添加与 self.images 相关的所有代码:
class TrainingImages:
def __init__(self, csvFile = "../train.csv", imageDirectory = "../images/"):
self.csvFile = csvFile
self.imageDirectory = imageDirectory
self.images = []
def appendCsvLine(self, line):
'''Assumes the line is from a csv.reader object'''
signatureId = line[1]
if len(self.images) <= signatureId:
newImage = Image(signatureId)
self.images.append(newImage)
newImage.append(line)
else:
self.images[(signatureId-1)].append(line)
def buildImages(self):
with open(self.csvFile, 'rb') as strokeData:
reader = csv.reader(strokeData, delimiter=",")
for line in reader:
self.appendCsvLine(line)
self.addImageData()