HichriA
HichriA

Reputation: 5

Problem with variable when working with python

Hello everyone im trying to work with the Digit tactile sensor with the PyTouch library so when i try to run the contact area code example i get this error

DigitSensor with SensorDataSources.RAW data source
Traceback (most recent call last):
File "contactarea.py", line 31, in <module>
extract_surface_contact()
File "contactarea.py", line 17, in extract_surface_contact
major, minor = pt.ContactArea(sample_img, base=base_img)
File "/home/ayoub.hichri/.local/lib/python3.8/site- 
packages/pytouch/tasks/contact_area.py", line 32, in __call__
) = self._compute_contact_area(contours, self.contour_threshold)
File "/home/ayoub.hichri/.local/lib/python3.8/site- 
packages/pytouch/tasks/contact_area.py", line 107, in _compute_contact_area
return poly, major_axis, major_axis_end, minor_axis, minor_axis_end
UnboundLocalError: local variable 'poly' referenced before assignment

this is the code in file contact_area.py

    def _compute_contact_area(self, contours, contour_threshold):
    for contour in contours:
        if len(contour) > contour_threshold:
            ellipse = cv2.fitEllipse(contour)
            poly = cv2.ellipse2Poly(
                (int(ellipse[0][0]), int(ellipse[0][1])),
                (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)),
                int(ellipse[2]),
                0,
                360,
                5,
            )
            center = np.array([ellipse[0][0], ellipse[0][1]])
            a, b = (ellipse[1][0] / 2), (ellipse[1][1] / 2)
            theta = (ellipse[2] / 180.0) * np.pi
            major_axis = np.array(
                [center[0] - b * np.sin(theta), center[1] + b * np.cos(theta)]
            )
            minor_axis = np.array(
                [center[0] + a * np.cos(theta), center[1] + a * np.sin(theta)]
            )
            major_axis_end = 2 * center - major_axis
            minor_axis_end = 2 * center - minor_axis
    return poly, major_axis, major_axis_end, minor_axis, minor_axis_end

and this is the python code for my main file which im trying to run

     import pytouch
     from pytouch.handlers import ImageHandler
     from pytouch.sensors import DigitSensor
     from pytouch.tasks import ContactArea

def extract_surface_contact():
base_img_path = "/home/../Documents/digit.png"
sample_img_path = "/home/../Documents/Digit2.png"

base_img = ImageHandler(base_img_path).nparray
sample_img = ImageHandler(sample_img_path).nparray
sample_img_2 = sample_img.copy()

# initialize with default configuration of ContactArea task
pt = pytouch.PyTouch(DigitSensor, tasks=[ContactArea])
major, minor = pt.ContactArea(sample_img, base=base_img)

print("Major Axis: {0}, minor axis: {1}".format(*major, *minor))
ImageHandler.save("surface_contact_1.png", sample_img)

# initialize with custom configuration of ContactArea task
contact_area = ContactArea(base=base_img, contour_threshold=10)
major, minor = contact_area(sample_img_2)

print("Major Axis: {0}, minor axis: {1}".format(*major, *minor))
ImageHandler.save("surface_contact_2.png", sample_img_2)


if __name__ == "__main__":
extract_surface_contact()

Thanks in advance for anyone who helps me

Upvotes: 0

Views: 53

Answers (2)

user2668284
user2668284

Reputation:

There are two possibilities.

Either the contours iterable is empty or len(contour) <= contour_threshold

If either of those two conditions is true then none of poly, major_axis, major_axis_end, minor_axis, minor_axis_end will have been instantiated

Upvotes: 0

Danii672
Danii672

Reputation: 33

Seems like the condition len(contour) > contour_threshold inside _compute_contact_area is never matched so the variable poly is never defined.

I recommend trying to print the length of contour before the if statement to check the values. If you want it to work even if the condition isn't matched just declare the variable at the start of your function with an empty value (poly = None)

Upvotes: 1

Related Questions