13

我使用 3 个网络摄像头偶尔在 OpenCV 中拍摄快照。它们连接到同一个 USB 总线,由于 USB 带宽限制,它不允许同时进行所有 3 个连接(降低分辨率最多允许 2 个同时连接,我没有更多的 USB 总线)。

因此,每次我想要拍摄快照时都必须切换网络摄像头连接,但这会在大约 40 次切换后导致内存泄漏。

这是我得到的错误:

libv4l2: error allocating conversion buffer
mmap: Cannot allocate memory
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument

Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
libv4l1: error allocating v4l1 buffer: Cannot allocate memory
HIGHGUI ERROR: V4L: Mapping Memmory from video source error: Invalid argument
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial memory buffers.
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or 
unsupported array type) in cvGetMat, file 
/build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482

Traceback (most recent call last):
File "/home/irobot/project/test.py", line 7, in <module>
cv2.imshow('cam', img)
cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482: 
error: (-206) Unrecognized or unsupported array type in function cvGetMat

这是产生此错误的一段简单代码:

import cv2

for i in range(0,100):
    print i
    cam = cv2.VideoCapture(0)
    success, img = cam.read()
    cv2.imshow('cam', img)
    del(cam)
    if cv2.waitKey(5) > -1:
        break

cv2.destroyAllWindows()

也许值得注意的是,VIDIOC_QUERYMENU: Invalid argument每次连接相机时都会出错,尽管我仍然可以使用它。

作为一些额外的信息,这是我v4l2-ctl -V的网络摄像头输出:

~$ v4l2-ctl -V
Format Video Capture:
Width/Height  : 640/480
Pixel Format  : 'YUYV'
Field         : None
Bytes per Line: 1280
Size Image    : 614400
Colorspace    : SRGB

是什么导致了这些错误,我该如何解决?

4

1 回答 1

1

错误消息的相关片段是 Unrecognized or unsupported array type in function cvGetMat。该cvGetMat()函数将数组转换为 Mat。OpenCVMat 是C/C++ 世界中使用的矩阵数据类型(注意:OpenCV您使用的 Python 接口使用 Numpy 数组,然后在后台将其转换为 Mat 数组)。考虑到这一背景,问题似乎是您传递给的数组 im 格式不正确cv2.imshow()。两个想法:

  1. 这可能是由您的网络摄像头上的古怪行为引起的……在某些摄像头上,有时会返回空帧。在将 im 数组传递给 之前imshow(),请尝试确保它不为空。
  2. 如果错误发生在每一帧上,则消除您正在执行的一些处理,并cv2.imshow()在您从网络摄像头抓取帧后立即调用。如果这仍然不起作用,那么您就会知道这是您的网络摄像头的问题。否则,逐行添加您的处理,直到您找出问题所在。例如,从这个开始:

    while True:
    
    
    # Grab frame from webcam
    retVal, image = capture.read(); # note: ignore retVal
    
    #   faces = cascade.detectMultiScale(image, scaleFactor=1.2, minNeighbors=2, minSize=(100,100),flags=cv.CV_HAAR_DO_CANNY_PRUNING);
    
    # Draw rectangles on image, and then show it
    #   for (x,y,w,h) in faces:
    #       cv2.rectangle(image, (x,y), (x+w,y+h), 255)
    cv2.imshow("Video", image)
    
    i += 1;
    

来源:相关问题:OpenCV C++ Video Capture 似乎不起作用

于 2017-02-13T11:58:53.350 回答