darkman9333
darkman9333

Reputation: 131

Object variables not resetting

I'm working on creating a vision tracking program using a pipeline. In my pipeline I have the following code:

@staticmethod
def __approx_contours(input_contours):
    output = []
    kp = None
    for contour in input_contours:
        error = 0.1*cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, error, True)
        print(approx)
        kp = Keeper(approx)
        print(kp)
    if kp == None:
        return output
    for x,y in zip(kp.getX(),kp.getY()):
        output.append((x,y))
    kp.empty()
    return output

and here is Keeper class

class Keeper:

    _x = []
    _y = []

    # constructor, requires a 3-D array
    def __init__(self, third_dim):
        if third_dim.ndim == 3:
            for row in third_dim:
                for col in row:
                    self._x.append(col[0])
                    self._y.append(col[1])
        else:
            raise NotThirdDimension("Entered array was not three dimensional")
        print(self._x)
        print(self._y)

    # return object "X" array
    def getX(self):
        return self._x

    # return object "Y" array
    def getY(self):
        return self._y

    def empty(self):
        self._x = []
        self._y = []

Disregard the print statements in __init__, they are only for debugging purposes.

Example output in current state:

[[[183 169]]

 [[187 323]]]
Keeper
[183, 187]
[169, 323]
<keeper.Keeper object at 0x05199630>

[[[ 62 117]]

 [[ 93 366]]

 [[187 256]]]
Keeper
[183, 187, 62, 93, 187]
[169, 323, 117, 366, 256]
<keeper.Keeper object at 0x06F10B70>

It appears that the values in my Keeper object are not being reset, despite calling kp.empty(). I've also noted that the Keeper object is changing place in memory, perhaps this is part of the issue but I'm not sure where I am going wrong. Full code is available here

Upvotes: 0

Views: 1453

Answers (1)

darkman9333
darkman9333

Reputation: 131

The issue was as user @juanpa.arrivillaga said, I was using class attributes instead of instance attributes. To remedy my issue the following changes needed to be made to Keeper:

class Keeper:

    # constructor, requires a 3-D array
    def __init__(self, third_dim):
        self.x = []
        self.y = []
        if third_dim.ndim == 3:
            for row in third_dim:
                for col in row:
                    self.x.append(col[0])
                    self.y.append(col[1])

Upvotes: 2

Related Questions