I’m a bit of a novice, needing to do this for a university project. I need to connect two USB webcams to the RPi and get each to take a still image and save it in “%iimg%i” naming notation to identify the image and the camera it come from. The images are saved onto a USB drive. This all has to run from a script that runs automatically on startup.

Any suggestions as to how to address the cameras as separate entities? And they don’t need to be full colour nor do I need to stream video so any way I can cut down on the data size too, aside from resolution?

EDIT: I'm working on a simple fswebcam function following the advice from Bierschi:

# Install fswebcam on RPi:  sudo apt-get install fswebcam
# fswebcam allows for basic photography from a specified device

str DIRECTORY = ""                      #path to write to (undefined)

int takePhoto (int cam, int num):
    if cam == 0:
        str command = "fswebcam -d /dev/video0 -r 640x480 --no-banner --png " + DIRECTORY + "/1img" + num + ".png"
        os.system (command)
        str commamd = "fswebcam -d /dev/video1 -r 640x480 --no-banner --png --flip h " + DIRECTORY + "/2img" + num + ".png"
        os.system (command)
return 0

Would this activate the cameras correctly? I believe my implementation of the strings is correct.

  • It's great to hear you solved your problem, but we don't encourage adding (SOLVED) to the title here at Stack Exchange. Instead, you can click the checkmark below to mark the answer as accepted, which will indicate in the UI that you solved your problem. This will also give @bierschi some rep as a 'thank you'.
    – Aurora0001
    Mar 3 '18 at 15:14
  • 1
    @Aurora0001 Thanks for the information, I'll keep that in mind and accept the answer now :) Mar 3 '18 at 15:15

You connect both cameras on the usb ports and if the drivers are configured properly you will see it with following command:

ls /dev/video*


/dev/video0 /dev/video1

Now you have access to both devices with /dev/video0 and /dev/video1.

Try to take a photo with a software like fswebcam:

sudo apt-get install fswebcam

and type in:

fswebcam -d /dev/video0

if this works with both cameras, at this point you can start to write your python script with any camera software like picamera https://picamera.readthedocs.io/en/release-1.13/

or with opencv https://pypi.python.org/pypi/opencv-python

example function in python:

import os

def takePhoto(cam, num):
  directory = "/home/pi/Desktop/"
  if cam == 0:
     command = "fswebcam -d /dev/video0 -r 640x480 --no-banner " + directory + "1img" + str(num) + ".png"
   command = "fswebcam -d /dev/video1 -r 640x480 --no-banner --flip h " + directory + "2img" + str(num) + ".png"

if __name__ == '__main__':
  takePhoto(0, 1)

Be careful, c and python are not the same, so look at the python function definitions. I made a simple example, but please try to use the subprocess module in python, because os.system() is deprecated.

  • Thanks Bierschi, so basically after manually listing the attached devices I should just be able to specify which of them to read via fswebcam? As I’m just capturing still images do I even need openCV? In my previous digging I remember seeing that you can take a photo straight from fswebcam. Is that right? Jan 29 '18 at 16:09
  • To give some more context at this early prototyping stage I just need to get the cameras to take the photos and save to the USB drive when called. There are a few other bits but it basically comes down to: move-photo1-photo2-move and repeat. Jan 29 '18 at 16:13
  • yeah you´re right. If you just want to take photo from both cameras, it is sufficient to work with fswebcam. This 2 suggestions (picamera, opencv) are useful libs to write your own camera python scripts with a lot of opportunities for your own settings. In your case you can work with 'fswebcam', try the command fswebcam -h you get an extensively output with differents options you can select (output to store, resolution, different devices...)
    – bierschi
    Jan 29 '18 at 16:28
  • 1
    Thanks again! I’ve used openCV before in a C++ Project (so much easier to use than PCL). The one other feature I’ll need is to flip the image from one camera, I’ll have a look through fswebcam for that, if not then I’ll find another solution. I’ll let you know how it goes :) Jan 29 '18 at 16:35
  • hi again. Got my Pi, got one of the two cameras, second is delayed because seller assumed I only wanted one and got my Pi set up thru VNC and can take snapshots via fswebcam (I’ve learnt so much more about the Pi and Python this past week than ever before!). I’m gettin goth impression that fswebcam is working as a “bang” command when implemented in the command line? So I wouldn’t be able to use it natively in Python scripts, right? Feb 2 '18 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.