A principle of operation: the script periodically receives from ZoneMinder a current frame, calculates average value of brightness of all pixels and depending on result switches on or off an intra-red highlighting.
Howto install:
- save texts of programs in appropriate files (see comments in the beginning)
- compile jpgbright.c the command: gcc -O2 -s -o jpgbright -ljpeg jpgbright.c
- add in crontab a line similar to these: */5 5-10,16-22 * * * /usr/local/zm/opt/lightctl
- in ZoneMinder add the special user for dialogue of a script with ZoneMinder, no access rights are necessary for this user for version 1.19.4.
- in the beginning of a file lightctl correct value of variables for your variant.
For me for handle of highlighting it is used OneWire device if for you differently - correct a code of functions light_on and light_off.
Code: Select all
#!/bin/sh
# File lightctl
ON_VAL=100
OFF_VAL=125
ZMU=/usr/local/zm/bin/zmu
ZMUSER=irlight
ZMPASS=irlight
MONITORID=1
JPGBRIGHT=/usr/local/zm/opt/jpgbright
STAT=/var/tmp/irlight
light_on () {
echo 1 >/mnt/1wire/12.264329000000/PIO.A
}
light_off () {
echo 0 >/mnt/1wire/12.264329000000/PIO.A
}
cd /var/tmp
if MONNAME=`$ZMU -m $MONITORID -U $ZMUSER -P $ZMPASS -qi | grep '^Name :' | sed -e 's/^Name : //'`; then
newbr=`$JPGBRIGHT $MONNAME.jpg`
rm -f $MONNAME.jpg
fi
[ "$newbr" = "" ] && exit 1
currdate=`date '+%Y%m%d%H%M%S'`
[ -f $STAT ] || echo "0 0 $currdate" >$STAT
lastst=`cat $STAT | cut -d' ' -f 1`
[ "$lastst" = "" ] && lastst=0
newst=$lastst
lastbr=`cat $STAT | cut -d' ' -f 2`
[ "$lastbr" = "" ] && lastbr=0
if [ $lastst = 0 ]; then
if [ $newbr -le $ON_VAL ]; then
newst=1
light_on
fi
else
if [ $newbr -ge $OFF_VAL ]; then
newst=0
light_off
fi
fi
echo "$newst $newbr $currdate" >$STAT
exit 0
Code: Select all
/* File jpgbright.c */
#include <stdio.h>
#include "jpeglib.h"
#include <setjmp.h>
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
typedef struct my_error_mgr * my_error_ptr;
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
GLOBAL(int)
read_JPEG_file (char * filename)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
int p;
long s;
if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return 0;
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
s = 0;
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
for (p=0;p<row_stride;p+=3) {
s += buffer[0][p] + buffer[0][p+1] + buffer[0][p+2];
}
}
printf("%i\n", s / cinfo.output_height / row_stride);
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 1;
}
int main(int ac, char **av) {
if (ac != 2) {
fprintf(stderr, "Usage: %s file.jpg\n", av[0]);
exit(1);
}
if (!read_JPEG_file(av[1])) exit(1);
exit(0);
}