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?
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:
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.
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
Related
I expected to receive a video feed in my laptop from my phone. In my opinion, the code is correct. The same code is running on other devices. But when I tried doing the same in my laptop, it displayed the following error:
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap.cpp (142) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): http://192.168.1.3:8080/video in function 'cv::icvExtractPattern'
Since I am a newbie to the world of opencv, I could'nt understand what caused the error. Hence, I did'nt try anything worthwhile other than changing a few lines of code.
My code is:
import cv2
url= 'http://192.168.1.3:8080/video'
cap=cv2.VideoCapture(url)
cap.set(3,240)
cap.set(4,240)
while(True):
ret,frame=cap.read()
if frame is not None:
cv2.imshow('frame',frame)
q=cv2.waitKey(1)
if q==ord("q"):
break
cv2.destroyAllWindows()
cap.release()
I had encountered the same issue but with a a saved video file as the input.
The file path I had entered did no exist and I got the same error.
I would suggest to check if your have given the right permissions to for accessing your phone's camera and if the feed is live and is returning images in the correct format.
I am trying to use epiphan SDI2USB with ubuntu 18.04. I downloaded the correct driver from their support site and the driver is correctly installed. My kernel version is 4.15.0.
If I try to run vlc and i go to media menu --> Open Capture Device --> video device name --> Here I see both my /dev/video0 (integrated camera) and /dev/video1 (epiphan video) When I select /dev/video1 and click play, I can see it works fine.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
However when I write a simple python program from internet (i changed source from 0 to 1) and run it, I get an error
compass-admin#Alienware-13-R3:~$ sudo python3 cam.py
Insufficient buffer memory on /dev/video1 – decreasing buffers
Insufficient buffer memory on /dev/video1 – decreasing buffers
mmap: Invalid argument
munmap: Invalid argument
VIDEOIO ERROR: V4L: can’t open camera by index 1
munmap: Invalid argument
Traceback (most recent call last):
File “/home/compass-admin/.local/lib/python3.6/site-packages/numpy/lib/shape_base.py”, line 843, in split
len(indices_or_sections)
TypeError: object of type ‘int’ has no len()
I dont know if it is because of permissions or if you have to use a specific function in opencv. No matter what I do, I get the same error.
I changed permissions of /dev/video1 to 777, I also added the current user to video group but no luck. I also tried video source in opencv as 0, 1, -1 but no luck. Any help please.
I have use codes from this link and sucessfully done the detection but the problem is it is only from webcam. I tried to modify the code so that it can read from file. the part I have modified is : I have written this
print("[INFO] starting video stream...")
vs= cv2.VideoCapture('cars.avi')
time.sleep(2.0)
fps = FPS().start()
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()
instead of this (code from the above link)
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
fps = FPS().start()
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()
For running the program from terminal I am using this command for both the cases:
python real_time_object_detection.py --prototxt
MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel
The error I am getting when reading from file is
the error I am getting is :
C:\Users\DEBASMITA\AppData\Local\Programs\Python\Python35\real-time-object-
detection>python videoobjectdetection.py --prototxt
MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel
[INFO] loading model...
Traceback (most recent call last):
File "videoobjectdetection.py", line 54, in <module>
frame = imutils.resize(frame, width=400)
File "C:\Users\DEBASMITA\AppData\Local\Programs\Python\Python35\lib\site-
packages\imutils\convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'tuple' object has no attribute 'shape'
I don't know where I am doing wrong. Please guide me.
I am unfamiliar with any of the code you are referencing, but the error is straightforward and similar errors hav been answered in other questions: You're trying to do a fancy method on a plain tuple object. Here's an example of this python concept using a common package, numpy for arrays:
#an example of the error you are getting with a plain tuple
>>>tup = (1,2,3,4)
>>>len(tup)
4
>>> tup.shape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'shape'
#an example that uses an attribute called 'shape'
>>> import numpy as np
>>> x = np.array([1,2,3,4])
>>> x.shape
(4,)
>>> x.shape.shape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'shape'
As you can see in my last two lines, the first time I call .shape on the numpy array, the call is valid. This call returns a tuple, so the last call to .shape.shape is invalid, it is operating on (4,). As for how to fix it? I don't know. For example, in this question the original poster thought that they were getting back some kind of image object, instead they were getting a tuple (maybe a tuple of image objects). Something similar is happening to you: Your VideoStream.read() call is returning a tuple. So when you call imutils.resize(frame, width=400) you are passing in a tuple, not an image or frame. So when that method tries to call .shape you get the error. VideoStream.read() may return a tuple by design, or an error condition. You'd have to read up on VideoStream to be sure.
I am writing something in which I need the webcam stream and do some face detection on it. Everything works fine, but I get an error message printed in the terminal, despite of me using cv.CaptureFromCAM(-1) which detects any camera connected to my machine.
I also tried using cv2 instead of cv, and I get the same error:
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
Note that the only code I am running currently is cv.CaptureFromCAM(-1).Oh and I am using python 2.7.
import cv
cv.CaptureFromCAM(-1)
Can someone tell me how to fix this?
Thanks.
I ran into the same error messages. In my case however they are not fatal, just an indication that python is using v4l2 to probe parameters that happen to be invalid for my camera / os combination.
In my case
>>> cap = cv2.VideoCapture(0)
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
...
But
ret, frame = cap.read()
gives ret=true and a numpy image-array for frame.
In my case the video camera is a Logitech, Inc. HD Webcam C910. And I am running Angstrom on a beagleboneblack.
try:
import cv
cv.NamedWindow("Capture", 1)
cap_1 = cv.CaptureFromCAM(-1)
While True:
Frame = cv.QueryFrame(cap_1)
cv.ShowImage("Capture", Frame)
cv.WaitKey(1)
also, using cv2 and VideoCapture:
from cv2 import *
cam = VideoCapture(0)
s, img = cam.read()
EDIT: I found the following information while googling about this error:
The messages: ‘VIDIOC_QUERYMENU: Invalid argument’ come from opencv cap_libv4l.cpp
file.
The ‘querymenu.id” value still has the “V4L2_CTRL_FLAG_NEXT_CTRL” set when enumerating
the menu items.
Following patch solves this:
— OpenCV-2.3.1/modules/highgui/src/cap_libv4l.cpp 2011-09-12 20:41:29.000000000 +0200
+++ opencv/modules/highgui/src/cap_libv4l.cpp 2011-12-01 19:00:37.503761631 +0100
## -489,7 +489,7 ##
{
// printf (” Menu items:\n”);
CLEAR (capture->querymenu);
- capture->querymenu.id = capture->queryctrl.id;
+ capture->querymenu.id = capture->queryctrl.id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
for (capture->querymenu.index = capture->queryctrl.minimum;
(int)capture->querymenu.index queryctrl.maximum;
capture->querymenu.index++)
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..
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?
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.
This is documented at https://opencv-tutorial.readthedocs.io/en/latest/intro/intro.html#capture-live-video
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).
You should use arguments in 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
If you are trying to capture video using inbuilt webcam then following code line will give you best results
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)