2

I am trying to debug a piece of code that unexpectedly crashes on one of cv2.resize calls because of some exception raised in C code. For some reason if I try to run it as python -m pdb file.py I get "Post mortem debugger finished" message, and therefore can not enter debugging session, since the current line is set to the script's entry point again. Any tips on how to catch this error in the debugger? I guess opencv python wrapper processes errors incorrectly and crashes the whole process instead of raising a python exception. ipdb also crashes with some sort of not self._running assertion error and messes up the whole terminal session for some reason: a lot of symbols such as newline symbol are not processed properly and clear does not help.

The opencv error is OpenCV Error: Assertion failed (ssize.width > 0 && ssize.height > 0) in resize, file /io/opencv/modules/imgproc/src/resize.cpp, line 4044. This is the line in opencv sources, it calls CV_Assert(), no idea how it works. I found a line in python code that causes it, I print sizes of images before running resize and they all are of reasonable sizes.

UPD: I found an ValueError: signal only works in main thread error in logs, so the issue is that this is a C exception in a different thread.

Ben Usman
  • 7,969
  • 6
  • 46
  • 66
  • 2
    What version of OpenCV on which platform? Can you create a [mcve] that reproduces it? – Dan Mašek May 03 '18 at 17:46
  • 1
    @DanMašek it is CentOS with opencv 3.4.0 and Python 3.6.4. I also wish I could provide a reproducible example, but the error appears somewhere in the body of another poorly written library :) my question is more of a "how to run pdb on false assertions in c code" rather then "fix my code please" :) – Ben Usman May 03 '18 at 17:51
  • 1
    Oh, I see :) | Hmm, update. That assert causes `cv::Exception` and the wrappers translate those. It would happen if the source image is empty. | Btw, even if you catch the exception, OpenCV will still print the message to stderr (right now the only way to stop that with stable release is [patching OpenCV](https://stackoverflow.com/a/49604578/3962537)) – Dan Mašek May 03 '18 at 17:58

1 Answers1

1

An issue was indeed caused by image being of width zero, but output was obscured by output from other threads because processing was performed in a pool of workers. A solution was to somehow make that processing code execute in the main thread, and then pdb started debugging session and I was able to figure out the issue.

Ben Usman
  • 7,969
  • 6
  • 46
  • 66