简体   繁体   中英

Pycharm - Running Open CV Code goes straight to “Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)”

I am trying to run open-cv code to get access to my webcam in a python script. However, when I try to run it I get "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)" every time I try to run it. There is no error in my code and I also looked at other posts to add the environment variables: PYTHONUNBUFFERED=1;PYDEVD_USE_FRAME_EVAL=NO;PYTHONMALLOC=debug My code is:

import cv2

# define a video capture object 
vid = cv2.VideoCapture(0)
while (True):
    # Capture the video frame
    # by frame
    ret, frame = vid.read()

    # display the resulting frame
    cv2.imshow('frame', frame)

    # the 'q button is set as the
    # quitting button
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # after the loop realse the cap object
    vid.release()
    # destroy all windows
    cv2.destroyAllWindows() 

Last two lines must be outside while loop:

import cv2

# define a video capture object 
vid = cv2.VideoCapture(0)
while (True):
    # Capture the video frame
    # by frame
    ret, frame = vid.read()

    # display the resulting frame
    cv2.imshow('frame', frame)

    # the 'q button is set as the
    # quitting button
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# after the loop realse the cap object
vid.release()
# destroy all windows
cv2.destroyAllWindows()

To add to this.

SIGSEGV is a memory access violation, I find this error a lot when using code that comes from a c++ library. Essentially you are trying to access a object in memory that has been deleted but the reference to it still remains.

When you call these

`# after the loop realse the cap object
vid.release()
# destroy all windows
cv2.destroyAllWindows()`

Opencv must essentially be deleting the object in the memory.

Then when you do the next iteration of your while loop you try to access them with this.

`ret, frame = vid.read()

# display the resulting frame
cv2.imshow('frame', frame)`

But the video capture object and the window are not there (but the reference to the python object remains).

And then boom, crash. Because it fails to find the object and doesn't know what to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM