Tweak Brightness at sunrise/sunset

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
dreadlocks
Posts: 16
Joined: Tue Nov 30, 2004 9:53 am
Contact:

Tweak Brightness at sunrise/sunset

Post by dreadlocks »

My camera has poor visibility after it gets dark and I have to manually adjust the brightness settings every day.. so I made a script to do it and here it is..

chbrightness.sh
#!/bin/sh
if [ "$1" = "day" ];then
mysql -u YOURUSER --password=YOURPASS zm < /path/to/day.contrast.sql
killall -9 zmc
elif [ "$1" = "night" ];then
mysql -u YOURUSER --password=YOURPASS zm < /path/to/night.contrast.sql
killall -9 zmc
else
echo DAY? OR NIGHT?
fi
then I made 2 .sql files that have the settings I wish to change..

day.contrast.sql (change ID = to your camera ID)
UPDATE `Monitors` SET `Host` = NULL ,
`Path` = NULL ,
`Brightness` = '32768' WHERE `Id` = '1' LIMIT 1 ;
and night.contrast.sql (again, change ID = to your camera ID)
UPDATE `Monitors` SET `Host` = NULL ,
`Path` = NULL ,
`Brightness` = '42768' WHERE `Id` = '1' LIMIT 1 ;
now you should be able to run "./chbrightness.sh day" and watch your camera switch its brightness to how you would like it in the day, then try "./chbrightness.sh night" and it should switch to your evening contrast settings..

then I got a program called 'sunwait' that waits until sunrise or sunset from
http://www.risacher.org/sunwait/
build and install it somewhere and then locate the longitude and latitude for your location.. a simple search of google should find this (try: latitude and longitude for YOURCITY)
and here is my crontab that starts the program at 6am then waits til sunrise and executes the change, then at 5pm it does the same.. here the sun never rises before 6am and never sets before 5pm so I felt these were good times..
00 6 * * * /usr/local/bin/sunwait sun up 37.23N, 97.20W ; /path/to/chbrightness.sh day
00 17 * * * /usr/local/bin/sunwait sun down 37.23N, 97.20W ; /path/to/chbrightness.sh night
oskin
Posts: 84
Joined: Tue May 25, 2004 7:03 pm
Location: Moscow, Russia

Post by oskin »

It is better to take advantage of the utility zmu instead of killall. :)
zmu -Bxxx ....

... and instead of the hard setting of time of switching it is possible to use such way: http://www.zoneminder.com/forums/viewtopic.php?t=3657
Jolaf
Posts: 14
Joined: Mon May 02, 2005 10:46 pm
Location: Bordeaux (france)

Post by Jolaf »

I have the same problem.
I used Oskin's jpgbright program, and did my own lightctl script, based on his.

The script will try to maintain image brightness between LUM-LARG and LUM+LARG by progressively increasing or decreasing the camera brightness for monitor MONITORID.
It is very usefull for sunrise, sunset, light shift due to clouds passing ...

If captured image brightness is higher than LUM+LARG, INC will be subtracted to the actual camera brightness untill it becomes lower.
If captured image brightness is lower than LUM-LARG, INC will be added to the actual camera brightness untill it becomes higher.
You have to check that the add or subtraction of INC will not overshoot the 2*LARG band to prevent ligh oscillation. This is not critical however.

Date time actual image brightness, corrected brightness, actual camera brightness, and brightness camera shift are loged in a file as CSV format (and with csv extension), so that you just have to double click on it to open on your favorite spreadsheet editor.

Code: Select all

#!/bin/sh
# File lightctl

#captured image target brightness
LUM=160
#error (+/-)
LARG=10
#camera brightness increment
INC=1000

ZMU=/usr/local/bin/zmu
MONITORID=1
JPGBRIGHT=/usr/bin/jpgbright
STAT=/tmp/lightctl.log.csv


getbright() {
MONITORID=$1

if MONNAME=`$ZMU -m $MONITORID -qi | grep '^Name :' | sed -e 's/^Name : //'`; then
        newbr=`$JPGBRIGHT $MONNAME.jpg`
        rm -f $MONNAME.jpg
fi

return $newbr
}

cd /tmp #write enabled directory
lumiactuelle=`$ZMU -m $MONITORID -B`
val=$lumiactuelle
getbright $MONITORID
newbr=$?
oldbr=$newbr

$ZMU -u #Disable monitor detection

while [ $newbr -le $[$LUM - $LARG] ]
do
	val=$[$val+$INC]
    	$ZMU -m $MONITORID -B$val >/dev/null
	getbright $MONITORID
	newbr=$?
done

while [ $newbr -ge $[$LUM + $LARG] ]
do
	val=$[$val-$INC]
	$ZMU -m $MONITORID -B$val >/dev/null
	getbright $MONITORID
	newbr=$?
done

$ZMU -r #Reenable monitor detection

currdate=`date '+%d/%m/%Y %H:%M:%S'`
lumichangee=$[`$ZMU -m $MONITORID -B`-$lumiactuelle]

echo "$currdate,$oldbr,$newbr,$lumiactuelle,$lumichangee" >>$STAT
exit 0 
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi.

I guess it will help with regular cameras as well? How ZM should be configured to use it?

Thanks.
Jolaf
Posts: 14
Joined: Mon May 02, 2005 10:46 pm
Location: Bordeaux (france)

Post by Jolaf »

I don't understand what you mean.
I use a pal camera pluged into a PCTV, which is already a regular to me.

There is no need to configure ZM for my script to work.
Only crontab : the script is launched every ten minutes.
However, the script needs to be a litle bit more sophisticated : checking that monitor exists and is working. Checking the state (alert, prealert) before changing brigthness. And also tweaking contrast.
It would be very nice if ZM could execute a script on monitor start/stop.
It would also be nice if zmu allowed to get informations on a zone.
Perhaps I will fill a feature request for this.

Here is a graphic on several days of the image brightness control before and after executing the script.
Image
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi.

I meant if it works with usual, non-IR cameras, but I guess the answer is pretty obvious :).

I think this will be a good addition to ZM, and will enable it to handle the low-light situations (if not handled automatically by the camera).

What do you think, Phil?
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

I'd be very happy to add it to a 'contribs' directory in the distribution if someone can supply the files and a brief set of instructions for people to read to figure out how to use it.
Phil
Jolaf
Posts: 14
Joined: Mon May 02, 2005 10:46 pm
Location: Bordeaux (france)

Post by Jolaf »

Here is a cleaner version with a few parameter settings and some kind of help to use it : lightctl -?

Now you can specify the monitor and the parameters on the command line.
It is possible to run it without any parameters since there are valid default values in the script.

There is a very little error checking, scripting is not my party (this is my first one).
Feel free to fix whatever you want. Do I really need to provide this information ?

:arrow: I suggest reading the script :!:

Code: Select all

#!/bin/sh
# Lightctl : a simple solution for boring brighness adjustment problems

# Default working (for me) values
MONITORID=1
LUM=160
LARG=10
INC=1000

WORKINGDIR=/tmp #write access required for temporary files
STAT_DEB="/tmp/lightctl" # must include base dir and begining of file name
STAT_FIN="log.csv" # file extention

ZMU=/usr/local/bin/zmu #zmu file location
JPGBRIGHT=/usr/bin/jpgbright # Oskin's jpgbright program location cf. http://www.zoneminder.com/forums/viewtopic.php?t=3657

while getopts l:e:i:m: c
do
   case $c in
   m)    MONITORID=$OPTARG;;
   l)    LUM=$OPTARG;;
   e)    LARG=$OPTARG;;
   i)	 INC=$OPTARG;;
   ?)   echo "Lightctl : a simple solution for boring brighness adjustment problems"
         echo "lightctl options are : "
   	 echo '	-m monitor ID'
   	 echo '	-l targeted image brighness ($LUM) [0 255]'
   	 echo '	-e image brightness error tolerance ($LARG) [0 255], should be much smaller (10/20)'
   	 echo '	-i camera brightness increment/decrement values ($INC)'
   	 echo " "
   	 echo ' lightctl will try to maintain image brightness between $LUM-$LARG and $LUM+$LARG by adding or subtracting $INC to actual camera brightness until the image brightness belong to [$LUM-$LARG $LUM+$LARG]'
   	 echo " "
   	 echo " Date/time, actual image brightness, corrected image 
brightness, camera brightness, camera brightness shift are loged into a separated file for each monitorID. ie : /tmp/lightctl.1.log.csv in coma separated variable for convenient log analysis (plot)"
   	 echo " "
   	 echo " tested on a camera plugged into my PCTV"
   	 echo " For questions about users and password, refer to Oskin's post http://www.zoneminder.com/forums/viewtopic.php?t=3657"
   	 echo " Don't forget to check locations at begening of the script"
   	 echo " add a cron job such as */10 * * * * root /usr/local/bin/lightctl -m 1 -l 160 -e 10 -i 1000"
   	 exit -1;;
   esac
done

if [[ $LUM -gt 255 || $LUM -lt 0 ]]; 
then
	echo "Image brightness value : $LUM is out of bounds [0 255]"
	exit -2
fi

if [[ $LARG -gt 255 || $LARG -lt 0 ]];
then
	echo "Image brightness tolerance value $LARG is out of bouds [0 255]"
        exit -2
fi


if [[ $INC -lt 0 ]];
then
        echo "Camera brightness increment value $INC should be positive"
        exit -2
fi

$ZMU -m $MONITORID -i
if [ $? -eq 255 ];
then
	echo "The monitor \"$MONITORID\" does NOT exist"
	exit -2
fi

getbright() {
MONITORID=$1

if MONNAME=`$ZMU -m $MONITORID -qi | grep '^Name :' | sed -e 's/^Name : //'`; then
        newbr=`$JPGBRIGHT $MONNAME.jpg`
        rm -f $MONNAME.jpg
fi

return $newbr
}


logfilename=$STAT_DEB.$MONITORID.$STAT_FIN

cd $WORKINGDIR
lumiactuelle=`$ZMU -m $MONITORID -B`
val=$lumiactuelle
getbright $MONITORID
newbr=$?
oldbr=$newbr

$ZMU -m $MONITORID -u #deactivate detection

while [ $newbr -le $[$LUM - $LARG] ]
do
	val=$[$val+$INC]
    	$ZMU -m $MONITORID -B$val >/dev/null
	getbright $MONITORID
	newbr=$?
done

while [ $newbr -ge $[$LUM + $LARG] ]
do
	val=$[$val-$INC]
	$ZMU -m $MONITORID -B$val >/dev/null
	getbright $MONITORID
	newbr=$?
done

$ZMU -m $MONITORID -r #Reactivation de la detection

currdate=`date '+%d/%m/%Y %H:%M:%S'`
lumichangee=$[`$ZMU -m $MONITORID -B`-$lumiactuelle]

echo "$currdate,$oldbr,$newbr,$lumiactuelle,$lumichangee" >>$logfilename
exit 0 
Post Reply