Skip to content

V0.4 Configuring a DSLR With Deferred Download

FormerLurker edited this page Jun 8, 2020 · 9 revisions

DSLR - Deferred Download Video

DSLR - Deferred Download Guide Companion Video

Note: These instructions are in beta. Please notify me with any issues or corrections, or just to say things worked for you.

Before Using This Guide

This guide assumes you have read and performed all of the steps in the Configuring a DSLR tutorial. If you have not done so, complete the guide and return here when you are finished.

Deferring Image Download - OctoPi instructions

These instructions assume you are running OctoPi, but the instructions for other types of Linux installations would be very similar.

It is possible to prevent Octolapse from downloading the images from your camera to your server until after your print is finished. Since it takes quite a bit of time to transfer and save an ultra high resolution image, waiting until after the print has finished before downloading the images will will drastically reduce the time it takes to acquire a snapshot, which will improve print quality by reducing oozing. Also, using this method Octolapse can renderer your timelapse too, which cannot be done when using the No Download method.

Unfortunately, there are also disadvantages to this method. First, this is the least likely script to work for your particular camera since it requires four features: save to SD, delete all images from SD, trigger capture, and download all images from SD. Second, You won't see any snapshot previews within the Octolapse tab, the Image Transposition settings within your camera profile will not work, and ALL images will be deleted from your SD card when you start a print. However, if you want to reduce image capture time and improve quality, render automatically within Octolapse, and you don't mind not having a timelapse preview, this is the guide for you!

Warning: ALLl of the images on your DSLR will be completely erased before the start of each print so BE SURE TO BACK YOUR IMAGES UP BEFORE STARTING!!

Step 1 - Create Camera Initialization Script

First we have to add a new script, which we'll call our 'Camera Initialization Script'. This script will tell your camera to save all images to the SD card instead of to internal memory. IMPORTANT NOTE - Some cameras do NOT support configuration via GPhoto2. We will run some tests during this tutorial to see if things are working properly.

I put my script in /home/pi/scripts by navigating to the following directory:

cd /home/pi/scripts

Next, create a new script file with the nano editor like so:

nano initialize-camera-erase-all-images.sh

This will open the nano editor. Copy the following script and paste it into the nano editor by pressing the right mouse key.

Important Note: The very first line #!/bin/sh is important, and must be included.

#!/bin/sh
# Camera Initialization Script
# Sets the capture target to the SD card
# Written by: [email protected]

# Put the arguments sent by Octolapse into variables for easy use
CAMERA_NAME=$1
# Set camera to save images to flash memory
gphoto2 --auto-detect --set-config capturetarget=1
# DELETE ALL FILES ON THE CAMERA, SO BACKUP YOUR FAMILY PHOTOS FIRST!
gphoto2 --auto-detect --delete-all-files --recurse

Now we need to save the script. Press ctrl + o and then Enter to save. Then press ctrl + x to exit.

Now we need to add execute permission to the script with the following command

chmod +x initialize-camera-erase-all-images.sh

Note about the capturetarget setting

The config options vary by camera and by the gphoto2 version. You can run the following command to see what options for capturetarget are available on your camera:

gphoto2 --auto-detect --get-config capturetarget

Here's what my camera reports after running this command:

Label: Capture Target
Readonly: 0
Type: RADIO
Current: Memory card
Choice: 0 Internal RAM
Choice: 1 Memory card
END

If your options aren't the same as mine (Memory card = 1) you will have to adjust the initialize-camera-erase-all-images.sh script accordingly.

Now we will run a test to make sure that the script is working properly.

Warning: This will COMPLETELY erase all of the images on the SD card, so be absolutely sure you've backed up anything important else it will be lost forever.

Enter the following to test the initialize-camera-erase-all-images.sh:

./initialize-camera-erase-all-images.sh

Note: The ./ is important, so make sure you copy that part.

If you encounter an error here, you should search the internet for a solution. I am planning to compile common errors and include them in a troubleshooting guide, but this is not yet complete. If you encounter an error and find a solution, please let me know and I will add it to the guide.

Step 2 - Create trigger snapshot script

Next, we will need to create a new script to take the actual snapshot. Again, we need to create a new file with the nano editor like so:

nano trigger-snapshot.sh

Again, this will open the nano editor. Copy the following script and paste it into the nano editor by pressing the right mouse key.

Important Note: The very first line #!/bin/sh is important, and must be included.

#!/bin/sh
# Camera Capture Script - Leave on Camera, Don't download
# Requires a camera initialization script with the following command:  gphoto2 --auto-detect --capture-image --set-config capturetarget=1
# Written by: [email protected]
# Put the arguments sent by Octolapse into variables for easy use
SNAPSHOT_NUMBER=$1
DELAY_SECONDS=$2
DATA_DIRECTORY=$3
SNAPSHOT_DIRECTORY=$4
SNAPSHOT_FILENAME=$5
SNAPSHOT_FULL_PATH=$6

# trigger the camera and exit immediately
gphoto2 --auto-detect --trigger-capture

Now we need to save the script. Press ctrl + o and then Enter to save. Then press ctrl + x to exit.

We need to add execute permissions to this script as well like so:

chmod +x trigger-snapshot.sh

Now we will test this new script with the following command:

./trigger-snapshot.sh

Note: The ./ is important, so make sure you copy that part.

Your camera should take a snapshot, and it should be quite a bit faster than the normal download method you used in the Configuring a DSLR guide.

If you encounter an error here, you should search the internet for a solution. I am planning to compile common errors and include them in a troubleshooting guide, but this is not yet complete. If you encounter an error and find a solution, please let me know and I will add it to the guide.

Note: Not all cameras support the 'trigger-snapshot' command unfortunately. On my camera, the Nikon D5200, 'trigger-capture' returns after the snapshot is taken but before it is saved to SD. Some cameras return BEFORE the image is taken. In this case you may need to make some changes to trigger-snapshot.sh. Specifically you'll need to add a sleep 1 command right after gphoto2 --trigger-capture, which would look like this:

#!/bin/sh
# Camera Capture Script - Leave on Camera, Don't download
# Requires a camera initialization script with the following command:  gphoto2 --capture-image --set-config capturetarget=1
# Written by: [email protected]
# Put the arguments sent by Octolapse into variables for easy use
SNAPSHOT_NUMBER=$1
DELAY_SECONDS=$2
DATA_DIRECTORY=$3
SNAPSHOT_DIRECTORY=$4
SNAPSHOT_FILENAME=$5
SNAPSHOT_FULL_PATH=$6

# trigger the camera and exit immediately
gphoto2 --auto-detect --trigger-capture

# Wait for 1 second, you may need to adjust this value.  You want the lowest possible value
# that gives you a stable timelapse.
sleep 1  

That will cause the script to wait for 1 second before exiting, giving your camera time to take a snapshot. You will have to experiment to determine how long to sleep. You want to use the smallest possible value. For example, if you determine that one half of a second is adequate, you can change your script to sleep 0.5, or if one second isn't enough you could increase it to two seconds like this: sleep 2

Step 3 - Create Pre-Render Script

Now we need to create a script that will run after the print is complete, but before rendering takes place. This script will download all of the images from your camera into the proper directory on your server, and will rename them so that rendering will work properly. Again, we need to create a new file with the nano editor like so:

nano download-from-camera-and-rename.sh

Again, this will open the nano editor. Copy the following script and paste it into the nano editor by pressing the right mouse key.

IMPORTANT NOTE: The very first line #!/bin/sh is important, and must be included.

#!/bin/sh
# Camera Pre-Render script
# Written by: [email protected]

# Put the arguments sent by Octolapse into variables for easy use
CAMERA_NAME=$1
SNAPSHOT_DIRECTORY=$2
SNAPSHOT_FILENAME_TEMPLATE=$3
SNAPSHOT_FULL_PATH_TEMPLATE=$4

# Check to see if the snapshot directory exists
if [ ! -d "${SNAPSHOT_DIRECTORY}" ];
then
  echo "Creating directory: ${SNAPSHOT_DIRECTORY}"
  mkdir -p "${SNAPSHOT_DIRECTORY}"
fi

# switch to the snapshot directory
cd "${SNAPSHOT_DIRECTORY}"

# download all of the images on the camera
gphoto2 --auto-detect --get-all-files --force-overwrite

# Count the number of images downloaded.  If we find 0, report an error. 
a=0
for i in *.JPG *.jpg *.JPEG *.jpeg; do
  a=$((a+1))
done

# If no snapshots were found, report an error and exit.
if [ "$a" = "0" ]; then
    echo "No snapshot were found with extensions .JPG, .JPEG, .jpg or .jpeg. " 1>&2; 
    exit 1;
fi

# rename images according to the supplied file template
a=0
for i in *.JPG *.jpg *.JPEG *.jpeg; do
  num_files=0
  num_files=$(ls -lq "${i}" 2>/dev/null | wc -l  )
  if [ "$num_files" != "0" ]; then
    new=$(printf "${SNAPSHOT_FILENAME_TEMPLATE}" "${a}")
    mv -- "${i}" "${new}" 2>/dev/null
    a=$((a+1))
  fi
done

Now we need to save the script. Press ctrl + o and then Enter to save. Then press ctrl + x to exit.

We need to add execute permissions to this script as well like so:

chmod +x download-from-camera-and-rename.sh

Testing this script is a little tricky. I am working out how to do this now, but later in the guide, we can use a test button within the Octolapse camera profile to test the script.

Step 4 - Configure Octolapse

Finally, we will need to configure Octolapse to use these two new scripts. First, we will need to create a new camera profile:

First, we'll need to create a new profile.

Open up OctoPrint in a browser window and click on the Octolapse tab. Make sure the Current Settings are expanded. If they are not visible, click on the Current Run Configuration link.

Then open up the camera profiles page within the Octolapse settings by clicking on the 'Cameras' link:

Open the camera profiles page

Next click on the Add Profile... button.

Add a new camera profile

Name your profile so you can easily identify the camera. I'm going to use Nikon D5200 DSLR in this example:

Name and Describe Your Profile

Now, change the Camera Type dropdown box to External Camera - Script:

Select External Camera - Script

Next enter the following for 'Snapshot Acquire Script'

/home/pi/scripts/trigger-snapshot.sh

Here is what your Snapshot Acquire Script should look like:

External Camera - Script Selected

Now, click the Test button next to the Snapshot Acquire Script:

Click the Test Button

If everything worked, you will see this popup indicating success:

The Snapshot Acquire Script Worked!

You will notice that the toast message is orange, which indicates a warning. The message will say that no snapshot was found in the target folder. This could indicate an error, but in this case it is exactly what we want!

If you get a failure message (will be in red) or a warning message (will be orange), there was a problem. I will try to add some troubleshooting steps, but for now I recommend rechecking your script and testing GPhoto2.

Next, set the 'Snapshot Delay' to 0 since the script won't exit until the photo is taken and downloaded. It's a good idea to turn the snapshot timeout up to 10000 (10 seconds) while you are testing to make sure your camera has enough time to take and download the photo, especially at higher resolutions.

Recommended Snapshot Delay and Snapshot Timeout settings for DSLR Cameras

Now move to the Custom Camera Scripts section and set the Before Print Start Script to the following:

/home/pi/scripts/initialize-camera-erase-all-images.sh

Here is what you should have when you are finished:

Before Print Start Script - Deferred Download

Now click the Test botton next to the Before Print Start Script:

Click the Test Button

If things went well, you should see this success message:

Custom Camera Script Success Message

If you get a failure message (will be in red), there was a problem. I will try to add some troubleshooting steps, but for now I recommend rechecking your script and testing GPhoto2.

Set the 'Before Render Script' setting to the following path:

/home/pi/scripts/download-from-camera-and-rename.sh

It should look like this when you are finished:

Before Render Script Settings for Deferred Download

Now click the Test botton next to the Before Render Script:

Click the Test Button

If things went well, you should see this success message:

Custom Camera Script Success Message

If you get a failure message (will be in red), there was a problem. If your camera has lots of images on it, this script may have timed out. Wait for a few minutes and try it again. Alternatively, you could manually delete all of the images on your camera except one, then retry the test button. When you run a test or live print, this script will be executed with no timeout.

I will try to add some additional troubleshooting steps later, but for now I recommend rechecking your script and testing GPhoto2.

Make sure all of the other custom script fields are empty (Before Snapshot Script, After Snapshot Script, After Render Script, After Print End Script).

Finally save your profile.

Save Camera Settings

Finally, edit your current rendering profile settings and make sure the 'Timelapse Generation' option is checked. If you have multiple processors on your server (for example, the Pi3 B+ has 4 cores), you may want to adjust the 'Rendering Thread Count' under 'Advanced Rendering Settings'. This setting can greatly reduce the amount of time required to render a video. This is really nice because rendering a high res video can take a very long time on a pi! Be careful, however, since setting the available thread count higher than the number of available cores will HURT performance. If you are using a Pi3 B+ it is pretty safe to set the rendering thread count to 3, but NOT more than 4.

Step 5 - Test Print

That should be it as far as configuration goes! Now run a short test print. Navigate to the Octolapse tab, expand the Current Run Configuration section, and enable the Test Mode slider at the bottom:

Test Mode Enabled

Test mode will prevent your printer from heating up (bed and extruder) and will strip off all extrusion commands, so you won't waste any time or plastic. I strongly recommend you unload your filament, though, just in case there is a bug or glitch in the test mode code, and make sure your bed/hotend stay cool.

Select a test file to print. I recommend something small and fast, so you don't have to wait long to make sure things are working correctly. Click Print once you have selected your file.

Octolapse should now run as usual. Since Test Mode is enabled, your bed and hotend should stay cool, no fans should turn on, and no plastic should be extruding. Octolapse will now acquire images from your DSLR, and any other cameras you have enabled. You should hear your camera snapping away during the snapshot phase. It should take substantially less time to acquire images now that the images aren't being downloaded.

When the test print is completed, Octolapse will run the Before Render Script, which should download all of your images. If you have errors here, it could be that your camera is using an unknown naming pattern. I'll update the script when I find these, so check back for fixes and updates.

Finally, Octolapse should render a video.

Congratulations, your DSLR is configured and ready to go!

Troubleshooting

Coming Soon

Windows Installation

The instructions for this are coming soon!

Clone this wiki locally