slow_one
slow_one

Reputation: 113

Python: error check failing to catch exception

I'm writing in a stripped down version of python, micro python.
I'm doing some image processing and am trying to find the longest line returned from a method called, "find_line_segments" (it does Canny Edge and Hough Lines transforms).
BUT! I keep getting an error.
Code

    rl = max(img.find_line_segments(roi = r_r, threshold = 1000, theta_margin = 15, rho_margin = 15, segment_threshold = 100), key = lambda x: x.length())
    if rl is not None:
        if rl[6] > 0 :
            img.draw_line(rl.line(), color = 155)
            print("RL")
            print(rl)

Error:

Traceback (most recent call last):
File "<stdin>", line 77, in <module>
ValueError: arg is an empty sequence
MicroPython d23b594 on 2017-07-05; OPENMV3 with STM32F765
Type "help()" for more information.

That error points to the line if rl is not None: ... and I don't understand why it is causing an error. If the max() function doesn't return a value (in the case that a line isn't found), the "if statement" should never execute.
What am I not understanding?

Edit:
Accidentally removed some code.

Upvotes: 0

Views: 204

Answers (1)

LeopoldVonBuschLight
LeopoldVonBuschLight

Reputation: 911

Try separating out that max(img.find_line_segments(...)...) statement into two statements and test if the result of find_line_segments is anything before using the max function. It sounds like the max function is what's throwing the exception:

# Dummy function
def find_line_segments():
    return []

max_segment = max(find_line_segments())

Gives this exception for trying to use max() on an empty sequence:

Traceback (most recent call last):
  File "C:\Users\ayb\Desktop\t (3).py", line 8, in <module>
    max_segment = max(get_line_segments())
ValueError: max() arg is an empty sequence

Do something like this instead to avoid the exception.

segments = find_line_segments()
if segments:
    max_segment = max(segments)

Upvotes: 1

Related Questions