我遇到以下問題,我正在運行一些Python代碼。它應該只是遍歷一個列表,但它似乎在做一些奇怪而微妙的事情,我真的無法弄清楚。在連續運行時更改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()
我的猜測是無限遞歸。如果你通過一個KeyboardInterrupt來停止程序,你的回溯看起來像什麼? –
是否打印數字1然後「掛起」? –
不,在打出內存錯誤之前,它會多次打印數字1。 –