Ask Your Question
0

How to use thread with mutliple cam?

asked 2015-12-16 10:35:50 -0600

Tiras gravatar image

updated 2015-12-23 10:26:16 -0600

I successfully view 2 webcams parallely by using the multiple processes. As code below. I use dedicate USB bus each for single webcam.

import cv2
from multiprocessing import Process
def f(camID):
        vc = cv2.VideoCapture(camID)
        camIDstr = str(camID)
        cv2.namedWindow("preview" + camIDstr)
        if vc.isOpened(): # try to get the first frame
                rval,  frame  = vc.read()
        else:
                rval  = False

        while rval:
                cv2.imshow("preview" + camIDstr, frame)
                rval, frame = vc.read()
                key = cv2.waitKey(20)
                if key == 27: # exit on ESC
                        break
        cv2.destroyWindow("preview" + camIDstr)

if __name__ == '__main__':
        #f(0)
        '''
        t0 = threading.Thread(target=f, args= (0,))
        t0.start()
        t1 = threading.Thread(target=f, args= (1,))
        t1.start()
        '''
        p0 = Process(target=f, args=(0,))
        p0.start()
        p1 = Process(target=f, args=(1,))
        p1.start()

And here is my simplistic thread version.

import cv2
#from multiprocessing import Process
import threading
def f(camID):
        vc = cv2.VideoCapture(camID)
        camIDstr = str(camID)
        cv2.namedWindow("preview" + camIDstr)
        if vc.isOpened(): # try to get the first frame
                rval,  frame  = vc.read()
        else:
                rval  = False

        while rval:
                cv2.imshow("preview" + camIDstr, frame)
                rval, frame = vc.read()
                key = cv2.waitKey(20)
                if key == 27: # exit on ESC
                        break
        cv2.destroyWindow("preview" + camIDstr)

if __name__ == '__main__':
        #f(0)
        t0 = threading.Thread(target=f, args= (0,))
        t0.start()
        t1 = threading.Thread(target=f, args= (1,))
        t1.start()

Update :

For further information. When I cut all the function and let my main function just this.

   for i in range(0,11):
      vc = cv2.VideoCapture(i)
      if vc.isOpened():
         print i

The available indexes are 0 and 1.

    0
    1
    VIDEOIO ERROR: V4L: index 2 is not correct!
    VIDEOIO ERROR: V4L: index 3 is not correct!
    VIDEOIO ERROR: V4L: index 4 is not correct!
    VIDEOIO ERROR: V4L: index 5 is not correct!
    VIDEOIO ERROR: V4L: index 6 is not correct!
    VIDEOIO ERROR: V4L: index 7 is not correct!
    VIDEOIO ERROR: V4L: index 8 is not correct!
    VIDEOIO ERROR: V4L: index 9 is not correct!
    VIDEOIO ERROR: V4L: index 10 is not correct!

Problem :
Program retunrs 1 line with error message as follows.

VIDEOIO ERROR: V4L: index 1 is not correct!

Question :
How come V4L says my index is incorrect?
If they are not 0 or 1. What number it will be?

New Question :
Is it possible to create stereo camera by using thread?
Or I need process?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-12-16 23:34:56 -0600

Tetragramm gravatar image

updated 2015-12-16 23:36:11 -0600

Looking at the documentation for VideoCapture, it says "If there is a single camera connected, just pass 0." So you may need to do 1 and 2. It is likely that passing 0 gives the same camera as 1, and you can't connect to the same camera twice.

edit flag offensive delete link more

Comments

When I reach my computer I will inform you back here. Put index 1 and 2 is never come up in my mind.

Tiras gravatar imageTiras ( 2015-12-17 02:29:56 -0600 )edit

VIDEOIO ERROR: V4L: index 2 is not correct!

Tiras gravatar imageTiras ( 2015-12-17 09:20:17 -0600 )edit

Ok, I did some playing around, and looking at other people's answers to similar questions. There are two possible reasons.

  1. You might be overloading your USB bus. There's a fairly low maximum. Often the front USB ports, and the back USB ports are on different buses, so try moving them.
  2. Sometimes the camera list is odd. Try creating the devices with the default constructor (), then calling .open(index) It should return true if it opened successfully. If it fails, increase the index you're trying with. You would need to get back the number of the first thread and start the second from one index higher, unless it's safe to try to open the same index twice, which it may be. I doubt there's more than 10 device slots, so going up to 10 should cover it.
Tetragramm gravatar imageTetragramm ( 2015-12-17 16:34:00 -0600 )edit

@Tetragramm Thank you for your comments. I had avoided USB overloading. That's why I can run 2 processces of webcam viewer simulataneously. and for the 2nd comment I had tried open calling same index twice. I called zero twice. Then got This
libv4l2: error setting pixformat: Device or resource busy VIDEOIO ERROR: libv4l unable to ioctl S_FMT libv4l2: error setting pixformat: Device or resource busy libv4l1: error setting pixformat: Device or resource busy VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT

Tiras gravatar imageTiras ( 2015-12-20 05:53:57 -0600 )edit

Did you try opening zero in the first thread, then trying values 1-10 in the second thread until .open(int) returns true?

Tetragramm gravatar imageTetragramm ( 2015-12-20 18:12:33 -0600 )edit

I tried (0,1), (1,2), and (0,0) as last comment. But I stop to running the camID since I am so afraid I am going to break something since I have no idea about the returned error at all. I will follow you reply (0,1), (0,2), ...(0,10).

Tiras gravatar imageTiras ( 2015-12-20 19:48:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-16 10:35:50 -0600

Seen: 3,345 times

Last updated: Dec 23 '15