Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak with VideoCapture in Python OpenCV

I am using 3 webcams to occasionally take snapshots in OpenCV. They are connected to the same usb bus, which does not allow for all 3 connections at the same time due to usb bandwidth limitations (lowering the resolutions allows at most 2 simultaneous connections and I don't have more usb buses).

Because of this, I have to switch webcam connections every time I want to take a snapshot, but this causes a memory leak after some 40 switches.

This is the error I get:

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

This is a simple piece of code that generates this error:

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()

Maybe a worthy note is that I get VIDIOC_QUERYMENU: Invalid argument errors every time the camera connects, although I can then still use it.

As some extra info, this is my v4l2-ctl -V output of the webcam:

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

What causes these errors and how can I fix them?

like image 633
RemiX Avatar asked Feb 05 '14 11:02

RemiX


People also ask

What is VideoCapture in OpenCV?

OpenCV VideoCaptureOpenCV provides the VideoCature() function which is used to work with the Camera. We can do the following task: Read video, display video, and save video. Capture from the camera and display it.

What is cv2 VideoCapture in Python?

cv2.VideoCapture(1): Means second camera or webcam. cv2.VideoCapture("file name.mp4"): Means video file. After this, we can start reading a Video from the camera frame by frame. We do this by calling the read method on the VideoCapture object. This method takes no arguments and returns a tuple.

What does cv2 VideoCapture return?

Reading ( cam. read() ) from a VideoCapture returns 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 .


1 Answers

The relevant snippet of the error message is Unrecognised or unsupported array type in function cvGetMat. The cvGetMat() function converts arrays into a Mat. A Mat is the matrix data type that OpenCV uses in the world of C/C++ (Note: the Python OpenCV interface you are utilising uses Numpy arrays, which are then converted behind the scenes into Mat arrays). With that background in mind, the problem appears to be that that the array im you're passing to cv2.imshow() is poorly formed. Two ideas:

  1. This could be caused by quirky behaviour on your webcam... on some cameras null frames are returned from time to time. Before you pass the im array to imshow(), try ensuring that it is not null.
  2. If the error occurs on every frame, then eliminate some of the processing that you are doing and call cv2.imshow() immediately after you grab the frame from the webcam. If that still doesn't work, then you'll know it's a problem with your webcam. Else, add back your processing line by line until you isolate the problem. For example, start with this:

    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;
    

source: Related Question: OpenCV C++ Video Capture does not seem to work

like image 53
bob marti Avatar answered Oct 10 '22 07:10

bob marti