Why is OpenCV with sublime text 3 and python 3.8 quitting unexpectedly?

Viewed 513

I am trying to use OpenCV to video capture my webcam. Each time I run the program, python quits unexpectedly, and the code says [Finishd in 0.3s with exit code -6] Operating System - Catalina 10.15.3 Using sublime text 3 Python 3.8

import cv2

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()
2 Answers

The problem is that Sublime's build systems can't handle GUI window creation like cv2.imshow(). I believe it has to do with how the build system is executed using the subprocess module. You'd run into the same problem if you were trying to display an image using Pillow or matplotlib, for example. Please note that you can do image processing just fine within Sublime, as long as you don't try to display the results.

The easiest way around it is to just keep a Terminal window open and manually run your scripts from the command line after saving.

It would probably help to add some error handling to your code. For instance, after

cap = cv2.VideoCapture(0)

Try to put something like this:

if !cap.isOpened():
    print("Error")
    exit(1)

Then, after

ret, frame = cap.read()

try:

if !ret:
    print("Error: frame not captured")

At least this should give you some hints as to where the problem is.

Good luck

Andreas

Related