无法通过OpenCV python打开Mac网络摄像头

2024-10-01 13:29:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我是opencv新手,试图通过opencv python访问Macbook的内置摄像头,但它给出了一个错误

import cv2

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)

while True:
   success, img = cap.read()
   cv2.imshow("Result", img)
   if cv2.waitKey(1) & 0xFF == ord('q'):
       break

Traceback (most recent call last):
  File "/Users/hasanaktas/PycharmProjects/OpencvPython/project3.py", line 12, in <module>
    cv2.imshow("Result", img)
cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

已尝试将VideoCapture(0)更改为VideoCapture(1)并添加以下代码,但仍然没有帮助。顺便说一句,使用PyCharm

cap.release()
cv2.destroyAllWindows()

Tags: inimgsizeerrorresultcv2opencvusers
2条回答

我想提两个建议

#1:启用终端或PyCharm以到达摄像头


  • 转到System Preferences->Security and Privacy->Camera并将PyCharm添加到列表中

    • enter image description here

#2使用while cap.isOpened()代替while True,这样您就可以知道PyCharmterminal可以访问您的相机

  • import cv2
    
    frameWidth = 640
    frameHeight = 480
    cap = cv2.VideoCapture(0)
    cap.set(3, frameWidth)
    cap.set(4, frameHeight)
    cap.set(10,150)
    
    while cap.isOpened():
        success, img = cap.read()
        if success:
            cv2.imshow("Result", img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    

我有同样的问题,我没有找到任何解决办法。通过反复试验,我发现我的OpenCV版本已损坏。因此,我删除了它并安装了一个新的。您可以使用以下两个选项之一: 1。终端 运行brew uninstall opencv3卸载opencv 然后使用sudo apt-get install libopencv-dev python-opencv命令安装它

2。Python 实际上,我用这个方法解决我自己的问题

  1. 敞口Python
  2. 转到“环境”部分,选择您的环境,如下所示: enter image description here 然后单击installed并搜索opencventer image description here 选择opencv包并卸载它。然后通过选择“未安装”并搜索opencv,尝试重新安装opencv。请小心安装正确的版本。 enter image description here

相关问题 更多 >