33

Is there a way to have my webcam sense lighting conditions and adjust screen brightness accordingly?

11

I've improved on my previous attempt (python):

import opencv
import opencv.highgui
import time
import commands


def get_image():
    image = opencv.highgui.cvQueryFrame(camera)
    return opencv.adaptors.Ipl2PIL(image)

camera = opencv.highgui.cvCreateCameraCapture(-1)

while 1:
    image = get_image()
    image.thumbnail((32, 24, ))
    image = tuple(ord(i) for i in image.tostring())
    x = int((int((max(image) / 256.0) * 10) + 1) ** 0.5 / 3 * 10)
    cmd = ("sudo su -c 'echo " + str(x) +
        " > /sys/devices/virtual/backlight/acpi_video0/brightness'")
    status, output = commands.getstatusoutput(cmd)
    assert status is 0

Two problems with this approach: The display brightness, at least with my webcam, never goes lower than four, because the camera doesn't work well in darkness, and it might skip a bit between four and five. Anyway,

This is how to use it:

  1. sudo apt-get install python-opencv
  2. Save this script somewhere, say ~/test.py
  3. Run it via python test.py

Instead of the average brightness, I'm now getting the maximum brightness of a small tumbnail (this is to avoid dead pixel problems and make it faster). And at least with my combination of lights and camera, it works rather well!

Give it a try (:

6
  • 1
    Note that this is a very simplistic approach. It'd be very easy to get a weighted black and white histogram of the image instead of the arithmetic mean of the pixels (red and blue don't have the same brightness). Also, you could set a minimum, maximum and have it switch only after a certain level of confidence that the ambient light has changed. But I didn't bother because of the issues I mentioned. It would have the same problems as this one. Jan 28 '11 at 1:05
  • 1
    Hmm, well it works like you say. :)
    – zpletan
    Jan 28 '11 at 14:45
  • @zpletan give it another go; I've started from scratch. Feb 2 '11 at 16:23
  • 1
    This seems to work about the way I want. Thank you! (And sorry for taking so long to answer.)
    – zpletan
    Jun 1 '11 at 1:39
  • 1
    There is no highgui from stackoverflow.com/questions/30684661/… . Is there any other way? Jun 6 '15 at 16:21
2

Take a look into the RedShift project, which changes the screen temperature based on your geographic location as F.Lux was formerly doing.

http://jonls.dk/redshift/

Instructions on installing and how to use are well documented in the website and, depending on your hardware and monitor you can feel your eyes like "refreshed" by the nice effect which is achieved by RedShift.

5
  • 2
    I don't think this answers the question, maybe you should delete your answer and make it a comment on the original question instead. Answers should always directly answer the question. Jan 28 '11 at 1:57
  • your link does not work.
    – TheXed
    Jan 28 '11 at 2:01
  • 1
    It sounds like this does not take your surroundings into account? So if I was in a dark room in the middle of the day, it wouldn't do anything?
    – zpletan
    Jan 28 '11 at 3:03
  • @zpletan Yes, this one only changes the colour, according to the position of the sun. Jan 28 '11 at 15:51
  • This side the link seems to work without problems. Stefano Palazzo, sorry if you didn't find my answer useful. However, I think somebody else could find it is useful if you keep the answer. Your choice. You can vote to request deletion. :) Thank you. Jan 29 '11 at 19:32
2

To add the PPA, press Ctrl+Alt+T on keyboard to open the terminal. When it opens, run the command below:

sudo add-apt-repository ppa:fantasyleague0629/wildguppy

Then install WildGuppy via:

sudo apt-get update; sudo apt-get install wildguppy

Works on Ubuntu 14.04, Ubuntu 13.10, Ubuntu 13.04, Ubuntu 12.10 and Ubuntu 12.04.

Make WildGuppy running at startup:

Run command to edit the config file.

sudo gedit ~/.config/autostart/wildguppy-gtk.desktop

Type in your password when prompted. Note that terminal will not display your password when typing, just type in mind and hit enter.

When the file opens, paste below content and save it.

[Desktop Entry]
Type=Application
Exec=wildguppy-gtk
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=WildGuppy
Name=WildGuppy
Comment[en_US]=
Comment=
3
  • Hey, thanks for the answer! This is exactly what I was looking for back in the day, but unfortunately I don't use Ubuntu any more and have no way to test (though I don't know why it wouldn't work). As such, I don't feel comfortable removing the answer status on the other answer, but hope that others will find this useful and it will be swiftly voted to the top if so. Cheers!
    – zpletan
    Jun 6 '15 at 23:18
  • @zpletan Welcom, When you install this package, you can run python script located in /opt/wildguppy/wildguppy.py. So you can use this package in any distro! Cheers! Jun 7 '15 at 4:25
  • @zpletan I also make some change to calculate average brightness to prevent fast and sudden changes in brightness.github.com/Daniyal-Javani/autobrightness/blob/master/… Jun 7 '15 at 5:43
0

I also made a C tool for that, using OpenCV. You can find it at https://launchpad.net/brightness (you have to compile it by your own). I hope it will help you.

1
  • I've got it compiled, but how do I use it?
    – zpletan
    Jun 1 '11 at 1:19
0

You may use https://github.com/Wandersalamander/Dimmer which does exactly what you asked for.

0

This works for me:

#!/usr/bin/env python3

import subprocess, time


# webcam brightness if webcam doesn’t get any light
blackpoint = 0.05

# webcam brightness if webcam is fully exposed (e.g. sun at noon)
whitepoint = 0.92549

# Path to program that sets screen brightness.  Takes float between 0 and 1 as
# a parameter.  Should be whitelisted for sudo if this script is not run as
# root.  Sample script:
#
# #!/bin/sh
# echo "($1 * 4882) / 1" | bc > /sys/class/backlight/intel_backlight/brightness
brightness_setter = "/home/bronger/bin/set_brightness.sh"

# it doen’t get any darker
minimal_brightness = 0.1

# in seconds
sleeping_time = 20


def get_brightness():
    """Returns webcam brightness as a float between 0 and 1 (boundaries
    included)."""
    fswebcam = subprocess.Popen(["fswebcam", "-q", "--no-banner", "--png", "0", "-"], stdout=subprocess.PIPE)
    convert = subprocess.run(["convert", "png:-", "-colorspace", "gray", "-scale", "10%x10%",
                              "-format", "%[fx:image.maxima]", "info:"],
                             check=True, stdin=fswebcam.stdout, capture_output=True, text=True)
    assert fswebcam.wait() == 0
    brightness = float(convert.stdout)
    brightness = (brightness - blackpoint) / (whitepoint - blackpoint)
    brightness = max(0.0, min(1.0, brightness))
    return brightness


old_brightness = None
while True:
    brightness = get_brightness() ** 2
    brightness = max(minimal_brightness, brightness)
    if old_brightness is None or abs(brightness - old_brightness) > 0.2:
        subprocess.run(["sudo", brightness_setter, str(brightness)], check=True)
        old_brightness = brightness
    time.sleep(sleeping_time)
0

I've developed a C app which does this. See https://github.com/goglecm/AutoBrightnessCam .

It uses fswebcam to take pictures, can be configured with systemd to start automatically and detects whether the laptop is discharging (then it kicks in).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.