Python:未定义

2024-04-26 10:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我用micropython在相机上运行我的代码:OpenMV Camera

我在python中随机得到一个错误,self没有定义。我的python代码是这样的:(整个文件太长)

class BlobAnalyser:
#
#constructor and lots of functions
#...
#
    def findLandmarkCombo(self, bnoAngle, playingTowardsBlue):
        self.findBlobs()
        print(type(self))
        self.possibleLandmarkIDs = []
        if len(self.blobs) == 0:
            return None
        for blobIndex in range(len(self.blobs)):
            self.possibleLandmarkIDs.append([])
            #and so on and so on

现在,我有两个不同的错误消息:

sometimes within self.findBlobs() or at "self.possibleLandmarkIDs = []"

AttributeError: ',' object has no attribute 'possibleLandmarkIDs'

有时“,”是一个“int”或“(箭头符号)”,这可能是因为计算机和摄像机之间的通信中断。你知道吗

另一种类型的错误是打印错误(类型(self)),“局部变量self在定义之前被调用”是错误消息。调用函数时从未发生过此错误,它始终在函数中。你知道吗

这些错误完全是随机发生的。这个方法被调用了几百次,突然就不起作用了?由于这个类的实例不在任何特定的范围内(它的创建就像您打开了一个解释器并键入>;>;a=0),我无法想象它会被垃圾收集器删除。你知道吗

有人知道我可以继续研究什么吗? 感谢您的回答, 德西林茨

编辑:

这里我添加了findBlobs(self)函数:

def findBlobs(self):
        img = sensor.snapshot()
        #merge = True,
        allBlobs = img.find_blobs(self.thresholds, pixels_threshold=200, area_threshold=150, merge=True)
        self.blobs = []
        print("=====")
        i = 0
        for blob in allBlobs:
            i += 1
            img.draw_string(blob.cx() - 5, blob.cy() - 5, str(i))
            img.draw_rectangle(blob.rect())
            self.blobs.append(blob)
            print(str(i) + ": " + str(bin(blob.code())))
        self.sortBlobs()

Tags: and代码selfimgforlen定义def
1条回答
网友
1楼 · 发布于 2024-04-26 10:38:19

因为一开始我认为这是一个一般的(微)python错误,所以我在这里创建了这个主题。然后我在OpenMV相机的官方论坛上发布了同样的问题,并上传了整个文件。固件的一个开发人员回答我说,micropython的这个实现没有堆栈保护,因为那样会消耗很多性能。我使用了一个递归函数,当堆栈满的时候,这个函数破坏了堆,产生了这些“随机”错误。你知道吗

相关问题 更多 >