我是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()
我想提两个建议
#1:启用终端或PyCharm以到达摄像头
转到
System Preferences
->Security and Privacy
->Camera
并将PyCharm添加到列表中#2使用
while cap.isOpened()
代替while True
,这样您就可以知道PyCharm
或terminal
可以访问您的相机我有同样的问题,我没有找到任何解决办法。通过反复试验,我发现我的
OpenCV
版本已损坏。因此,我删除了它并安装了一个新的。您可以使用以下两个选项之一: 1。终端 运行brew uninstall opencv3
卸载opencv
然后使用sudo apt-get install libopencv-dev python-opencv
命令安装它2。Python 实际上,我用这个方法解决我自己的问题
opencv
: 选择opencv
包并卸载它。然后通过选择“未安装”并搜索opencv
,尝试重新安装opencv
。请小心安装正确的版本。相关问题 更多 >
编程相关推荐