You are not logged in.

#1 2011-05-04 10:35:32

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

calise - use the webcam as a light sensor

Hi, I wrote a a python program to use the webcam as a a light sensor. Since I was looking for something like this but I did't found nothing, I started my own.
Often I spend hours in the afternoon/evening on the laptop, but since I'm really concentrated, I don't notice that my lcd backlight is still on max value.
After a few code optimizations, changes and ideas I think that the program is ready for the beta. Anyway I'll be happy to recive hints and feedbacks.

For usage instructions: calise and calised man pages or calise/calised --help or refer to README on the package
For the first run use --calibrate options (I've seen that webcams white-balance is a lot different from cam to cam)

Dependencies
- Python
- PyGame
- PIL
- NumPy
- PyQt4

http://sourceforge.net/projects/calise/

AUR package at http://aur.archlinux.org/packages.php?ID=50801

Last edited by smilzoboboz (2012-06-04 07:25:41)

Offline

#2 2011-05-04 10:47:36

metre
Member
Registered: 2011-03-13
Posts: 130

Re: calise - use the webcam as a light sensor

Brilliant idea: it deserves an AUR package

Offline

#3 2011-05-04 12:54:11

honki
Member
Registered: 2005-11-04
Posts: 16

Re: calise - use the webcam as a light sensor

Excellent idea! Unfortunately its crashing quite frequently on my machine. Here's what I did:

$ sudo python2 camsensor.py --calibrate --auto
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Cover the webcam and then press enter 
Now calibrating, do not uncover the webcam...

Measurements: [10500, 71224, 10500, 71414, 71160, 70936]
Standard Deviation: 28606.8158487
minimum value set: 50955

Do you want to set also max value? [y/N] 

Calibration finished, optimal values for your system are: --base=454.477722226 --offset=50500.5222778. Next time give these two parameters to the program.

Main program started... Hit CTRL+C to kill
Backlight level changed from 8 to 5 (62.0249840476%) [avg on 1]
Traceback (most recent call last):
  File "camsensor.py", line 169, in <module>
    flt = defgap(imageprocess(frame),ofs,bse)
  File "camsensor.py", line 62, in defgap
    flt = (math.log(lit-ofs,bse)-1)*100
ValueError: math domain error


$ sudo python2 camsensor.py --auto --base=454.477722226 --offset=50500.5222778
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Main program started... Hit CTRL+C to kill
Backlight level changed from 4 to 5 (63.4762394739%) [avg on 1]
Backlight level changed from 5 to 3 (41.2283002379%) [avg on reset]
Backlight level changed from 3 to 6 (68.4045826366%) [avg on 1]
Backlight level changed from 6 to 5 (64.7286649268%) [avg on 2]
Traceback (most recent call last):
  File "camsensor.py", line 169, in <module>
    flt = defgap(imageprocess(frame),ofs,bse)
  File "camsensor.py", line 62, in defgap
    flt = (math.log(lit-ofs,bse)-1)*100
ValueError: math domain error

Also, in the few seconds it worked it had already changed my display brightness several times which would probably become quite annoying after a few minutes. Is there any way to change the 'sensitivity' of the program?

Last edited by honki (2011-05-04 12:55:33)

Offline

#4 2011-05-04 13:05:44

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

Just fixed, I uploaded the wrong file... It was caused by log(-x) !!!
Now it should be correct, I added "if lit <= ofs : ofs = 0"

@honk: maybe it was not clear, I see you have an offset value of ~50500, when asked, you have to cover the webcam (no light as much as possible). I used my palm. The offset value should not be higher than 5000 or so.
I'm sorry I gave a wrong hint, --base and --offset syntax is different (refer to --help, there is no "=" but " ")

And yes, there is sensitivity... I worked a bit in that sense. At start (with correct input values) it changes 2-3 times in row (white balance), then there is an average value. The average resets if there is a drastic change of brightness (>15%). And for me that works very well.

For the first run avoid root privileges and --auto parameter, test with --verbose first.

Last edited by smilzoboboz (2011-05-04 13:23:57)

Offline

#5 2011-05-04 13:19:05

honki
Member
Registered: 2005-11-04
Posts: 16

Re: calise - use the webcam as a light sensor

Thanks, I just tried your new version and it seems to work just fine now smile I actually covered my camera when it told me to do so and even after calibrating a second time the offset value is still ~10045...

I'm off now but I'll test it again tonight.

Offline

#6 2011-05-06 13:48:53

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

I just changed the equation that calculates the percent. Now fits more with the real values but uses a bit more CPU. I'll optimize in next releases. The parameters have been modified a bit: since there are no more logarythms, the --base parameter changed to --delta parameter and has totally different usage/values than before.
Added also the --steps switch. For LCD with more than ten steps of backlight.
However some big problems remain: the memory usage (~100MB), the CTRL+C way to terminate and the CPU usage (maybe the more easy to fix)
I want to fix them before the release of the beta (so that the beta won't be buggy).
I'd be glad if you give me an hand. And of course let me know if it works, what would you change, the errors in the code (I know that has to be cleaned a lot), and so on.
Bye wink

Last edited by smilzoboboz (2011-05-06 13:50:55)

Offline

#7 2011-05-11 17:01:36

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

alpha8 released, big improvements and dependencies changed. Check --help for some help on new features. I'll write something more precise soon

Offline

#8 2011-05-11 17:56:47

Jelle
Member
From: Netherlands
Registered: 2011-01-30
Posts: 84

Re: calise - use the webcam as a light sensor

Very , very cool! Thanks for your (continuing) efforts big_smile

Offline

#9 2011-05-18 13:40:31

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

alpha10 released. I rewrited the code on classes/objects to be better for everyone wink
With the rewriting most of the parameters have been adapted and optimized (eg. brightness is not 0<255000 anymore, now is 0<255), and the equation have been changed. I also removed two parameters (--acpi ad --cam).
I didnt search where and how the code can be optimized, but since it changed a lot will take its time (and the code it's still very buggy).
BTW now it's also on sourceforge subversion.

Last edited by smilzoboboz (2011-05-24 22:53:46)

Offline

#10 2011-05-24 23:15:09

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

Hi, the tings are going on very well. Since I started using to svn, main releases stopped at alpha11 but I continued coding on svn and now the program is almost feature complete (intended features, of course). I need as more as possible of you to test the code for bugs (just run it and report errors). Virtually, any error that is not excepted has to be fatal, but anyway please report everything.
Since I re-rewritten most of the code I found out and fixed two or three redundant passages, and so, now CPU usage is ~70% then before, RAM usage goes from 28 MB to 20 MB.
To get the last revision:

svn co https://camsensor.svn.sourceforge.net/svnroot/camsensor camsensor

With revision 14 the program supports profiles, so, on the first run, it asks for calibration and writes the default configuration. Then it will read the default configuration by default (repetita juvant LOL), so a tipical command for the second run of the program will be

camsensor --auto # no output log, only logs when backlight changes
camsensor --verbose --auto # simple output data log
camsensor --debug --auto # full data output log

and remember that you have to have writing rights on the file specified in "Camera>path" section of the default.cfg obtained after the calibration
Last but not least, now it can be used as daemon (user daemon, not root) because I excepted SIGterm, and KeyboardInterrupt, so even if recives TERM signals (eg. daemon stop) it will exit gracefully.

Last edited by smilzoboboz (2011-05-24 23:19:57)

Offline

#11 2011-06-20 18:30:06

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

0.0.1 beta is out.
Almost every feature I wanted is full-working.

Major changes are:
Profiles: improved a bit, switching from one another changes all program vars without the need of a calibration (of course you need to calibrate to create a profile...)
Library: can be used as library that takes a frame from the cam or a screenshoot and returns average lightness (python lib 'camsensor')
Translations: full support for gettext localization (please help translating, I cannot go further English and Italian).
Setup: cames also with a nice setup script (distutils, distutils-extra)
And of course I managed to speed everything up a bit.

Added AUR package at https://aur.archlinux.org/packages.php?ID=50020

Last edited by smilzoboboz (2011-06-20 19:09:41)

Offline

#12 2011-11-30 21:52:00

smilzoboboz
Member
Registered: 2010-06-01
Posts: 19

Re: calise - use the webcam as a light sensor

Just to point out: the project is alive and kicking, now it's on version 0.0.7.

Project blog; http://sourceforge.net/apps/wordpress/calise/
Download: http://sourceforge.net/projects/calise/
AUR: see first post

Last edited by smilzoboboz (2011-11-30 21:52:25)

Offline

#13 2013-04-09 17:07:40

kelloco2
Member
Registered: 2012-02-13
Posts: 121

Re: calise - use the webcam as a light sensor

Brilliant app. Proops !Thanks!


sorry for my english. {Arch Linux, Debian} User

Offline

Board footer

Powered by FluxBB