0

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.

slow_one
  • 113
  • 1
  • 4
  • 13
  • If you formatted your code correctly it would be easier to help you. –  Jul 19 '17 at 20:56
  • what's wrong with my code? – slow_one Jul 19 '17 at 20:59
  • `None != []`. `if rl:` might work since it will avoid anything falsey, but it's a guess because it's hard to understand your question. Also, you don't appear to make any attempt to catch the exception (which your question title suggests) but try to avoid the exceptional circumstance. – roganjosh Jul 19 '17 at 20:59
  • Are you sure that's the right line? It sounds like an error thrown for the max function... Try commenting out everything after the max function and see if it runs. – LeopoldVonBuschLight Jul 19 '17 at 21:00
  • @LeopoldVonBuschLight I think what's happening is that the OP thinks that `if rl is not None:` will avoid an exception further down the code, but it's not very clear. It would be helpful if there was a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – roganjosh Jul 19 '17 at 21:02
  • @roganjosh : i'd love to provide that ... except that you'd need an OpenMV camera (https://openmv.io/) to be able to verify the code in its entirety. – slow_one Jul 19 '17 at 21:05
  • At the very least, please provide the full traceback (copy/paste the whole thing, not just a bit from the end). Also, please provide feedback on whether changing `if rl is not None:` to `if rl:` made any difference. – roganjosh Jul 19 '17 at 21:07
  • @LeopoldVonBuschLight : I've tried that ... and it does continue to fail ... but what's weird is that this same code sequence has worked elsewhere without issue. Which is why I'm lost. – slow_one Jul 19 '17 at 21:07
  • @roganjosh : Traceback (most recent call last): File "", line 77, in ValueError: arg is an empty sequence MicroPython d23b594 on 2017-07-05; OPENMV3 with STM32F765 Type "help()" for more information. – slow_one Jul 19 '17 at 21:08
  • As an edit to the question. Also, your line numbers have changed from 59 to 77. But the traceback should go further back and point to all the lines of code that eventually led to the error as a chain reaction, it's helpful to see it all. – roganjosh Jul 19 '17 at 21:09
  • the rest is just commented code ... – slow_one Jul 19 '17 at 21:11
  • @slow_one Try seperating 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. – LeopoldVonBuschLight Jul 19 '17 at 21:11
  • @LeopoldVonBuschLight : I think you're right. I think I'll need to do that ... will be doing so tomorrow. – slow_one Jul 19 '17 at 21:14

1 Answers1

1

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)
  • That if statement seems to have averted the issue. I'm not sure why the max() statement is having that issue. I'll have to explore that further (the find_line_segments() function *is* able to find lines ... it's not that it doesn't having anything to "max()" – slow_one Jul 20 '17 at 13:20