简体   繁体   中英

Why does my python script crash every time I run OpenCV function?

I'm using a MacBook Air and whenever I run this code the system shows some error on a popup window with a long error message like this:

运行代码后弹出窗口

And in the terminal it shows:

"Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

Here is the code:

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()

I tried with IDE's like PyCharm and VS Code. When I tried with Jupyter Notebook, it worked until I end the video capturing but again it crashed when I try to kill the video window.

您可能内存不足。

I am not using Catalina and I can't reproduce your error, but starting Mojave, Apple now requires users to explicitly allow applications to access the camera.

See Control access to your camera on Mac :

Some apps and websites can use your camera to take photos or video. You can decide which apps are allowed to use the camera on your Mac.

  1. On your Mac, choose Apple menu > System Preferences, click Security & Privacy, then click Privacy.

  2. Select Camera.

  3. Select the tickbox next to an app to allow it to access your camera.

    Deselect the tickbox to turn off access for that app.

Check that PyCharm and VS Code have permissions to access the camera. Furthermore, your Terminal should already have access to the camera, so if it works in the Terminal but not in IDEs, then enabling permissions for IDEs could work.

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