0

I'm trying to learn to use gphoto2 to control a DSLR camera for image and video capture. I want the camera to take a photo every second, or minimum at a fixed rate but I can't work out how to do it.

I have found that in the command line one can use the command

gphoto2 - -I 2 -F 5 --capture-image and download

to capture an image every 2 seconds for 5 images for example but I don't know how to implement that in Python code and I don't know how many frames will be recorded. I would just like to capture the image, save to the camera SD card and 1 second later capture the next image.

Using

triggerCommand = ["--trigger-capture"]

appears to take images much quicker than

triggerCommand = ["--capture-image"]

but I cannot control the timings, both options also take longer than a second between frame captures.

My current code is below:

#!/usr/bin/env python3
import subprocess
import time
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess

#kill gphoto2 process that occurs whenever connect camera
def killgphoto2():
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out,err = p.communicate()
    for line in out.splitlines():
        if b'gvfsd-gphoto2' in line:
            #kill process
            pid = int(line.split(None, 1)[0])
            os.kill(pid, signal.SIGKILL)
   
triggerCommand = ["--trigger-capture"]

killgphoto2()
start_time = time.time()
for i in range(5):
    gp(triggerCommand)
end_time = time.time()
print(end_time - start_time)

If anyone could help, I'd really appreciate it.

Also can one use gphoto2 or something else with the raspberry pi to record the video using the camera SD card, so that the camera native resolution and frame rate can be used?

I currently use opencv on the pi which records the video screen but has difficulty maintaining a stable frame rate.

Thanks

Benjy
  • 19
  • 1
  • 7
  • You could try running the `gphoto2 - -I 2 -F 5 --capture-image and download` command with `subprocess` (or the `sh` package if you prefer). `trigger-capture` will be quicker than `capture-image` because it returns as soon as it's told the camera to take a picture, instead of waiting until the picture's taken and then copying it to the computer. – Jim Easterbrook Jan 06 '23 at 15:40
  • Right, but what is the syntax for that? I don't really understand how to convert command line code for gphoto2 into the subprocess module and I haven't found anything there that explains this properly. – Benjy Jan 07 '23 at 21:18
  • The `subprocess` module command is usually a list of strings, one for each part of the command line. I don't use it with `gphoto2` though, as I prefer to use `python-gphoto2` to give the Python process direct access to libgphoto2, rather than running `gphoto2` commands in another process. It's not easy to use though. – Jim Easterbrook Jan 09 '23 at 16:03
  • Right. For running a video, I tried `subprocess.run(['gphoto2', '--set-config', 'movie=1']) \n\n] subprocess.run(['gphoto2', '--set-config', 'record=1']) while (time.process_time() - start_time) < duration: pass print(time.process_time() - start_time) subprocess.run(['gphoto2', '--set-config', 'record=0'])`. – Benjy Jan 11 '23 at 14:15
  • This started the video but then the video cut out after 1 second and I couldn't figure out why. Any ideas? Using `subprocess.run(["gphoto2", "--capture-movie", "3s"])` would apparently record a 3s movie but then said 300+ frames were recorded and mjpeg output had 1 frame – Benjy Jan 11 '23 at 14:21

0 Answers0