Search code examples
pythonmicropythonopenmv

Python: self randomly not defined


I am running my code with micropython on that camera: OpenMV Camera

I randomly get the error in python that self is not defined. This is how my python code looks like: (the whole file would be too long)

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

Now, I've got 2 different error messages:

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

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

Sometimes the ',' is an 'int' or an '(arrow sign)', which is probably because the communication between computer and camera is interrupted.

The other type of error is at print(type(self)), "local variable self was called before defined" is the error message. This error has never occured while calling the function, it is always within the function.

Those errors occur completely randomly. This method is called several hundred times and suddenly it does not work? And since the instance of this class is not within any certain scope (it's created like you have opened an interpreter and type >>> a = 0), I can't imagine that it is deleted by the garbage collector.

Has anyone an idea what it could be or were I could continue research? Thankfully looking forward to your answers, desireentz

EDIT:

Here I added the findBlobs(self) function:

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()

Solution

  • Since I, at first, thought that this was a general (micro-)python error, I created this topic here. Then I posted the same question at the official forum of the OpenMV camera and uploaded the whole file. One of the developers of the firmware answered me that this implementation of micropython had no stack protection because that would cost a lot of performance. And I was using a recursive function which then corrupted the heap when the stack was full, producing these "random" errors.