Skip to content

Configuring an External Camera

FormerLurker edited this page Oct 5, 2018 · 35 revisions

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

Octolapse now supports external cameras by calling an external script. The script is responsible for acquiring an image. Optionally the script can put the resulting image in the proper place so that Octolapse can render the resulting timelapse.

Linux (octopi) installation instructions

These instructions assume that you are running OctoPi v0.15.1. The steps will be similar for other flavors of Linux, though some modifications may be necessary.

Watch a video version of this guild here.

Step 1 - Installing the Latest Octolapse and OctoPrint

First, install the newest version of OctoPrint (1.3.9 currently). This is very important, so don't skip it!

In order to access the new feature you need to install the latest pre-release version of Octolapse: v0.3.4rc1.dev3. You can read more about this pre-release here. Be sure to check out the previous releases since there have been several since the last stable release (currently v0.3.1). You can install the latest pre-release via the plugin manager by using the following URL:

https://github.com/FormerLurker/Octolapse/archive/v0.3.4rc1.dev3.zip

Simply go into the plugin manager, click 'Get More' and enter the above url into the box labeled '... from URL'. Restart when prompted. After the restart, make sure you clear your browser cache by pressing ctrl + F5.

Next open your current octolapse printer profile settings. Under 'Current Slicer Settings' select your slicer from the 'slicer type' drop down box. Now copy all of the settings listed from your slicer into your Octolapse profile. If you see 'non-unique speed warnings' and would like to use the new 'Layer Change - High Quality' snapshot trigger, see this link for instructions on how to clear the warnings.

Step 2 - Gphoto2 Installation

Similar steps would be needed for a non/raspberry pi Linux setup. This example install uses GPhoto2 to acquire the images from your DSLR. I had some trouble with installation on my pi, so let me know if you have problems installing.

Here is a list of compatible cameras.

Use SSH or some other method to open a shell and execute the following command:

sudo apt-get install gphoto2

Note: If you have problems with the above install command you might need to install libgphoto2. Please let me know if you need to install it by running this command and then retrying the gphoto2 install:

sudo apt-get install libgphoto2-dev

Step 3 - Configure /etc/sudoers file

The next step is to add gphoto2 to the list of programs that can run without sudo. Otherwise you'll need to use sudo and enter your password to acquire an image. Type the following command to edit the sudoers file:

sudo visudo

That will open an editor that should look something like this if you are using octopi:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

add the following lines to the VERY END of the file:

# allow 'sudo gphoto2' to run without supplying a password
pi ALL = (root) NOPASSWD: /usr/bin/gphoto2

Remember that you can copy and paste into nano by moving the cursor to the position you want to insert the rule (the very bottom in this case) and right clicking once to paste.

Now press ctrl+O (o as in output) to save your changes, and then press ctrl+z to exit.


Important Note: If you've installed gphoto2 a different way than described above, for example with the gphoto2 updater script, gphoto2 may not be in the /usr/bin/ directory. Use the following command to find out where gphoto2 was installed:

whereis gphoto2

For example if whereis returns the following

gphoto2: /usr/local/bin/gphoto2

the last line of your sudoers file should look like this:

pi ALL = (root) NOPASSWD: /usr/local/bin/gphoto2

Next, we need to reboot the pi so that the sudoers file change takes effect. You can do that by entering the following command:

sudo reboot

You'll now need to wait for your pi to reboot, then re-connect via ssh.

Step 4 - Test your camera

Now plug in your DSLR via usb, power it up, and execute the following command to make sure GPhoto2 is detecting your camera:

gphoto2 --auto-detect

If things are working properly You should get something similar to the following:

Model                          Port
----------------------------------------------------------
Nikon DSC D5200                usb:001,016

If there are no cameras detected you will get the following output:

Model                          Port
----------------------------------------------------------

In this case you should move to the official gphoto2 site and try to find a solution. Make sure you check the list of compatible cameras.

If things are working you should be able to run the following command to acquire an image:

gphoto2 --capture-image

Your camera should take a picture and save it on the camera. You should also be able to download an image to the current directory with the following command:

gphoto2 --capture-image-and-download

You should now see a new image in the current folder by entering the ls command. If you have problems here they will need to be corrected before continuing.

Step 5 - Create a snapshot script

Octolapse will soon ship with some default scripts, but for now they need to be created manually. As soon as these scripts are available I will update these instructions.

Below you will find a script that will trigger a snapshot, download the image from the camera's internal RAM to the pi, and move it into the proper directory with the proper name. This method preserves all of the functionality of Octolapse that you normally enjoy when using a webcam, including image transformations, post processing, snapshot preview, rendering, etc.

Now we need to create our snapshot script. I put my script in /home/pi/scripts like so:

cd /home/pi/scripts

nano take-snapshot.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
# 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

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

# IMPORTANT - You must add gphoto2 to your /etc/sudoers file in order to execute gphoto2 without sudo
# otherwise the following line will fail.
sudo gphoto2 --capture-image-and-download --filename "${SNAPSHOT_FULL_PATH}"

if [ ! -f "${SNAPSHOT_FULL_PATH}" ];
then
  echo "The snapshot was not found in the expected directory: '${SNAPSHOT_FULL_PATH}'." >&2 
  exit 1
fi

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 take-snapshot.sh

Finally, we can test our script by entering in the following command (it's long, so make sure you copy everything):

/home/pi/scripts/take-snapshot.sh 1 1 "" "/home/pi/scripts" "" "/home/pi/scripts/test.jpg"

you should see the following output:

New file is in location /capt0000.jpg on the camera
Saving file as /home/pi/scripts/test.jpg
Deleting file /capt0000.jpg on the camera

If you enter ls you should see test.jpg in the current directory. You can delete the test image by entering rm test.jpg.

Step 6 - Configure Octolapse

We're almost done! Open OctoPrint and open your Octolapse camera profile. Change the drop down box at the top of the page from 'Webcam' to 'External Camera - Script'

Next enter the following for 'Snapshot Acquire Script'

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

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. Finally save your profile.

Step 7 - Test Print

That should be it as far as configuration goes! Now run a short test print. Change your 'debug profile' within Octolapse to Test Print - Full Diagnostic. This option will cause a diagnostic lot to be recorded. Additionally, 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.

Octolapse should now run as usual, but should acquire an image from your DSLR instead of the usual webcam. You should hear your camera snapping away during the snapshot phase. Also, there should be some snapshots going into a new folder created within the following directory:

/home/pi/.octoprint/data/octolapse/snapshots/SOME_GUID_HERE/THE_CAMERA_GUID_HERE

If you are having trouble at this point, you might consider opening an issue here. Be sure to submit plugin_octolapse.log and your settings.json file, as is requested in the standard issue template. Please note any deviations from the above instructions.

Step 8 - Finishing up

Now that you've completed your test print, check out the resulting timelapse (it might take a while to render). You're probably noticing that the quality isn't quite as high as you'd expect. Never fear, this can be corrected!

First, set the debug profile to 'No Logging'. There is no reason to log if you aren't having problems. In fact, the logger can write tons of data which might be interfering with your print, so only use it when necessary.

Second, open up the 'Rendering' profile. Consider turning up the bitrate to 15M or 20M. The resulting video will be much larger. I do NOT recommend that you use the H264 codec for DSLR timelapses, because it often results in a malloc error for some reason. If anyone has any information about why this could happen (happens even on my workstation), please let me know!

Third, if you have a multicore computer (Pi3), you may want to turn up the rendering threads. Rendering an ultra high res timelapse can take a very long time, especially on a pi running only a single thread. Ideally you would use one thread per core. Be sure NOT to attempt a new print while Octolapse is rendering unless you are sure there is plenty of processing power. If you MUST print while you are rendering, make sure to leave 1 or 2 open cores when you set the rendering threads (do this at your own risk!).

Notes about running gphoto2 with sudo

If for some reason you absolutely cannot add gphoto2 to the /etc/sudoers file, you can replace this line of the take-snapshot.sh script:

sudo gphoto2 --capture-image-and-download --filename "${SNAPSHOT_FULL_PATH}"

with this one:

echo "PUT_YOUR_PASSWORD_HERE" | sudo -S gphoto2 --capture-image-and-download --filename "${SNAPSHOT_FULL_PATH}"

Enter your password where it says PUT_YOUR_PASSWORD_HERE, but make sure you leave both quotation marks!

This procedure is absolutely not recommended, ESPECIALLY if your pi (or pc) is accessible via the internet! I may even remove these instructions at some point.

Windows Installation

The instructions for this are coming soon!

Clone this wiki locally