I am just wondering how many USB cameras can be accessed by one desktop PC? Is there any limit? I am planning to create my own Windows application (using .NET) to capture ar
a bit late sorry :) What i found out is that a single USB card is limited by the USB bandwidth. but.. if you add USB cards on the PCI you can get more cameras but... most vendors do not bother to alter the USB card address the computer see so you need to buy USB to PCI cards from different vendors and try your luck. I had the same problem with firewire. here is my code for python. (thank other programmers on stackoverflow)
# show multiple usb cameras
import os
import cv2
import threading
import time
import datetime
#font for image writing
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (255,180,180)
lineType = 2
SaveImage = True # if true save images
duration = [100,100,100,10,10] # time between image saves in sec
IMAGESAVEPATH = "C:/tmp/pix" # path for camera to store image to
ShowText = True #Show text on image - text will be saved with the image
#camera thread. here me make a thread and its functions
class camThread(threading.Thread):
def __init__(self, previewName, camID):
threading.Thread.__init__(self)
self.previewName = previewName
self.camID = camID
def run(self):
print ("Starting " + self.previewName)
camPreview(self.previewName, self.camID)
#camera main loop - here we init the specific camera and start it then have a window to show the image and we store the image to the right directory
def camPreview(previewName, camID):
cv2.namedWindow(previewName)
cam = cv2.VideoCapture(camID) #start the camera (the cameras are numbered by the order they are connected to the computer)
if cam.isOpened(): # try to get the first frame
cam.set(3,4000) #this will bring the largest frame set
cam.set(4,4000)
cam.set(5,1) #fps
time.sleep(2)
cam.set(15, -1.0)
rval, frame = cam.read() #read the image
else:
rval = False
TStart = time.time() # time for next image
mpath = os.path.join(IMAGESAVEPATH, str(camID)) #make sure the directory we save in exists, otherwise make it
print("try to make dir ", mpath, " T " , time.time())
if not os.path.exists(mpath):
os.makedirs(mpath)
cv2.namedWindow(previewName, cv2.WINDOW_NORMAL)
while rval: #if we get an image
height, width, channels = frame.shape
if ShowText: # write text on the image
caption = str(camID) + " - " + str(height) + " " + str(width) + " "
cv2.putText(frame,str(caption),(20,20),font, fontScale, fontColor, lineType)
cv2.imshow(previewName, frame) # show image in its window
#cv2.resizeWindow(previewName, 1280,960) # resize all windows removed ofer
rval, frame = cam.read() #raed next image
key = cv2.waitKey(20)
if key == 27: # exit on ESC
print("key pressed ", camID)
break
TDiff = int(time.time() - TStart) # time difference from last image
if (SaveImage and TDiff > duration[camID]): # Save if time passed
file_name = os.path.join(mpath, "T{:%Y.%m.%d %H-%M-%S}.jpg".format(datetime.datetime.now())) # make file name string
cv2.imwrite(file_name, frame)
print("\rsaved to : ", file_name)
TStart = time.time() #reset time to next image
cv2.destroyWindow(previewName)
# Create 5 threads as follows
thread1 = camThread("Camera 1", 0)
thread2 = camThread("Camera 2", 1)
thread3 = camThread("Camera 3", 2)
thread4 = camThread("Camera 4", 3)
thread5 = camThread("Camera 5", 4)
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()