How to Control and Capture Images from DSLR using Raspberry Pi
In this short post, I will walk you through the steps for controlling and capturing pictures from a DSLR connected to the Raspberry Pi using a USB cable. There are numerous tutorials out there for this setup but I found them to be outdated to some extent. This post covers the installation of libgphoto2 and ghoto2 from the source and also covers the steps for capturing pictures using Python scripts.
Prerequisite
For this tutorial, I am assuming that you have a Raspberry Pi with Raspbian or Noob OS installed on it. You can get a Raspberry Pi from Amazon if you don’t already have one.
Also, I am assuming that you already have one of the supported cameras listed here:
Projects :: libgphoto2 :: supported cameras
I used the Canon Rebel T7 camera which I got from Amazon.
Install libgphoto2 and gphoto2
libghoto2 library lets you interface with 100s of supported DSLR cameras and ghoto2 is a command-line utility for using libghoto2
Here are the steps for installing both these libraries.
Prerequisites
Install the following dependencies:
sudo apt-get install git make autoconf libltdl-dev libusb-dev libexif-dev libpopt-dev libxml2-dev libjpeg-dev libgd-dev gettext autopointInstall libghoto2
Download the latest code for libgphoto2 from:
git clone https://github.com/gphoto/libgphoto2.gitExecute the following commands to install libgphoto2
cd ~/libgphoto2
autoreconf --install --symlink
./configure
make
sudo make installInstall ghoto2
Download code for gphoto2
git clone https://github.com/gphoto/gphoto2.gitBuild and install gphoto2
cd ~/gphoto2
autoreconf --install --symlink
./configure
make
sudo make installAdd the following line in /etc/ld.so.conf.d/libc.conf
/usr/local/libRefresh cache before proceeding further.
sudo ldconfigGenerate udev rules for the camera
/usr/local/lib/libgphoto2/print-camera-list udev-rules version 201 group plugdev mode 0660 | sudo tee /etc/udev/rules.d/90-libgphoto2.rulesGenerate the hardware database file for udev
/usr/local/lib/libgphoto2/print-camera-list hwdb | sudo tee /etc/udev/hwdb.d/20-gphoto.hwdbUsing ghoto2
Now that we have both the libraries installed, now we can use ghoto2 for interfacing with the camera. Make sure that the camera is connected to the Raspberry Pi.
Execute the following command to see if ghoto2 can detect the camera.
gphoto2 --auto-detectIf your camera name shows up in the output, you can now go ahead and click a picture by executing the following command:
gphoto2 --capture-imageUsing Python Scripts
Now that we have tested that the camera connection is working fine, we will go ahead and capture pictures using simple python scripts. Firstly, install the python wrapper for ghoto2
sudo pip install -v gphoto2Next, create a script that will let you capture an image from the DSLR.
We first get an instance of the camera object using get_camera and then pass this instance to the capture_image function to click a picture.
That’s it for this post. Please leave a clap if you found this article useful. Consider subscribing to Medium to read more of my stories.
