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 &quot;contactarea.py&quot;, line 31, in &lt;module&gt;
extract_surface_contact()
File &quot;contactarea.py&quot;, line 17, in extract_surface_contact
major, minor = pt.ContactArea(sample_img, base=base_img)
File &quot;/home/ayoub.hichri/.local/lib/python3.8/site- 
packages/pytouch/tasks/contact_area.py&quot;, line 32, in __call__
) = self._compute_contact_area(contours, self.contour_threshold)
File &quot;/home/ayoub.hichri/.local/lib/python3.8/site- 
packages/pytouch/tasks/contact_area.py&quot;, 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) &gt; 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 = &quot;/home/../Documents/digit.png&quot;
sample_img_path = &quot;/home/../Documents/Digit2.png&quot;

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(&quot;Major Axis: {0}, minor axis: {1}&quot;.format(*major, *minor))
ImageHandler.save(&quot;surface_contact_1.png&quot;, 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(&quot;Major Axis: {0}, minor axis: {1}&quot;.format(*major, *minor))
ImageHandler.save(&quot;surface_contact_2.png&quot;, sample_img_2)


if __name__ == &quot;__main__&quot;:
extract_surface_contact()

Thanks in advance for anyone who helps me

Seems like the condition len(contour) &gt; 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)
