OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Capturing video from two cameras in OpenCV at once

  • Thread starter Thread starter Cerin
  • Start date Start date
C

Cerin

Guest
How do you capture video from two or more cameras at once (or nearly) with OpenCV, using the Python API?

I have three webcams, all capable of video streaming, located at /dev/video0, /dev/video1, and /dev/video2.

Using the tutorial as an example, capturing images from a single camera is simply:

Code:
import cv2
cap0 = cv2.VideoCapture(0)
ret0, frame0 = cap0.read()
cv2.imshow('frame', frame0)
cv2.waitKey()

And this works fine.

However, if I try to initialize a second camera, attempting to read() from it returns None:

Code:
import cv2
cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
ret0, frame0 = cap0.read()
assert ret0 # succeeds
ret1, frame1 = cap1.read()
assert ret1 # fails?!

Just to ensure I wasn't accidentally giving OpenCV a bad camera index, I tested each camera index individually and they all work by themselves. e.g.

Code:
import cv2
#cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
#ret0, frame0 = cap0.read()
#assert ret0
ret1, frame1 = cap1.read()
assert ret1 # now it works?!

What am I doing wrong?

Edit: My hardware is a Macbook Pro running Ubuntu. Researching the issue specifically on Macbooks, I've found others that have run into this problem too, both on OSX and with different types of cameras. If I access the iSight, both calls in my code fail.
<p>How do you capture video from two or more cameras at once (or nearly) with OpenCV, using the Python API?</p>

<p>I have three webcams, all capable of video streaming, located at /dev/video0, /dev/video1, and /dev/video2.</p>

<p>Using the <a href="http://docs.opencv.org/trunk/dd/d43/tutorial_py_video_display.html" rel="noreferrer">tutorial</a> as an example, capturing images from a single camera is simply:</p>

<pre><code>import cv2
cap0 = cv2.VideoCapture(0)
ret0, frame0 = cap0.read()
cv2.imshow('frame', frame0)
cv2.waitKey()
</code></pre>

<p>And this works fine.</p>

<p>However, if I try to initialize a second camera, attempting to <code>read()</code> from it returns None:</p>

<pre><code>import cv2
cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
ret0, frame0 = cap0.read()
assert ret0 # succeeds
ret1, frame1 = cap1.read()
assert ret1 # fails?!
</code></pre>

<p>Just to ensure I wasn't accidentally giving OpenCV a bad camera index, I tested each camera index individually and they all work by themselves. e.g.</p>

<pre><code>import cv2
#cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
#ret0, frame0 = cap0.read()
#assert ret0
ret1, frame1 = cap1.read()
assert ret1 # now it works?!
</code></pre>

<p>What am I doing wrong?</p>

<p>Edit: My hardware is a Macbook Pro running Ubuntu. Researching the issue specifically on Macbooks, I've found others that have run into this problem too, both on OSX and with different types of cameras. If I access the iSight, both calls in my code fail.</p>
 

Latest posts

Top