我是 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
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)
已经尝试将 VideoCapture(0) 更改为 VideoCapture(1) 并添加以下代码,但仍然没有帮助。顺便说一句,使用 PyCharm
cap.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)