0

I'm trying to write a python program which reads frames in jpg format in real time using the imread() function.

The program work fines in pycharm but it suddenly stops working after around 3000-6000 iterations (it depends) if the program is packaged using pyinstaller on Windows.

I have tried to use cv2.VideoCapture(f"{src}/%d.jpg", cv2.CAP_IMAGES) but still the problem exists.

Below is part of the code using imread() inside frames reading function, which is run on a thread.

 while not self.isStopTask:
                    print("Debug - 1",flush=True)
                    imgpath = abspath( join(self.src,str(CurrFrameNum)+".jpg"))
                    print("Debug - 2",flush=True)
                    frame = cv2.imread(imgpath)
                    print("Debug - 3",flush=True)
                    if frame is None:
                        print("Debug - cv2.imread() failed. frameNum =", str(CurrFrameNum), flush=True)
                        time.sleep(0.1)
                    else:
                        ...

It is noticed that the thread stop execution after running print("Debug - 2",flush=True). I guess opencv has raise an error which causes the thread to die.

So I have also checked if the thread is alive or not in a separate thread. It is noticed that the thread is still alive even the execution stopped as above. Therefore, I guess there is some problem with imread() that it causes the thread stop working and stuck without raising any errors.

I have tried to upgrade and downgrade both pyinstaller and opencv but the problem still cannot be resolved.

Does anyone know what's happening and how to fix it?

Thank you so much!

Justin
  • 1
  • 3
  • What is your program output as an error? – Yunus Temurlenk Jan 30 '20 at 11:44
  • No error has been outputted. The situation is that the thread works fine in the first 3000-6000 iterations but the thread stops executing suddenly after printing "Debug - 2" and it never continues. – Justin Jan 30 '20 at 11:48
  • From the little information provided, it seems to me that imread has encountered a problem, causing the thread to terminate. Things to check are: - what is the actual error of imread - is the file that you try to read actually existint - is the file accessible – Andreas Jan 30 '20 at 14:04
  • It seems to me that I cannot catch any errors from imread() as stated in [here](https://stackoverflow.com/questions/9131992/how-can-i-catch-corrupt-jpegs-when-loading-an-image-with-imread-in-opencv/), and I think I encounter similar situation as [here](https://answers.opencv.org/question/206653/error-handling-with-imread/) – Justin Feb 01 '20 at 02:02

0 Answers0