User avatar
amin137398
Posts: 8
Joined: Sat May 14, 2016 7:46 am
Location: Iran

Multiple cameras on a single raspberry pi

Wed May 18, 2016 5:06 am

hi
can u guys help me with this?
I have a raspberry pi 2 B and I want to run 2 cameras with it . ( one using default flat camera port on the board and the other using USB port )
is this possible? can the CPU handle such a thing?
if yes , how? what do I need to do?
thanks
-_-

MarkR
Posts: 128
Joined: Fri Jan 25, 2013 1:55 pm

Re: Multiple cameras on a single raspberry pi

Wed May 18, 2016 10:00 am

It's possible to add multiple USB cameras, with suitable hubs, although you won't be able to stream video from them all at once because the bus has a limited speed.

The Pi camera doesn't use the USB and you can nominally only connect one camera using the CSI connector on the Pis (except the compute-module which supports two).

However, I think there may be some unofficial third party multiplexing boards for the Pi camera. These won't allow you to run >1 Pi camera at once, but you can switch between several cameras in software, with a suitable delay (maybe >1 second) while a different camera wakes up.

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: Multiple cameras on a single raspberry pi

Wed May 18, 2016 10:17 am

I am streaming both the raspberry camera and also USB camera.
But indeed the ethernet is bottle neck and I have set both to 5 fps only.
I use motion to stream the USB cam and it uses the CPU heavily. Rapberry camera is mainly hardware thing and only uses the network.


User avatar
amin137398
Posts: 8
Joined: Sat May 14, 2016 7:46 am
Location: Iran

Re: Multiple cameras on a single raspberry pi

Fri May 20, 2016 2:53 pm

thanks
so the best way is to somehow switch between 2 cameras
-_-

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5805
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Multiple cameras on a single raspberry pi

Fri May 20, 2016 3:15 pm

amin137398 wrote:thanks
so the best way is to somehow switch between 2 cameras
One Pi Camera and one USB camera will work fine if that achieves your aims.

The USB camera is unlikely to be able to use any GPU processing/encoding as they spit out a format that can't easily be consumed (YUYV). It depends what you want to do with the data.

The switcher was mentioned as an alternative if you wanted two identical cameras, or use the Compute Module which has both camera interfaces of the SoC exposed.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
amin137398
Posts: 8
Joined: Sat May 14, 2016 7:46 am
Location: Iran

Re: Multiple cameras on a single raspberry pi

Sat May 21, 2016 8:32 am

6by9 wrote:
amin137398 wrote:thanks
so the best way is to somehow switch between 2 cameras
One Pi Camera and one USB camera will work fine if that achieves your aims.

The USB camera is unlikely to be able to use any GPU processing/encoding as they spit out a format that can't easily be consumed (YUYV). It depends what you want to do with the data.

The switcher was mentioned as an alternative if you wanted two identical cameras, or use the Compute Module which has both camera interfaces of the SoC exposed.
ohhh thanks that's helpful
I want to do image processing ( at least on 1 of the cameras ) , and then display both images from the cameras on 1 LCD simultaneously
something like this :
Image
-_-

PiGraham
Posts: 3404
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Multiple cameras on a single raspberry pi

Sat May 21, 2016 10:26 am

amin137398 wrote:I want to do image processing ( at least on 1 of the cameras ) , and then display both images from the cameras on 1 LCD simultaneously
something like this :
Image
Yes, you can do that. Two USB cameras, or a Pi cam + a USB cam. Here is some pygame code that displays two cameras PIP as your drawing:

Code: Select all

#Python 2.7.9

import sys
import pygame
import pygame.camera
import datetime as dt
import time

pygame.init()
pygame.camera.init()

change_channel = False
channel = 0
changeover_time = 5
Run = True

#create display 
screen = pygame.display.set_mode((1240,768),0)
pygame.font.init()
ovl_font = pygame.font.Font(None,30)
label1 = ovl_font.render('Camera 1', True,(255,255,255),(0,0,0))
label2 = ovl_font.render('Camera 2', True,(255,255,255),(0,0,0))

#find, open and start low-res camera
cam_list = pygame.camera.list_cameras()
webcam1 = pygame.camera.Camera(cam_list[0],(1024,768))
webcam2 = pygame.camera.Camera(cam_list[1],(320,240))
#webcam = pygame.camera.Camera(cam_list[channel],(1280,720))
#If no cameras found then nothing to do, quit
if(len(cam_list)==0):
    pygame.quit()
    sys.exit()
  
webcam1.start()
webcam2.start()
#webcam.start()

while Run:
     time.sleep(0.01)  
    #grab image, scale and blit to screen
    imagen1 = webcam1.get_image()
    imagen2 = webcam2.get_image()
    #imagen = webcam.get_image()
     #imagen = pygame.transform.scale(imagen,(1920,1080))
    screen.blit(imagen1,(0,0))
    screen.blit(imagen2,(0,0))
    screen.blit(label1,(imagen1.get_width()-label1.get_width()-10,imagen1.get_height() - label1.get_height() - 10))
    screen.blit(label2,(imagen2.get_width() - label2.get_width() - 10,imagen2.get_height() - label2.get_height() - 10))
    
    #draw all updates to display
    pygame.display.update()

    # check for events ESC key quits
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                Run = False
                webcam1.stop()
                webcam2.stop()
Rough code to show the principle. Requires error checking, handling variations between cameras etc etc. It works for me on Pi3B with a Philips SPC900NC and a Logitech B525 HD Webcam.
Also works with Pi Camera (V1) as one of the cameras. Make sure V4l2 driver is running:

Code: Select all

sudo modprobe bcm2835-v4l2
CHeck you have two video devices listed:

Code: Select all

ls /dev/vid*
You should get
/dev/video0 /dev/video1

Frame rate isn't full 30 fps (maybe 15 - 20 fps) . CPU load is ~60%.
I expect there are lots of optimisations that can be done. Not least avoid getting images sequentially from two unsynchronised cameras.
Last edited by PiGraham on Sat May 21, 2016 3:26 pm, edited 1 time in total.

User avatar
amin137398
Posts: 8
Joined: Sat May 14, 2016 7:46 am
Location: Iran

Re: Multiple cameras on a single raspberry pi

Sat May 21, 2016 12:40 pm

PiGraham wrote: Yes, you can do that. Two USB cameras, or a Pi cam + a USB cam. Here is some pygame code that displays two cameras PIP as your drawing:

Code: Select all

#Python 2.7.9

import sys
import pygame
import pygame.camera
import datetime as dt
import time

pygame.init()
pygame.camera.init()

change_channel = False
channel = 0
changeover_time = 5
Run = True

#create display 
screen = pygame.display.set_mode((1240,768),0)
pygame.font.init()
ovl_font = pygame.font.Font(None,30)
label1 = ovl_font.render('Camera 1', True,(255,255,255),(0,0,0))
label2 = ovl_font.render('Camera 2', True,(255,255,255),(0,0,0))

#find, open and start low-res camera
cam_list = pygame.camera.list_cameras()
webcam1 = pygame.camera.Camera(cam_list[0],(1024,768))
webcam2 = pygame.camera.Camera(cam_list[1],(320,240))
#webcam = pygame.camera.Camera(cam_list[channel],(1280,720))
#If no cameras found then nothing to do, quit
if(len(cam_list)==0):
    pygame.quit()
    sys.exit()
  
webcam1.start()
webcam2.start()
#webcam.start()

while Run:
     time.sleep(0.01)  
    #grab image, scale and blit to screen
    imagen1 = webcam1.get_image()
    imagen2 = webcam2.get_image()
    #imagen = webcam.get_image()
     #imagen = pygame.transform.scale(imagen,(1920,1080))
    screen.blit(imagen1,(0,0))
    screen.blit(imagen2,(0,0))
    screen.blit(label1,(imagen1.get_width()-label1.get_width()-10,imagen1.get_height() - label1.get_height() - 10))
    screen.blit(label2,(imagen2.get_width() - label2.get_width() - 10,imagen2.get_height() - label2.get_height() - 10))
    
    #draw all updates to display
    pygame.display.update()

    # check for events ESC key quits
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                Run = False
                webcam1.stop()
                webcam2.stop()
Rough code to show the principle. Requires error checking, handling variations between cameras etc etc. It works for me on Pi3B with a Philips SPC900NC and a Logitech B525 HD Webcam.
Frame rate isn't full 30 fps (maybe 15 - 20 fps) . CPU load is ~60%.
I expect there are lots of optimisations that can be done. Not least avoid getting images sequentially from two unsynchronised cameras.
thanks
I'm going to do it and keep you guys posted and share the result soon .
-_-

User avatar
amin137398
Posts: 8
Joined: Sat May 14, 2016 7:46 am
Location: Iran

Re: Multiple cameras on a single raspberry pi

Mon May 23, 2016 11:46 am

I can use 2 USB camera , can I?
-_-

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: Multiple cameras on a single raspberry pi

Mon May 23, 2016 12:22 pm

Yes, in theory. Depending on details you may have issues with CPU power, but yes, basically it is possible to use 2 USB cameras.

PiGraham
Posts: 3404
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Multiple cameras on a single raspberry pi

Mon May 23, 2016 2:00 pm

amin137398 wrote:I can use 2 USB camera , can I?
Didn't you actually read my post above where I confirmed using two USB cameras?

PiGraham
Posts: 3404
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Multiple cameras on a single raspberry pi

Mon May 23, 2016 4:49 pm

Tweaked code seems to run better.
Only reads an image from a camera when there is one available, so should avoid sequential blocking calls to get_image().
Uses double buffering and HW rendering
Added FPS info. Reports 10 to 15 fps. Seems more consistent.
CPU load still fairly high, 60 - 75%.

Code: Select all

import sys
import pygame
import pygame.camera
import datetime as dt
import time
import datetime

pygame.init()
pygame.camera.init()

change_channel = False
channel = 0
Run = True
newimg = False
current = datetime.datetime.now()
last = current
framerate = 0
iir_filter = 0.95

#create display 
#screen = pygame.display.set_mode((1024,768),pygame.DOUBLEBUF|pygame.HWSURFACE|pygame.FULLSCREEN)
screen = pygame.display.set_mode((1024,768),pygame.HWSURFACE|pygame.DOUBLEBUF)
pygame.font.init()
ovl_font = pygame.font.Font(None,30)
label1 = ovl_font.render('Camera 1', True,(255,255,255),(0,0,0))
label2 = ovl_font.render('Camera 2', True,(255,255,255),(0,0,0))
fps = ovl_font.render('FPS:', True,(255,255,255),(0,0,0))

#find, open and start low-res camera
cam_list = pygame.camera.list_cameras()
webcam1 = pygame.camera.Camera(cam_list[0],(1024,768))
webcam2 = pygame.camera.Camera(cam_list[1],(320,240))
#webcam = pygame.camera.Camera(cam_list[channel],(1280,720))
#If no cameras found then nothing to do, quit
if(len(cam_list)==0):
    pygame.quit()
    sys.exit()
  
webcam1.start()
webcam2.start()
#webcam.start()

while Run:

    time.sleep(0.01)       
    #grab image, scale and blit to screen
    if webcam1.query_image():
        imagen1 = webcam1.get_image()
        newimg = True
    if webcam2.query_image():
        imagen2 = webcam2.get_image()
        #newimg = True
    #imagen = webcam.get_image()
     #imagen = pygame.transform.scale(imagen,(1920,1080))
    if newimg:
        newimg = False
        current = datetime.datetime.now()
        framerate = iir_filter * (1/(current - last).total_seconds()) + (1.0 - iir_filter)  * framerate	# iir filter value
        last = current
        fps = ovl_font.render("FPS: %r" % round(framerate,1) , True,(255,255,255),(0,0,0))
        screen.blit(imagen1,(0,0))
        screen.blit(imagen2,(0,0))
        screen.blit(label1,(imagen1.get_width()-label1.get_width()-10,imagen1.get_height() - label1.get_height() - 10))
        screen.blit(label2,(imagen2.get_width() - label2.get_width() - 10,imagen2.get_height() - label2.get_height() - 10))
        screen.blit(fps,(imagen1.get_width() - fps.get_width() - 10,10))
    
        #draw all updates to display
        pygame.display.flip()

    # check for events ESC key quits
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                Run = False
                webcam1.stop()
                webcam2.stop()

User avatar
amin137398
Posts: 8
Joined: Sat May 14, 2016 7:46 am
Location: Iran

Re: Multiple cameras on a single raspberry pi

Wed May 25, 2016 8:34 am

PiGraham wrote:
amin137398 wrote:I can use 2 USB camera , can I?
Didn't you actually read my post above where I confirmed using two USB cameras?
oh sorry
I just didn't pay attention :)
-_-

Return to “General discussion”