2

OpenCV fails to open VideoCaptures for more than 8 webcams on Linux. Here a simple example:

# "opencap.py"
import cv2, sys
dev = int(sys.argv[1])
cap = cv2.VideoCapture(dev)
print "device %d: %s" %(dev, "success" if cap.isOpened() else "failure")

For my setup (OpenCV 2.4.11, Ubuntu 14.04) with, say, 9 webcams, opencap.py succeeds for the first 8 webcams (0-7), but for the last one I get

> python opencap.py 8
HIGHGUI ERROR: V4L: index 8 is not correct!
device 8: failure

Note: v4l2-ctl --list-devices correctly lists the 9 webcams (/dev/video0, ..., /dev/video8).

Ulrich Stern
  • 10,761
  • 5
  • 55
  • 76
  • I think the GPU cannot handle the 8 webcams, especially in case the webcams have high resolution. – Tes3awy Jul 27 '16 at 22:56
  • My application is real-time tracking of _Drosophila_. The tracker uses _CPU_ only currently and only about 12% of an i7-4930K when tracking with 8 webcams, each at 320x240 pixels and 7.5 fps. – Ulrich Stern Jul 28 '16 at 05:55
  • Then I am wrong. I will think of something else – Tes3awy Jul 28 '16 at 08:10
  • I just checked the CPU load more carefully with `top` and `vmstat` during an experiment with 12 webcams, and got just 7% CPU (mostly user; system is about 0.4%). This is for "experiment mode" where the tracker also displays frames and real-time heatmaps and writes M-JPEG movies. The 12% CPU number for 8 webcams was from some ppt I wrote a few months back; glad I looked again. :) – Ulrich Stern Jul 28 '16 at 15:57
  • I don't understand a single word :D!!. Could you please simplify so that I can understand what you mean – Tes3awy Jul 28 '16 at 16:12
  • In a nutshell, the tracker uses only 7% of an i7-4930K when tracking with 12 webcams. – Ulrich Stern Jul 28 '16 at 16:39
  • @ ooh okay I got know. I read the previous comment again after the last one and I got it. Thanks :) – Tes3awy Jul 28 '16 at 16:42

1 Answers1

6

The problem is caused by this line in the OpenCV source code:

#define MAX_CAMERAS 8

Simply changing the MAX_CAMERAS value and rebuilding OpenCV solves the problem. The file to change is modules/highgui/src/cap_libv4l.cpp (line 260) for a libv4l build, and cap_v4l.cpp for a v4l build. (See, e.g., this answer for more on the two build options.) For OpenCV 3.0, the directory changed to modules/videoio/src/.

Note: typically one runs into USB bandwidth issues with webcams before reaching the 8-camera limit. See, e.g., this answer.

Community
  • 1
  • 1
Ulrich Stern
  • 10,761
  • 5
  • 55
  • 76