4

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

sir_ian
  • 980
  • 4
  • 17
  • 38
Nishub
  • 43
  • 1
  • 5

2 Answers2

6
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);
p1.wait()
p2.wait()

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.

JayEye
  • 1,908
  • 12
  • 19
-2

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

Try

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

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.

sir_ian
  • 980
  • 4
  • 17
  • 38