Changed the video feed brightness, contrast and settings for my Logitech USB video

Changed the video feed brightness, contrast and settings for my Logitech USB video feed to Octoprint control in my browser.

Step-by-step

With the kind help of Fredrik Agert, I got this working perectly on my Pi and would like to share for others that may be looking for this solution.

Please feel free to add, update or comment if I have missed anything ( I have no Linux experience )

I am using Putty on Win 8 to open an SSH Terminal window to the Pi.

Current setup is :
Octoprint 1.3.6
OctoPi 0.14.0

Connect to the Pi using the SSH Terminal window ( Putty ).

First step was to install uvcdynctrl in the Pi with the following terminal command :
sudo apt-get install uvcdynctrl

Next, try a manual change to the video settings with :
uvcdynctrl -s 'Brightness' 55 'Contrast' 15 'Saturation' 80 'Sharpness' 'Focus, Auto'

The changes should show immediately in the Octoprint video feed.

Adjust the values to get the picture quality that you want.
My settings ( due to a very bright background ) are :
uvcdynctrl -s 'Brightness' 150 'Contrast' 35 'Saturation' 50 'Sharpness' 'Focus, Auto'

Next, create the script file that will run and change the settings :

create the pi directory if it does not exist
mkdir -p /home/pi

create the bin directory if it does not exist
mkdir -p /home/pi/bin

start the Pi text editor ( nano ) using :
nano /home/pi/bin/cam_init.sh

In that script file, paste or type the settings line ( with your choice of values ) :
uvcdynctrl -s 'Brightness' 55 'Contrast' 15 'Saturation' 80 'Sharpness' 'Focus, Auto'

ctrl-X to close and Y to Save the file, and Enter to commit the Save.

Next, you need to give that file permission to run as an executable file with :
chmod a+x /home/pi/bin/cam_init.sh

Now you need to tell the Server when to run that script file.
I could not get this to work using the 'Startup' event, but it works perfectly using the 'ClientOpened' event, which occurs every time you connect to the Server from your PCs web browser.

Edit the config file ( WARNING : I was advised to NOT do this when a print job is running ) :
nano /home/pi/.octoprint/config.yaml

and add to the bottom of the file :

events:
  subscriptions:
  - event: ClientOpened
    command: /home/pi/bin/cam_init.sh
    type: system

( aparently the spaces at the start of each line are important )

This tells the Server for every time the ' ClientOpened ' event occurs ( you connected to the Server ), to run the script file cam_init.sh

Restart the Pi and once your server is running, and you connect from your browser, the video feed settings will be changed.

If you want to change the settings, go back to :
sudo nano /home/pi/bin/cam_init.sh
and edit the settings.
Save the file, then close the browser tab for Octoprint and open a new tab - the ClientOpened event is triggered and the new settings will be in place.

6 Likes

@DaveOB :+1: I took the liberty to move this to "Guides", fix your formatting and remove two unnecessary uses of sudo.

Hi Gina

Thank You, and Thank You also for having made OctoPrint

Regards

Dave

Great post, @DaveOB. Do you know what the syntax is for setting a manual focus in this?

I tried : uvcdynctrl -d /dev/video0 -c
but that only shows :
Listing available controls for device /dev/video0:
Brightness
Contrast
Saturation
White Balance Temperature, Auto
Gain
Power Line Frequency
White Balance Temperature
Sharpness
Backlight Compensation
Exposure, Auto
Exposure (Absolute)
Exposure, Auto Priority
LED1 Mode
LED1 Frequency

Are you using a fixed focus camera (like a C270)? That would not show anything about turning off auto-focus, since it has no auto-focus.

I have a Logitech C270 HD720p.
It is manual focus as I had to follow a youtube guide to open and adjust the focus.

Folks, the command uvcdynctrl -s 'Brightness' 55 'Contrast' 15 'Saturation' 80 'Sharpness' 'Focus, Auto' -s only take one parameter at a time and ignores the rest,so the above only changes one thing, Brightness. Will be back later to update this post with my final resolution ..

So I had executed the above and made changes to different parameters with no change.. so I did a Man on uvcdynctrl and ran a -W with output file and found the only thing changing was the Brightness. So I tried one at a time and then I was able to configure all the parameters. I did not try a single line with multiple -s options.
Once complete you use the -W to create a config file and the -L to load it on startup .

Here is the content of -W with my current config

The CHK(0-255 is the range of valid arguments for Brightness VAL(120) is the current setting;
if you run the uvcdynctrl -W outfile after each adjustment you will see VAL change.

i@octopi:~/etc$ cat uvcdynctrl.conf
#V4L2/CTRL/0.0.2
APP{"libwebcam"}
# control data
#Brightness
ID{0x00980900};CHK{0:255:1:-8193}=VAL{120}
#Contrast
ID{0x00980901};CHK{0:255:1:57343}=VAL{30}
#Saturation
ID{0x00980902};CHK{0:255:1:57343}=VAL{40}
#White Balance Temperature, Auto
ID{0x0098090c};CHK{0:1:1:1}=VAL{0}
#Gain
ID{0x00980913};CHK{0:255:1:57343}=VAL{0}
#Power Line Frequency
ID{0x00980918};CHK{0:2:1:2}=VAL{2}
#White Balance Temperature
ID{0x0098091a};CHK{0:10000:10:61432}=VAL{3700}
#Sharpness
ID{0x0098091b};CHK{0:255:1:57343}=VAL{80}
#Backlight Compensation
ID{0x0098091c};CHK{0:1:1:57343}=VAL{0}
#Exposure, Auto
ID{0x009a0901};CHK{0:1:1:0}=VAL{1}
#Exposure (Absolute)
ID{0x009a0902};CHK{1:10000:1:166}=VAL{375}
#Exposure, Auto Priority
ID{0x009a0903};CHK{0:1:1:0}=VAL{0}

I created an etc and bin dir under pi home and placed the above content in ~/etc/uvcdynctrl.conf

I then edited a script called load_uvcdynctrl_config in ~/bin adding the following content

#!/bin/bash

#################################################################################
# Script  : load_uvcdynctrl_config
# Purpose : Load config to set webcam image parameters
#
#################################################################################

/usr/bin/uvcdynctrl -L /home/pi/etc/uvcdynctrl.conf

This script needs to be made executable and the added to the event as posted above so it loads the config when you start the browser.

Here is a timelaps done after the adjustments D&D miniature Orc Claw
Hope that helps the next person.
Thanks everyone !!
Doug

I stumbled on this article while trying to make adjustments on my Logitech C615. Airscapes is correct, as originally listed the commands only adjust one setting at a time. I played around with it a bit, trying multiple -s passes on the same line, and comma separating the values, but ended up just passing a bunch of separate commands as part of the suggested cam_init.sh.

For example, my cam_init.sh looks like this:

uvcdynctrl -s 'Brightness' 115
uvcdynctrl -s 'Contrast' 30
uvcdynctrl -s 'Saturation' 30
uvcdynctrl -s 'Sharpness' 50
uvcdynctrl -s 'Focus, Auto' 0
uvcdynctrl -s 'Focus (absolute)' 1
uvcdynctrl -s 'White Balance Temperature, Auto' 1

I'm pretty sure the focus options aren't working quite as expected with my C615, though. Using uvcdynctrl -g 'Focus (absolute)' to check the values it doesn't reflect what I have set in the shell script until I set it to something way higher first, then run the script. If I set the script to turn absolute focus up to 200 the value shows as 204, then set it back to 2 and the value shows as 0.

edit: I thought comma separated values worked but upon further testing I was mistaken. Above is what I landed on.

1 Like

Just didn't like the idea of the conf file for all the settings?

I used v4l2-ctl and udev as described here:

2 Likes

Hi,
I've tried this out on my Logitech C270 and it works really well. I am using the settings uvcdynctrl -s 'Brightness' 150 'Contrast' 35 'Saturation' 50' (the sharpness and focus ones seem to have zero effect) and like @JP_Powers has done, I have put each setting on a separate line.

However, since implementing this, the webcam has been super unreliable and I want to see if disabling all of this without deleting it entirely (essentially just stop it running the process at startup) will help. Basically I keep getting greeted with "Webcam stream loading". Occasionally it works but not for more than a few minutes. I'm also wondering if something else is potentially causing it because my Discord remote plugin has been acting funny lately but I want to rule this out.

I'm not quite sure how I would go about stopping the uvcdynctrl settings from running (new to all this) so could someone please help?

Thanks,
Jamie

Continuing the discussion from Changed the video feed brightness, contrast and settings for my Logitech USB video:

I'm trying to set my web USB camera. But no matter how I change values it doesn't change anything in the picture. After each command I get:

[libwebcam] Unknown V4L2 user control ID encountered: 0x0098090E (V4L2_CID_USER_BASE + 14)
[libwebcam] Unknown V4L2 user control ID encountered: 0x0098090F (V4L2_CID_USER_BASE + 15)
[libwebcam] Unknown V4L2 camera class (UVC) control ID encountered: 0x009A090F (V4L2_CID_CAMERA_CLASS_BASE + 15)
[libwebcam] Unknown V4L2 camera class (UVC) control ID encountered: 0x009A0920 (V4L2_CID_CAMERA_CLASS_BASE + 32)
[libwebcam] Unknown V4L2 camera class (UVC) control ID encountered: 0x009A0921 (V4L2_CID_CAMERA_CLASS_BASE + 33)