OpenCV Python脚本Mac“中止”

问题描述 投票:3回答:2

所以我只是想运行基本的OpenCV程序

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

但是由于某些原因,当我尝试运行它(使用python 2或3)时,我得到了这个奇怪的中止语句

    [1]    74309 abort      python3 index.py

(我猜5位数是PID),但是,如果我在VideoCapture函数中传递到已经存在的视频的路径,它确实可以工作。我是这方面的初学者,所以我不确定是什么问题

谢谢:)

python opencv macos-mojave
2个回答
5
投票

我找到了解决方案!我尝试使用mac随附的默认终端运行脚本,并且该脚本有效:)因此,似乎我使用的第三方终端(iTerm)出现了一些奇怪的问题]


1
投票

您必须授权iTerm访问摄像机(在系统偏好设置中)。默认情况下,Terminal和iTerm都不具有此权限。 macOS Mojave只是要求允许iTerm,您的Python程序运行良好!iTerm没问题,或者was

© www.soinside.com 2019 - 2024. All rights reserved.