[Octoprint] Configure an USB webcam

Connecting an USB webcam to Octopi (Octoprint on Raspberry Pi 3) is really straightforward, but you can suddenly notice that the video stream needs to be tuned.

Creality Ender 3 Pro with webcam and Raspberry Pi3 running Octoprint

The default configuration of the webcam provides automatic white balancing and automatic exposure settings that can be good in a generic highly dynamic environment, but it’s not good in a static system like a 3D printer.

In this guide I will explain how to tune the camera settings and how to store all the parameters such as they are loaded as default configuration.

Install dependencies and set the environment

To be able to control the USB camera settings we must install a few dependencies.

Open a console terminal (Ctrl+Shift+t) and start a ssh connection with the Raspberry Pi 3:
`$ ssh pi@octopi.local`

Install dependencies:
$ sudo apt-get install uvcdynctrl guvcview

Enable parameter settings storing:
$ sudo chmod a+rw /dev/media0


Connect your USB camera to one of the USB port of the Raspberry Pi3 and get the list of all the parameters that can be tuned for the camera:
$ uvcdynctrl -c
for example for the ELP-USBFHD01M-L28 camera:

Listing available controls for device video0:
   White Balance Temperature, Auto
   Power Line Frequency
   White Balance Temperature
   Backlight Compensation
   Exposure, Auto
   Exposure (Absolute)
   Exposure, Auto Priority

For each parameter you can get the current value:
$ uvcdynctrl -g 'param_name'
and set a new value:
$ uvcdynctrl -s 'param_name' param_value
when you set a new value you will see the effects directly in the Octoprint web interface selecting the Control tab.

You can play with each parameter to see its effect on the image.
Don’t forget to turn off “Auto Exposure” and “Auto White Balance”:
$ uvcdynctrl -s 'Exposure, Auto' 1
$ uvcdynctrl -s 'White Balance Temperature, Auto' 0

When you have tested all the parameters and you have found the best configuration for your webcam create a bash script to automatically set everything using a single command:
$ sudo nano ~/scripts/cam_init.sh

Each line of the script will set the value for each available parameter.
For example this is the content of the script for the ELP-USBFHD01M-L28 webcam:

uvcdynctrl -s 'White Balance Temperature, Auto' 0
uvcdynctrl -s 'White Balance Temperature' 3000
uvcdynctrl -s 'Exposure, Auto' 1
uvcdynctrl -s 'Exposure (Absolute)' 20
uvcdynctrl -s 'Exposure, Auto Priority' 1
uvcdynctrl -s 'Brightness' 20
uvcdynctrl -s 'Contrast' 40
uvcdynctrl -s 'Sharpness' 10
uvcdynctrl -s 'Backlight Compensation' 0
uvcdynctrl -s 'Hue' 5
uvcdynctrl -s 'Saturation' 100
uvcdynctrl -s 'Gamma' 100
uvcdynctrl -s 'Gain' 0
uvcdynctrl -s 'Power Line Frequency' 1

exit 0

Save the script with Ctrl+x and flag it as executable:
$ chmod a+x ~/scripts/cam_init.sh

At this point the script must be executed every time Octoprint starts.
Edit the config file (please do it ONLY WHEN NO PRINTING JOBS ARE RUNNING):
$ nano ~/.octoprint/config.yaml
add the following block at the end of the file (heading spaces are important):

  - event: ClientOpened
  command: /home/pi/scripts/cam_init.sh
  type: system 

Save the script with Ctrl+x and restart the Raspberry Pi3 to verify that everything is working as expected:
$ sudo reboot

Now you can start a new ssh connection to Raspberry Pi 3 to verify that the settings have been stored using the command
$ uvcdynctrl -g 'param_name'
or even better you can verify it visually in the Control tab of the Octoprint web interface.

This is the result applied to my 3D printer:

Have a good 3D printing day 🙂

[Original source: https://community.octoprint.org/t/changed-the-video-feed-brightness-contrast-and-settings-for-my-logitech-usb-video/1103]

Comments are closed.