Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use thread with mutliple cam?

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

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?

How to use thread with mutliple cam?

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

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?

How to use thread with mutliple cam?

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?

How to use thread with mutliple cam?

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. this.

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

The available indexes are 0 and 1. 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!

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?