View
@@ -19,7 +19,7 @@
#include "zm.h"
#include "zm_font.h"
#include "zm_image.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
@@ -374,34 +374,72 @@ bool Image::ReadJpeg( const char *filename )
return( true );
}
bool Image::WriteJpeg( const char *filename, int quality_override ) const
bool Image::WriteJpeg( const char *filename, int quality_override, bool on_blocking_abort ) const
{
if ( config.colour_jpeg_files && colours == 1 )
{
Image temp_image( *this );
temp_image.Colourise();
return( temp_image.WriteJpeg( filename, quality_override ) );
return( temp_image.WriteJpeg( filename, quality_override, on_blocking_abort ) );
}
int quality = quality_override?quality_override:config.jpeg_file_quality;
struct jpeg_compress_struct *cinfo = jpg_ccinfo[quality];
FILE *outfile =NULL;
static int raw_fd = 0;
bool need_create_comp = false;
raw_fd = 0;
if ( !cinfo )
{
cinfo = jpg_ccinfo[quality] = new jpeg_compress_struct;
cinfo->err = jpeg_std_error( &jpg_err.pub );
jpg_err.pub.error_exit = zm_jpeg_error_exit;
jpg_err.pub.emit_message = zm_jpeg_emit_message;
jpeg_create_compress( cinfo );
need_create_comp=true;
}
if (! on_blocking_abort)
{
jpg_err.pub.error_exit = zm_jpeg_error_exit;
jpg_err.pub.emit_message = zm_jpeg_emit_message;
}
else
{
jpg_err.pub.error_exit = zm_jpeg_error_silent;
jpg_err.pub.emit_message = zm_jpeg_emit_silence;
if (setjmp( jpg_err.setjmp_buffer ) )
{
jpeg_abort_compress( cinfo );
Debug( 5, "Aborted a write mid-stream and %s and %d", (outfile == NULL) ? "closing file" : "file not opened", raw_fd );
if (raw_fd)
close(raw_fd);
if (outfile)
fclose( outfile );
return ( false );
}
}
if (need_create_comp)
jpeg_create_compress( cinfo );
FILE *outfile;
if (! on_blocking_abort)
{
if ( (outfile = fopen( filename, "wb" )) == NULL )
{
Error( "Can't open %s: %s", filename, strerror(errno) );
Error( "Can't open %s for writing: %s", filename, strerror(errno) );
return( false );
}
}
else
{
raw_fd = open(filename,O_WRONLY|O_NONBLOCK|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (raw_fd < 0)
return ( false );
outfile = fdopen(raw_fd,"wb");
if (outfile == NULL)
{
close(raw_fd);
return( false );
}
}
jpeg_stdio_dest( cinfo, outfile );
cinfo->image_width = width; /* image width and height, in pixels */
View
@@ -136,7 +136,7 @@ class Image
bool WriteRaw( const char *filename ) const;
bool ReadJpeg( const char *filename );
bool WriteJpeg( const char *filename, int quality_override=0 ) const;
bool WriteJpeg( const char *filename, int quality_override=0, bool on_blocking_abort=false ) const;
bool DecodeJpeg( const JOCTET *inbuffer, int inbuffer_size );
bool EncodeJpeg( JOCTET *outbuffer, int *outbuffer_size, int quality_override=0 ) const;
View
@@ -29,7 +29,13 @@ extern "C"
#define MAX_JPEG_ERRS 25
static int jpeg_err_count = 0;
void zm_jpeg_error_silent( j_common_ptr cinfo ){
zm_error_ptr zmerr = (zm_error_ptr)cinfo->err;
longjmp( zmerr->setjmp_buffer, 1 );
}
void zm_jpeg_emit_silence( j_common_ptr cinfo, int msg_level ){
}
void zm_jpeg_error_exit( j_common_ptr cinfo )
{
static char buffer[JMSG_LENGTH_MAX];
View
@@ -38,6 +38,9 @@ struct zm_error_mgr
typedef struct zm_error_mgr *zm_error_ptr;
void zm_jpeg_error_silent( j_common_ptr cinfo );
void zm_jpeg_emit_silence( j_common_ptr cinfo, int msg_level );
void zm_jpeg_error_exit( j_common_ptr cinfo );
void zm_jpeg_emit_message( j_common_ptr cinfo, int msg_level );
View
@@ -40,6 +40,7 @@
#if HAVE_LIBAVFORMAT
#include "zm_ffmpeg_camera.h"
#endif // HAVE_LIBAVFORMAT
#include "zm_fifo.h"
#if ZM_MEM_MAPPED
#include <sys/mman.h>
@@ -2692,9 +2693,11 @@ unsigned int Monitor::DetectMotion( const Image &comp_image, Event::StringSet &z
static char diag_path[PATH_MAX] = "";
if ( !diag_path[0] )
{
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-r.jpg", config.dir_events, id );
snprintf( diag_path, sizeof(diag_path), config.record_diag_images_fifo ? "%s/%d/diagpipe-r.jpg" : "%s/%d/diag-r.jpg", config.dir_events, id );
if (config.record_diag_images_fifo)
FifoStream::fifo_create_if_missing(diag_path);
}
ref_image.WriteJpeg( diag_path );
ref_image.WriteJpeg( diag_path,0,config.record_diag_images_fifo );
}
Image *delta_image = ref_image.Delta( comp_image );
@@ -2704,9 +2707,12 @@ unsigned int Monitor::DetectMotion( const Image &comp_image, Event::StringSet &z
static char diag_path[PATH_MAX] = "";
if ( !diag_path[0] )
{
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-d.jpg", config.dir_events, id );
snprintf( diag_path, sizeof(diag_path), config.record_diag_images_fifo ? "%s/%d/diagpipe-d.jpg" : "%s/%d/diag-d.jpg", config.dir_events, id );
if (config.record_diag_images_fifo)
FifoStream::fifo_create_if_missing(diag_path);
}
delta_image->WriteJpeg( diag_path );
delta_image.WriteJpeg( diag_path,0,config.record_diag_images_fifo );
}
// Blank out all exclusion zones
View
@@ -22,6 +22,7 @@
#include "zm_zone.h"
#include "zm_image.h"
#include "zm_monitor.h"
#include "zm_fifo.h"
void Zone::Setup( Monitor *p_monitor, int p_id, const char *p_label, ZoneType p_type, const Polygon &p_polygon, const Rgb p_alarm_rgb, CheckMethod p_check_method, int p_min_pixel_threshold, int p_max_pixel_threshold, int p_min_alarm_pixels, int p_max_alarm_pixels, const Coord &p_filter_box, int p_min_filter_pixels, int p_max_filter_pixels, int p_min_blob_pixels, int p_max_blob_pixels, int p_min_blobs, int p_max_blobs, int p_overload_frames )
{
@@ -217,10 +218,13 @@ bool Zone::CheckAlarms( const Image *delta_image )
static char diag_path[PATH_MAX] = "";
if ( !diag_path[0] )
{
snprintf( diag_path, sizeof(diag_path), "%s/%s/diag-%d-%d.jpg", config.dir_events, monitor->Name(), id, 1 );
snprintf( diag_path, sizeof(diag_path), config.record_diag_images_fifo ? "%s/%s/diagpipe-%d-%d.jpg" : "%s/%s/diag-%d-%d.jpg", config.dir_events, monitor->Name(), id, 1 );
if (config.record_diag_images_fifo)
FifoStream::fifo_create_if_missing(diag_path);
}
diff_image->WriteJpeg( diag_path );
diff_image->WriteJpeg( diag_path,0,config.record_diag_images_fifo );
}
<<<<<<< HEAD
if ( !alarm_pixels )
{
@@ -235,6 +239,28 @@ bool Zone::CheckAlarms( const Image *delta_image )
overload_count = overload_frames;
return( false );
}
=======
if ( pixel_diff_count && alarm_pixels )
pixel_diff = pixel_diff_count/alarm_pixels;
Debug( 5, "Got %d alarmed pixels, need %d -> %d, avg pixel diff %d", alarm_pixels, min_alarm_pixels, max_alarm_pixels, pixel_diff );
if (config.record_diag_images_fifo)
FifoDebug( 5, "%d#ALRM#%d#%d", id,alarm_pixels, pixel_diff );
if( alarm_pixels ) {
if( min_alarm_pixels && (alarm_pixels < min_alarm_pixels) ) {
/* Not enough pixels alarmed */
return (false);
} else if( max_alarm_pixels && (alarm_pixels > max_alarm_pixels) ) {
/* Too many pixels alarmed */
overload_count = overload_frames;
return (false);
}
} else {
/* No alarmed pixels */
return (false);
}
>>>>>>> ce45843... fifo diag support
score = (100*alarm_pixels)/polygon.Area();
Debug( 5, "Current score is %d", score );
@@ -307,11 +333,14 @@ bool Zone::CheckAlarms( const Image *delta_image )
static char diag_path[PATH_MAX] = "";
if ( !diag_path[0] )
{
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 2 );
snprintf( diag_path, sizeof(diag_path), config.record_diag_images_fifo ? "%s/%d/diagpipe-%d-%d.jpg" : "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 2 );
if (config.record_diag_images_fifo)
FifoStream::fifo_create_if_missing(diag_path);
}
diff_image->WriteJpeg( diag_path );
diff_image->WriteJpeg( diag_path,0,config.record_diag_images_fifo );
}
Debug( 5, "Got %d filtered pixels, need %d -> %d", alarm_filter_pixels, min_filter_pixels, max_filter_pixels );
<<<<<<< HEAD
if ( !alarm_filter_pixels )
{
@@ -327,6 +356,24 @@ bool Zone::CheckAlarms( const Image *delta_image )
return( false );
}
=======
if (config.record_diag_images_fifo)
FifoDebug( 5, "%d#FILT#%d", id,alarm_filter_pixels );
if( alarm_filter_pixels ) {
if( min_filter_pixels && (alarm_filter_pixels < min_filter_pixels) ) {
/* Not enough pixels alarmed */
return (false);
} else if( max_filter_pixels && (alarm_filter_pixels > max_filter_pixels) ) {
/* Too many pixels alarmed */
overload_count = overload_frames;
return (false);
}
} else {
/* No filtered pixels */
return (false);
}
>>>>>>> ce45843... fifo diag support
score = (100*alarm_filter_pixels)/(polygon.Area());
Debug( 5, "Current score is %d", score );
@@ -527,9 +574,11 @@ bool Zone::CheckAlarms( const Image *delta_image )
static char diag_path[PATH_MAX] = "";
if ( !diag_path[0] )
{
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 3 );
snprintf( diag_path, sizeof(diag_path), config.record_diag_images_fifo ? "%s/%d/diagpipe-%d-%d.jpg" : "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 3 );
if (config.record_diag_images_fifo)
FifoStream::fifo_create_if_missing(diag_path);
}
diff_image->WriteJpeg( diag_path );
diff_image->WriteJpeg( diag_path,0,config.record_diag_images_fifo );
}
if ( !alarm_blobs )
@@ -538,6 +587,8 @@ bool Zone::CheckAlarms( const Image *delta_image )
}
alarm_blob_pixels = alarm_filter_pixels;
Debug( 5, "Got %d raw blob pixels, %d raw blobs, need %d -> %d, %d -> %d", alarm_blob_pixels, alarm_blobs, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs );
if (config.record_diag_images_fifo)
FifoDebug( 5, "%d#RBLB#%d#%d", id, alarm_blob_pixels, alarm_blobs );
// Now eliminate blobs under the threshold
for ( int i = 1; i < WHITE; i++ )
@@ -586,10 +637,13 @@ bool Zone::CheckAlarms( const Image *delta_image )
static char diag_path[PATH_MAX] = "";
if ( !diag_path[0] )
{
snprintf( diag_path, sizeof(diag_path), "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 4 );
snprintf( diag_path, sizeof(diag_path), config.record_diag_images_fifo ? "%s/%d/diagpipe-%d-%d.jpg" : "%s/%d/diag-%d-%d.jpg", config.dir_events, monitor->Id(), id, 4 );
if (config.record_diag_images_fifo)
FifoStream::fifo_create_if_missing(diag_path);
}
diff_image->WriteJpeg( diag_path );
diff_image->WriteJpeg( diag_path,0,config.record_diag_images_fifo );
}
<<<<<<< HEAD
Debug( 5, "Got %d blob pixels, %d blobs, need %d -> %d, %d -> %d", alarm_blob_pixels, alarm_blobs, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs );
if ( !alarm_blobs )
@@ -605,6 +659,29 @@ bool Zone::CheckAlarms( const Image *delta_image )
overload_count = overload_frames;
return( false );
}
=======
Debug( 5, "Got %d blob pixels, %d blobs, need %d -> %d, %d -> %d", alarm_blob_pixels, alarm_blobs, min_blob_pixels, max_blob_pixels, min_blobs, max_blobs );
if (config.record_diag_images_fifo)
FifoDebug( 5, "%d#FBLB#%d#%d", id, alarm_blob_pixels, alarm_blobs );
if( alarm_blobs ) {
if( min_blobs && (alarm_blobs < min_blobs) ) {
/* Not enough pixels alarmed */
return (false);
} else if(max_blobs && (alarm_blobs > max_blobs) ) {
/* Too many pixels alarmed */
overload_count = overload_frames;
return (false);
}
} else {
/* No blobs */
return (false);
}
score = (100*alarm_blob_pixels)/(polygon.Area());
if(score < 1)
score = 1; /* Fix for score of 0 when frame meets thresholds but alarmed area is not big enough */
Debug( 5, "Current score is %d", score );
>>>>>>> ce45843... fifo diag support
alarm_lo_x = polygon.HiX()+1;
alarm_hi_x = polygon.LoX()-1;
View
@@ -24,6 +24,7 @@
#include "zm_db.h"
#include "zm_signal.h"
#include "zm_monitor.h"
#include "zm_fifo.h"
void Usage()
{
@@ -91,8 +92,9 @@ int main( int argc, char *argv[] )
snprintf( log_id_string, sizeof(log_id_string), "zma_m%d", id );
zmLoadConfig();
logInit( log_id_string );
zmFifoDbgInit( id );
Monitor *monitor = Monitor::Load( id, true, Monitor::ANALYSIS );
View
@@ -25,6 +25,7 @@
#include "zm_user.h"
#include "zm_signal.h"
#include "zm_monitor.h"
#include "zm_fifo.h"
bool ValidateAccess( User *user, int mon_id )
{
@@ -54,7 +55,7 @@ int main( int argc, const char *argv[] )
{
srand( getpid() * time( 0 ) );
enum { ZMS_MONITOR, ZMS_EVENT } source = ZMS_MONITOR;
enum { ZMS_MONITOR, ZMS_EVENT, ZMS_FIFO } source = ZMS_MONITOR;
enum { ZMS_JPEG, ZMS_MPEG, ZMS_RAW, ZMS_ZIP, ZMS_SINGLE } mode = ZMS_JPEG;
char format[32] = "";
int monitor_id = 0;
@@ -117,6 +118,8 @@ int main( int argc, const char *argv[] )
if ( !strcmp( name, "source" ) )
{
source = !strcmp( value, "event" )?ZMS_EVENT:ZMS_MONITOR;
if (! strcmp( value,"fifo") )
source = ZMS_FIFO;
}
else if ( !strcmp( name, "mode" ) )
{
@@ -321,5 +324,12 @@ int main( int argc, const char *argv[] )
}
stream.runStream();
}
else if (source == ZMS_FIFO )
{
FifoStream stream;
stream.setStreamMaxFPS( maxfps );
stream.setStreamStart( monitor_id, format );
stream.runStream();
}
return( 0 );
}