Skip to content

V0.4 Enabling Camera Controls

FormerLurker edited this page May 28, 2020 · 17 revisions

OctoPi uses mjpg-streamer to create a camera stream, which allows you to monitor your printer via OctoPrint. Mjpg-streamer has the ability to change some camera settings via control.htm. Additionally, mjpg-streamer can create a description of your camera's controls and capabilities, accessible via a file called input.json. Since mjpg-streamer has no authentication or authorization capability, it was decided to disable all access to these pages within OctoPi by default.

If you have not used port-forwarding or some other insecure methods to view your OctoPi instance through the internet, these pages are generally safe to use. If these pages are available, Octolapse can take advantage by using them to create a screen that allows you to control your camera's settings.

Prerequisites

  • Mjpg-streamer must be installed.
  • Some plugins that create their own camera stream must be disabled. Currently this includes OctoPrint Anywhere and The Spaghetti Detective. These are both great plugins, but they are incompatible with custom camera controls.

Notes for Raspberry Pi Camera Users

If you are using a Raspberry Pi Camera, the steps are a bit different. See this comprehensive guide for using a Raspberry Pi Camera.

Step 1 - Connecting to Your Raspberry Pi

If you don't know how to connect to your Raspberry Pi, see this guide. When you are connected, continue on to step 2 below.

Step 2 - Edit your octopi.txt file

Open your octopi.txt file with the nano editor by entering the following command followed by the Enter key:

sudo nano /boot/octopi.txt

Open octopi.txt in nano

You may be prompted to enter your password. After the command completes, you will see the nano text editor. Use the arrow keys and edit this section (it may look slightly different than what is pictured here, but it should be close):

Next, using the arrow keys move down to the bottom of the octopi.txt file and find the following section:

Octopi.txt without access to control.htm

change the following text

#camera_http_webroot="./www-octopi"
#camera_http_options="-n"

to this:

camera_http_webroot="./www"
camera_http_options=""

Be sure to remove any # characters at the beginning of the lines! It should look like this when you are finished:

octopi.txt with control.htm access enabled

While you're here, you might want to adjust your camera's resolution and frame rate. Find the camera_usb_options section and edit those now. For example, if you want a 1080P image at 30 frames per second, you would use the following:

camera_usb_options="-r 1920x1080 -f 30"

Be sure to refer to your camera's documentation when choosing a resolution and frame rate. Not all resolutions and frame rates are available for all cameras.

Make sure to remove any # characters from the beginning of the line!

and the end result will look something like this:

1080P at 30 FPS

**Note: The higher the resolution and frame rate, the more bandwidth and CPU power will be used. Higher frame rates will reduce the image acquisition time (possibly improving quality, especially when using a smart trigger with snap-to-print enabled). Also, some versions of Firefox will have trouble showing a high frame rate mjpegstream, but Chrome seems to do just fine. Your mileage may vary, but if you have problems, try lowering your frame rate from 30 frames per second to 10 frames per second.

Now we want to save the file and exit nano. Hold down the ctrl key and press the letter O (so ctrl+O) to save the file. Press enter to confirm. Then press ctrl+X to exit.

Finally, reboot your Pi by entering the following command followed by the Enter key:

sudo reboot

Reboot

You may need to enter your password again if prompted. If you are using Putty, your session will end with an error. Wait until your Raspberry Pi has rebooted completely and then verify that OctoPrint is ready by opening it in a new browser window.

Step 3 - OPTIONAL - Add a New Camera Profile

If you don't want to change the settings of an existing camera profile (perhaps because you have multiple cameras), the steps below will walk you through adding a new camera profile. If you only have one camera, you can skip this step and move on to Step 4

Open up OctoPrint in a browser window and click on the Octolapse tab.

The Octolapse tab with current run configuration expanded

Make sure the Current Settings are expanded. If they are not visible, click on the Current Settings 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 ACME Webcam in this example. Once you are finished lick on the Save button.

Name and save your new camera profile

Now exit the OctoPrint settings popup by clicking Cancel at the bottom right hand corner.

Close Octolapse settings popup

Step 4 - Editing and Configuring your USB Camera Profile

Click the edit icon next to your your camera profile.

Edit your raspberry pi camera profile

This will open up the existing camera profile settings. If you want to rename your profile, feel free to do so now.

Next, make sure that you are not using an imported profile by selecting `Not Selected' from the dropdown box.

Make sure "no profile to import" is selected

Make sure that the Camera Type dropdown box is set to Webcam. Your settings will look something like this:

Camera type and address

Now we need to verify the Snapshot Address Template. Make sure your camera can take snapshots by clicking on the 'Test' button. If it is successful, it will look like this:

Camera test success!

If the test failed, you will see this message:

Camera test failed :(

If your test has failed, see this troubleshooting guide for help getting the Snapshot Address Template working.

Step 5 - Enabling Custom Image Preferences

Find the Custom Image Preferences section within the camera profile and check the Enabled check box. If you see an error like this, follow the troubleshooting guide here:

Enable custom image preferences failed

If custom image preferences were successfully enabled, you will see two new options: Apply Preferences at Startup and Apply Preferences Before Print Start. Enable them both. Your settings should now look like this:

Custom Image Preferences are enabled and will be applied after boot and before a print starts, even if the camera profile is disabled

Now, save your profile by clicking the Save button:

Save camera custom image preferences

Next, select your timelapse preview camera in the dropdown on the Octolapse tab found here:

Select the raspberry pi camera as your current timelapse preview camera

You will now see a message explaining that no snapshots are available for the webcam. This is normal, and you won't see any snapshots here until you run a print with Octolapse enabled.

Next, click the edit pen to the right of the camera you just selected to open the image preferences:

Edit the custom image preferences for your webcam

If everything is working properly, you will be able to see your camera stream at the top of the popup, which will look similar to this:

An example of a working camera stream

If the stream is not working, you will see something like this:

This camera stream failed to load

If your camera stream will not load, see this troubleshooting guide.

Finally, save your profile by clicking on the Save button at the very bottom.

Step 6 - Editing Your Camera's Image Preferences

Dialing in your camera settings is very important for getting a high quality timelapse. Before you start, it is important that you have adequate lighting. Good lighting is critical for getting a high quality timelapse, and it might even improve your print quality by reducing the snapshot acquisition time! Photography LEDs work great, but cheap LEDs that use low frequency pulse width modulation (PWM) might result in a lot of flicker in your final timelapse. Incandescent and halogen bulbs will work well, but they use a lot more power than LED lights. In general, you will want your lighting to be much brighter than is necessary to just see your prints.

I also recommend that you close any blinds in the room with the printer because the light levels in the room may vary, messing up the exposure and leading to a lower quality timelapse.

Once you have improved your lighting, it's time to adjust the camera settings.

Open the Octolapse tab, and select your camera from the dropdown box. Then click the edit pen in the upper right hand corner of the Latest Snapshot image:

Edit image preferences from the Octolapse tab

If you do not see the edit pencil, custom image preferences have not been properly enabled. Go back to step 7

Here you will find a couple options regarding how the image preferences will display. Feel free to toggle between Single Column and Two Column Mode. You can also switch between the default and custom image preference screen. Please note that this guide assumes you are using two column mode _and _the custom page, so your settings should look like this:

Custom, two-column mode is enabled

Note that the button above will say Switch to Single Column when it is in two column mode.

The first thing I usually adjust is the focus. If your camera has digital focus, you will see the control within your preferences. Be sure to deselect automatic focus if you have the option. Here is what this looks like for my webcam (a Logitech C920):

Disable Auto Focus and Adjust Manual Focus

Next, I recommend adjusting your exposure and gain. This will vary by camera, but usually you will need to set your exposure to manual and then edit the exposure and gain value. Automatic exposure will usually lead to flickering, as lighting conditions in the room change. Here is how this looks with my Logitech C920:

Exposure and Gain Settings Example

You want to use as low an exposure setting as possible while maintaining good image quality. If your camera does support gain controls, it's usually better to have a higher gain and lower exposure, since higher exposure times can sometimes increase the time it takes for Octolapse to acquire an image. Experiment a while to determine what works best for your camera and lighting conditions. I also recommend closing your blinds to keep the light levels in your room stable.

Another important setting to adjust is the white balance. Manual settings here can also cause a flickering timelapse. Some cameras have individual adjustments for red and blue, and some only have color temperature. Here is what my white balance settings look like:

Exposure and Gain Settings Example

I try to adjust my white balance until the color looks as true to reality as I can.

Next you can play around with the rest of the settings. If you run into trouble, you can click the Defaults button at the bottom to restore all of the default camera settings. When you are finished adjusting your image, click 'Save' at the bottom to store the settings and close the popup.

Congratulations! Your USB Webcam is all configured!

Other Useful Things to Know

If you completed step 5, your camera settings will all be automatically applied when your Raspberry Pi reboots, as well as when a new print starts. You can change this by editing your camera profile.

You can adjust your camera settings at any time, but be sure to save them when you're finished. Also, be careful when adjusting your camera settings in the middle of a timelapse. Changing the settings during a print may be quite noticeable in the timelapse video. It is much better to adjust your camera before starting a print.

Clicking Cancel while editing custom image preferences will undo any settings you have applied by re-applying the old settings.

Cancel Your Changes

You can always press the Apply All Settings Now button to ensure that every setting shown is applied to the camera image. It is possible for the camera to become out of sync with the Octolapse settings, and this button will correct the problem.

Apply All Settings Now

The other interesting button you might want to use is the Stabilize Extruder button, which will move your extruder to the snapshot position. This works as long as you are not using the 'Snap To Print' smart trigger option, and as long as stabilization is not disabled. Make sure your printer bed is clear before using this feature, else you may run into your print!.

Stabilize Extruder Button

Note that the 'Home Axis Gcode Script' within the printer profile will be executed before your extruder moves into position. You can edit this script within your current Printer Profile. It is important that your printer is homed and in absolute mode before Octolapse can stabilize safely, so make sure that your printer is in absolute xyz mode by using a G90 command. Also make sure it has been moved to the home position (usually accomplished via a G28 command).

Lastly, it may be useful to know that Octolapse dynamically creates the controls on the Custom Image Preferences screen based on the functionality reported by mjpg-streamer. It reads the available options and values from a file called input.json that can be found by navigating to a page whose URL has the following form: {camera base url}/input.json. Octolapse also contains a few custom camera pages that have improved over the default dynamic page. You can see if your camera was detected by looking under the camera stream:

Camera Type Detected

To check for a custom page or to switch to the default dynamic page, click this toggle button:

Check for a Custom Camera Page

If a page is available, you will see the following popup message:

Webcam Type Found

If a page is not available, consider creating an issue, and be sure to download and provide the input.json file mentioned above, and include your camera model. I can't guarantee I will be able to test it (odds are I don't have the same camera you do), but I will add it to a list for consideration.

Clone this wiki locally