Pages

Thursday, August 2, 2012

Control your camera with gPhoto2 via MacOSX Terminal (Nikon, Canon, Dslr / compact, etc)


Sometimes we need more control than what is offered by our digital camera (setup a timelapse shoot), and sometimes we want information about the camera which is just not available except by bringing it into a service center (total number of shots / actuations).

There is a great GPL/FREE tool which allows you to do both, and more...

**Already have gPhoto2 installed & working?  - Try this Applescript based GUI for TimeLapse shooting.

gPhoto2 OVERVIEW

gPhoto2 was developed to allow unix based systems to communicate with digital cameras.  Over 1400 camera models are supported, and as Mac OSX has a unix foundation, we can reap the benefits provided by gPhoto2.

Besides the basic file management tools allowing you to download files from the camera, format memory cards, some of the great things we can do are:
- rename files as they are being downloaded
- download only the thumbnails
- upload files into the camera
- retrieve the total number of shots (actuations) made by the camera in its lifetime
- tethered shooting: set the camera to capture images, with instant download to the computer (keeping or deleting from the camera)
- interval shooting: set the camera to shoot a specified number of exposures at specified intervals

The only thing which might scare off casual users is the gPhoto2 is a command-line based tool.  While there is no GUI for Mac OSX available, it is possible to create a few scripts for the functions you require call them either by applescript, or even icons from within the finder.  I'll go through this, as well as include a completed AppleScrip app that you can run simply by double clicking.


gPhoto2 INSTALLATION

Installations is not overly complex, but if more involved than the typical mac application install.

gPhoto2 runs on MacPorts, which requires Apple's Xcode Developer Tools.

So lets take this one step at a time.

1. Xcode - If you have Xcode installed, skip step 1.
Otherwise, make sure you get the latest version available for your OS version (v4.4 for Mountain Lion, v4.1 for Lion, v3.2 for Snow Leopard, and v3.1 for Leopard) available at Apple's Developer Site or through the AppStore.

The trick is that you still need the Xcode command line utilities - which are not installed by default.
This can be found @ Apple Developer Connection or through Xcode directly as instructed here - depending on the version you have.

2. MacPorts - download the latest version at the MacPorts Projects Official Homepage. Look at the "Mac OS X Package (.pkg) Installer" section to find the proper link for your OS X version.
After download and installation, we make our first venture into the command line.
Make sure you run the update with the command: sudo port -v selfupdate in Terminal.

3. gPhoto2 - As of March 2011, gPhoto2 is part of the MacPorts distribution, so you should be good to go from here.

If you've downloaded a distribution without gPhoto2, it can be installed from the same command line:
sudo port install gphoto2

This will take abut 10-15 mins depending on network connection, as each component is downloaded and applied.

You can then type: gphoto2 from the command line to confirm that all works.


gPhoto2 USAGE

When connecting your camera to your mac, the system "hijacks" your camera - this allows applications such as Image Capture, Aperture, iPhoto or others to interface directly with it.  Before running gphoto2 you need to terminate this "hijack" by running the following command:

killall PTPCamera

This will need to be done every time the camera is plugged in or turned on while connected to your mac.

** Make sure that your camera is connected and turned ON - check that it has not gone into sleep mode by half-pressing the shutter button.

And now to the fun stuff:

1. List what cameras you currently have connected - indicating the camera model and the port it is connected to.

gphoto2 --list-ports


2. List the files (images) on the memory card in the camera - will list all files, and provide a number to each file which can be used with the get command.

gphoto2 --list-files


3. Get files from the camera and save to current directory.

gphoto2 --get-file 24-30 (will download files numbered 24 through 30)
gphoto2 --get-all-files  (will download all image files from camera)


4. Delete files (images) on the memory card in the camera

gphoto2 --delete-files 24-30 (will delete files numbered 24 through 30) 
gphoto2 --delete-all-files   (will delete all files on the camera) 


5. Show camera status summary, camera model (including supported capabilities through gphoto) and memory card info

gphoto2 --summary

Sample output: 
Camera summary:
Manufacturer: Canon Inc.
Model: Canon EOS 5D Mark II
  Version: 3-2.1.1
  Serial Number: 72e4c1b6fa2f4410b5b81f55f37acdbc
Vendor Extension ID: 0xb (2.0)

Storage Devices Summary:
store_00010001:
StorageDescription: CF
VolumeLabel: None
Storage Type: Removable RAM (memory card)
Filesystemtype: Digital Camera Layout (DCIM)
Access Capability: Read-Write
Maximum Capability: 8009842688 (7638 MB)
Free Space (Bytes): 7977271296 (7607 MB)
Free Space (Images): -1



6. Show camera SHUTTER ACTUATION COUNT - this is one of my favorites.  Indispensible in determining how much use the camer actually has on it.  A great way to judge camera shutter resource usage if you are thinking of buying a second hand DSLR (or selling yours).

gphoto2 --get-config /main/status/shuttercounter

Sample output:
Label: Shutter Counter
Type: TEXT
Current: 12654


7. Take some pictures.

gphoto2 --capture-image (will take a shot with the camera)
gphoto2 --capture-image-and-download (will take a shot with the camera and download the image to your current directory)


8. Shooting TimeLapse pictures using the intervelometer function

gphoto2 --capture-image-and-download --interval 20 --frames 500

This will shoot and download (to current folder on computer) each image.  The "interval 20" will shoot every # of seconds specified (20 above).  The "--frames 500" will tell the command to shoot 500 frames.

At 20 seconds between frames, and 500 frames, would result in approx 2 hour and 45 min shoot, resulting in a 20 second video at 25fps.

The great thing here is that the files are downloaded to you computer shot by shot as acquired.  What you can do is with Terminal in the background running the process, have a finder window open to the same folder, reviewing the new shots as they come in.  You can take this further and do a quick preview with QuickTime playing back the images as a sequence to view the movie as its being shot.

Below is my first attempt to run a sequence using gphoto.


High Quality version of the video here.




gPhoto2 MAKING THINGS SIMPLER

Remembering the command parameters and referring to guide pages can get tiresome.

What I've done is create a "gphoto" folder and a few scripts to help me speed through some of the tools I need to use.

Using either a terminal text editor such as pico or nano (both present on Mac OSX systems), or any other favorite you may have, or just plain old simple TextEdit, create the batch files with the commands you need.

My favorites for this are:
gphoto_get_shutter_count   and    gphoto_capture_timelapse

Using nano in Terminal from within my gphoto folder:
nano gphoto_get_shutter_count

This will open up the editor; go ahead and paste the following:
killall PTPCamera
gphoto2 --get-config /main/status/shuttercounter

press CONTROL-X, which will command the editor to close and ask if you want to save changes.
press Y (for yes) and ENTER (to confirm)

This script will automatically run both, the PTPCamera reset, and the command.

ls -la   to see a listing of the files (scripts) in this directory

Sample output:
total 8
drwxr-xr-x   3 d  staff  102 Aug  3 00:36 .
drwxr-xr-x  10 d  staff  340 Aug  3 00:34 ..
-rw-r--r--   1 d  staff   68 Aug  3 00:36 gphoto_get_shutter_count

One last step before we can run our script.  By default we have saved a text document.  Which can be opened and edited - but it cannot be eXecuted !

We can enable execution of the file for the user (owner) with:
chmod u+x gphoto_get_shutter_count

Checking again with ls -la we see that the X flag has been added for our batch file.

total 8
drwxr-xr-x   3 d  staff  102 Aug  3 00:36 .
drwxr-xr-x  10 d  staff  340 Aug  3 00:34 ..
-rwxr--r--   1 d  staff   68 Aug  3 00:36 gphoto_get_shutter_count


Finally we RUN IT and reap the results!  *Make sure the camera is connected and turned on.

./gphoto_get_shutter_count

The "." and "/" are necessary before the filename in Terminal to launch files. *This is an oversimplification, but should be more than enough to get one started.

You can go ahead and create such scripts for all your favorite commands.


gPhoto2 MAKING THINGS EVEN MORE SIMPLE & "Mac Like"

Part 1 - Lets handle the "MAC Like" task first - add icons to the scripts and run them from the Finder.

To begin, lets get some nice ICONS and make this pretty:

The Camera Obscura icon page also has instructions on changing the icons of your files! (They can be found right below the download are in a section called "How to use these icons".


Part 2 - adding an interface to the timelapse script.


This one involves AppleScript - resulting in a very simple app which does the following:

  1. asks for number of SHOTS to be taken
  2. asks for the INTERVAL between shots
  3. asks for a PROJECT NAME
  4. creates a folder for the project
  5. creates the script to run within this folder
  6. runs the timelapse sequence, saving shots off camera directly to your project folder




If you are tired of the codes and command lines, feel free to download the app and just run it with the plain ol' "double click".



On the other hand.. if you are ready to dive into the script that makes this happen... here it is:

You can COPY/PASTE the following into the AppleScript Editor and change as you wish.


####
### RUNNING A TIMELAPSE VIA GPHOTO2
####
#
#
##########
# INTERFACE #
##########
#
# INFORMATION DISPLAY
display dialog "This will create a GPHOTO folder on your desktop with the PROJECT NAME you provide and use it for storing the timelapse photos"
-- get INTERVAL and SHOTS details
set thePrefix1 to ""
set theNumber1 to ""
set theIcon1 to noterepeatdisplay dialog thePrefix1 & "How many SECONDS (interval) would you like the camera wait before taking each shot (enter a number) ?" default answer theNumber1 with icon theIcon1set theNumber1 to text returned of resulttryif theNumber1 = "" then errorset theNumber1 to theNumber1 as numberexit repeaton errorset thePrefix1 to "INVALID ENTRY! "
set theIcon1 to stopend tryend repeat#
set thePrefix2 to ""
set theNumber2 to ""
set theIcon2 to noterepeatdisplay dialog thePrefix2 & "How many SHOTS should the camera take (enter a number) ?" default answer theNumber2 with icon theIcon2set theNumber2 to text returned of resulttryif theNumber2 = "" then errorset theNumber2 to theNumber2 as numberexit repeaton errorset thePrefix2 to "INVALID ENTRY! "
set theIcon2 to stopend tryend repeat#
#
set WaitingInterval to theNumber1
set RepeatQuantity to theNumber2
#
#
-- confirm selection with user
display dialog "You have requested a timelapse of " & RepeatQuantity & " shots, with an interval of " & WaitingInterval & " seconds between each shot.  Press OK to proceed or CANCEL to abort"
display dialog "* Please make sure that your camera is connected to the computer (via USB) and turned ON before continuing!  Double check by half-pressing the shutter button on the camera!"
#
#
#
# FOLDER CREATION with SUBFOLDERS
-- Get project name
set thePrefix1 to ""
set theNumber1 to "Timelapse project shoot name"
set theIcon1 to noterepeatdisplay dialog thePrefix1 & "Enter the project name:" default answer theNumber1 with icon theIcon1set theNumber1 to text returned of resulttryif theNumber1 = "" then errorset theNumber1 to theNumber1 as stringexit repeaton errorset thePrefix1 to "INVALID ENTRY! "
set theIcon1 to stopend tryend repeatset folderNameProject to theNumber1
set projectName to quoted form of theNumber1
-- check for existence / create master folder
set folderName to ("gphoto")
set folderLocation to path to desktopset message to folderLocation as string#display dialog message
tell application "Finder"
trymake new folder with properties {name:folderName, location:folderLocation}end tryend tell
-- goto/create new folder
set gphotoLocation to (((path to desktop folder as text) & "gphoto:") as alias)
tell application "Finder"
trymake new folder at gphotoLocation with properties {name:"" & folderNameProject & ""}end tryend tell
-- open current project folder
tell application "Finder"
activateset projectLocation to (((path to desktop folder as text) & "gphoto:" & folderNameProject & ":") as alias)
open projectLocation
end tell
-- prepare the script
set TheScript1 to "killall PTPCamera"
set TheScript2 to "gphoto2 --capture-image-and-download --interval " & WaitingInterval & " --frames " & RepeatQuantityset ScriptName to "gphoto-timelapse-" & projectName & ".script"
set ThePath to quoted form of the POSIX path of projectLocation
do shell script "cd " & ThePath & "&& pwd"
do shell script "touch " & ThePath & "/" & ScriptNamedo shell script "echo " & TheScript1 & " >" & ThePath & "/" & ScriptNamedo shell script "echo " & TheScript2 & " >>" & ThePath & "/" & ScriptNamedo shell script "chmod u+x " & ThePath & "/" & ScriptName
-- run the script
do shell script "cd " & ThePath & "/ && pwd && ./" & ScriptName


ENJOY!


REFERENCE LINKS
Apple Xcode - instructions for removing.
MacPorts - http://www.macports.org
gPhoto2 - http://www.gphoto.org
gPhoto2 - Command Reference (Same as running: man gphoto2)

8 comments:

  1. Excellent post, helped me out a lot! I was finding some delay in the time it took gphoto to get my 7D to take a picture and wrote a little writeup of getting that down a bit, hope it's useful to you: http://datkanarie.tumblr.com/post/43006784183/first-findings-on-using-gphoto2-with-a-canon-7d

    ReplyDelete
  2. NICE BLOG!!! Thanks for your valuable information,

    ReplyDelete
  3. thank you.. Immensely helpful

    ReplyDelete
  4. Wonderful info. Thanks so much!

    ReplyDelete
  5. Probably worth noting that libgphoto2 and gphoto2 cli are available in brew now as well.

    brew install libgphoto2
    brew install gphoto2

    ReplyDelete
  6. Holy moley this line just saved me so much time:

    killall PTPCamera

    Thank you!

    ReplyDelete
  7. Thank you for all of your comments!

    ReplyDelete
  8. Thanks for one’s Good posting! I enjoyed reading it, I want to encourage you to definitely continue your great writing, have a nice evening! keeping this website
    Easy Cover For Camera

    ReplyDelete