5
votes

So I'm just trying to run the basic OpenCV program

    import numpy as np
    import cv2

    cap = cv2.VideoCapture(0)

    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)

        # Display the resulting frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

But for some reason when I try to run it (with python 2 or 3) I get this weird abort statement

    [1]    74309 abort      python3 index.py

(I'm guessing the 5 digit number is the PID) However, it does work if I pass in a path to an already existing video in the VideoCapture function. I am a beginner in all of this so I'm not really sure what the problem is

Thanks :)

4
What kind of camera do you have hooked up to your computer? Built-in webcam? Presumably the camera works otherwise, yeah? - alkasm
I have a Logitech C920 and yeah it works fine in other camera apps. The built-in camera also get the same result (I do also change video capture param to 1 or 0 depending on which one I try) - DJLad97
I'm not sure if OpenCV's video capture for webcam uses ffmpeg or not, but you maybe could try capturing directly with ffmpeg and see if you also encounter an error: trac.ffmpeg.org/wiki/Capture/Webcam, not sure if it will help but might give others some additional information. - alkasm
Weird, I seem to be getting the same error when trying to use ffmpeg as well (the abort statement). Could it be something do with my actual device? (I did update to macOS Mojave...) - DJLad97
It's possible, but that's about the limits of my knowledge as I don't play with video devices like this. You may consider editing your question or posting a new one for FFMPEG specifically, since that's the underlying issue (at least I think it is). Hopefully someone else will be able to help more. Good luck! - alkasm

4 Answers

12
votes

I found the solution! I tried running the script using the default terminal that comes with mac and it worked :) So it seems there was some weird issue with the third party terminal (iTerm) I was using

1
votes

You have to authorize iTerm to access the camera (in System Preferences). By default, neither Terminal nor iTerm has this permission. macOS Mojave just asks to allow iTerm and your Python program works fine! It's not a problem with iTerm, or maybe was a problem.

1
votes

I had the same problem running OpenCV from VSCode. It turned out to be indeed permission error for accessing the camera on the new macOS. I couldn't get VSCode to ask for the permission (or add it manually through system preferences).

My solution was to run VSCode as root using

sudo /Applications/Visual Studio Code.app/Contents/MacOS/Electron

It's not the cleanest solution, but it worked for me.

0
votes

In VSCode ,

1.type cmd+shift+p 
2.type "shell command: Install code in PATH"
3. Close vscode
4. Use "sudo code" to open vscode 
5. It will give warning not to run as a root user
6. Ignore the warning and run the file , you will not get the "Abort trap: 6" error anymore.