Python cv2.videocapture.read() 不返回一个 numpy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13989627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:06:11  来源:igfitidea点击:

cv2.videocapture.read() does not return a numpy array

pythonopencvv4l2ftputil

提问by GrixM

I have this code trying to capture a frame from my webcam on raspberry pi, and saving it as an image. I use opencv 2, but I get strange errors when I run the code..

我有这段代码试图从树莓派上的网络摄像头捕获帧,并将其保存为图像。我使用 opencv 2,但是当我运行代码时出现奇怪的错误..

import time
import sys
from subprocess import call
import cv2

cam = cv2.VideoCapture()

while True:
        cam.open(-1)
        image = cam.read()
        cv2.imwrite("current.jpeg",image)
        time.sleep(10);

This is what the program returns:

这是程序返回的内容:

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument

Traceback (most recent call last):
  File "kvamskogen.py", line 18, in <module>
    cv2.imwrite("current.jpeg",image)
TypeError: <unknown> is not a numpy array

What is wrong here?

这里有什么问题?

采纳答案by mmgp

Reading (cam.read()) from a VideoCapturereturns a tuple (return value, image). With the first item you check wether the reading was successful, and if it was then you proceed to use the returned image.

cam.read()从 a 中读取 ( )VideoCapture返回一个元组(return value, image)。使用第一项检查读取是否成功,如果成功,则继续使用返回的image.

This is documented at http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html

这记录在http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html

回答by train1855

Everything mmgp said is spot-on; cam.read()returns first a boolean indicating whether the read was successful, and then the image itself (which will be empty if the return value was False). Also note that if you're not using the return value for anything, you can just set that portion to _, which tells Python "ignore me"; that line would then look something like _, image = cam.read(). Additionally, it is generally good practice to specify the index at which your camera is located (usually 0 if you have only one camera connected) when calling cv2.VideoCapture(), so that, in the event that you do have multiple cameras connected, OpenCV knows which camera to read from (otherwise it might just crash because it doesn't know what to do).

mmgp 所说的一切都是正确的;cam.read()首先返回一个布尔值,指示读取是否成功,然后返回图像本身(如果返回值为 ,则为空False)。另请注意,如果您不使用任何返回值,则可以将该部分设置为_,这会告诉 Python“忽略我”;那条线看起来像_, image = cam.read(). 此外,在调用 时指定您的相机所在的索引(通常为 0,如果您只连接了一个相机)通常是一个好习惯cv2.VideoCapture(),这样,如果您确实连接了多个相机,OpenCV 知道要连接哪个相机读取(否则它可能会崩溃,因为它不知道该怎么做)。

回答by Garry

You should use arguments in cv2.VideoCapture()

你应该在 cv2.VideoCapture()

Try this to capture from the default camera

试试这个从默认相机捕捉

cam = cv2.VideoCapture(0)

Try this to capture from ip camera

试试这个从网络摄像机捕捉

cam = cv2.VideoCapture('http://ip-address') # to check video source's ip address right click video and select "copy image address" and put the exact address in above line of code