Mat不是数字元组openCV 2

时间:2018-05-20 03:32:43

标签: python python-3.x opencv typeerror

我是python和openCV的新手,我正在尝试创建一个程序,从Macs的网络摄像头抓取输入并将其显示在窗口中,最终可以处理和编辑这些帧。这是我的代码:

import cv2
import numpy as nmp
capture=cv2.VideoCapture(0)

while True:
    frame = capture.read()
    cv2.imshow("Webcam", frame)
    if (cv2.waitKey(0)):
        break

cv2.release()
cv2.destroyAllWindows()

我的摄像头附近的灯亮了,但程序停止并出现以下错误

Traceback (most recent call last):
  File "/Users/spinder/Desktop/WebCam.py", line 7, in <module>
    cv2.imshow("Webcam", frame)
TypeError: mat is not a numerical tuple

这里有类似的问题,但它们不能解决我的问题,任何建议,修复或解决方法都会非常感激。

1 个答案:

答案 0 :(得分:1)

根据docs

  

Python:cv2.VideoCapture.read([image])→retval,image

返回2个值,第一个表示帧是否正确获取,第二个是帧。因此,在您的情况下,代码应如下所示:

import cv2
import numpy as nmp

capture=cv2.VideoCapture(0)

while True:
    res, frame = capture.read()
    if res:
        cv2.imshow("Webcam", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
cv2.destroyAllWindows()