I am working on the following code:

import os
os.system("sudo fswebcam -d /dev/video0 -r 1280x960 image.jpg")
os.system("sudo fswebcam -d /dev/video1 -r 1280x960 image2.jpg")

in above code, the processing like capturing image from cam1 then process that image and after that switch to second cam an capture image and save that image. this process take total time of 5 sec. I want to capture images from both camera at same time without any delay os less time delay like 1-2 seconds. is it possible? how can we solve this problem?

P.S. I am not using opencv and don't want to use opencv right now

import subprocess
p1 = subprocess.Popen("sudo fswebcam -d /dev/video0 -r 1280x960 image.jpg", shell=True);
p1 = subprocess.Popen("sudo fswebcam -d /dev/video0 -r 1280x960 image2.jpg", shell=True);

The images will still not be acquired at exactly the same time, but you'll have somewhat better parallelism. There are lots of obvious, and some non-obvious, improvements of the code above, but it will get you started.

Threading won't buy you anything, as what you are doing is running a separate program in a separate process anyway. If fswebcam can output to stdout rather than a file, use pipes to read the output from python directly, rather than writing a file to the filesystem and reading from the filesystem. The documentation for subprocess gives lots of examples.

  • Thanks @JayEye ur solution is workin time reduce to 1 second of image capturing and both cameras taking photos at the same time Thank you Sir. – Nishub May 30 '16 at 7:09
  • another satisfied customer :) – JayEye May 30 '16 at 19:45

Instead of

import os
os.system("sudo fswebcam -d /dev/video0 -r 1280x960 image.jpg")
os.system("sudo fswebcam -d /dev/video1 -r 1280x960 image2.jpg")


import os
from time import sleep
os.system("sudo fswebcam -d /dev/video0 -r 1280x960 image.jpg")
#where n is sleep time in seconds
os.system("sudo fswebcam -d /dev/video1 -r 1280x960 image2.jpg")

Theoretically this should speed it up to whatever time you set as n.

For a better option you should have it be multi-threaded so that you can have them happen at the same time with no delay if you'd like to try that i recommend you here.

  • thanks for the help @sir_ian maybe i should try multithreading. – Nishub May 28 '16 at 12:14
  • can i use ffmpeg for this? – Nishub May 28 '16 at 12:22
  • sleeping will only delay things, it will not cause them to run in parallel – JayEye May 29 '16 at 17:40
  • @JayEye i know that it wont make them run in parallel but it will force a sleep speed of n which should be better than the 5 seconds that he reported – sir_ian May 29 '16 at 21:22
  • 1
    no, it won't; os.system() will not return until the command has finished executing. All the sleep() call does is add delay. – JayEye May 29 '16 at 21:46

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.