[Phission-cvs] phission/examples/cpp/MotionVCR/src MotionVCR.cpp,NONE,1.1
Brought to you by:
pthoren
From: Philip T. <pt...@us...> - 2005-10-27 23:43:07
|
Update of /cvsroot/phission/phission/examples/cpp/MotionVCR/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16864/MotionVCR/src Added Files: MotionVCR.cpp Log Message: ddimage based motion detector to record x seconds of jpgs @ y fps into the ./record directory Security monitoring --- NEW FILE: MotionVCR.cpp --- /* --------------------------------------------------------------------------- Phission : Realtime Vision Processing System Copyright (C) 2003 Philip D.S. Thoren (pt...@cs...) University of Massachusetts at Lowell, Laboratory for Artificial Intelligence and Robotics This file is part of Phission. Phission is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Phission is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Phission; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------------------------------------------------------------*/ #include <time.h> #include <sys/time.h> #include <MotionVCR.h> #include <phission.h> /* ------------------------------------------------------------------------ */ int glbl_disable_displays = 0; /* ------------------------------------------------------------------------ */ void usage() { printf("\n\n\tUsage:\n"); printf("\t\t\t--help\t\t\tdisplay usage\n"); printf("\t\t\t--nodisplay\tdisable the allocation, opening or any use of a display.\n"); printf("\t\t\t--test <value> <count>\tsleep 'value' seconds and then close test\n"); printf("\t\t\t\t\tloops 'count' times during histogram before sleeping\n"); printf("\n\n"); exit(1); } /* ------------------------------------------------------------------------ */ int main(int argc, char *argv[] ) { phFUNCTION("main") #define MEDIUM() 1 #define SLOW() 0 int32_t record_seconds = 10; /* save ten seconds @ fps */ #if MEDIUM() int32_t fps = 15; /* number of frames in a second */ int sleeptime = 6666; /* us */ #elif SLOW() int32_t fps = 5; /* number of frames in a second */ int sleeptime = 20000; /* us */ #else int32_t fps = 30; /* number of frames in a second */ int sleeptime = 0; /* us */ #endif char *filename_prefix = "record/img"; char filename[512]; char timestamp_string[512]; int32_t frame_index = 0; struct tm *current_time = NULL; time_t timep = 0; time_t last_time = 0; struct tm result; uint32_t capture_image_count = 0; phImage image; int32_t minimum_blob_size = 25; unsigned int i = 0; unsigned int nDisplays = 2; char title[255]; phDisplayInterface **display = NULL; #ifdef WIN32 VFWSource *capture = new VFWSource(); #else V4LCapture *capture = new V4LCapture(); #endif phPipeline *pipeline = new phPipeline(); phColor incolor_limit = phColorGREY8_new(255); phColor threshold_limit = phColorGREY8_new(0); phColor outcolor_limit = phColorRGB24_new(255,0,0); phBlobData blob_data; blob_Filter *blob = new blob_Filter(); gaussianBlur_Filter *gauss = new gaussianBlur_Filter(); convert_Filter *convert = new convert_Filter(phImageGREY8); ddimage_Filter *ddimage = new ddimage_Filter(15,/* threshold: 0-255 */ 0, /* pixel-count: */ 3 /* stride */ ); /* Remove the code below when using this code as an example. * * This just checks whether "--test" has been specified with * a time value argument. It's for testing all the examples * without the need for human intervention. */ int test = 0; int test_loop_count = 0; int count = 0; /* Utility class to ease the starting and stopping of displays, captures and * pipelines */ phSystem system; phArgTable arg_parser; /* Setup and parse all the arguments */ rc = arg_parser.add("--test",&test,phARG_INT); phCHECK_RC(rc,NULL,"arg_parser.add"); rc = arg_parser.add("--nodisplay",&glbl_disable_displays,phARG_BOOL); phCHECK_RC(rc,NULL,"arg_parser.add"); rc = arg_parser.add("--help",(void *)&usage,phARG_FUNC); phCHECK_RC(rc,NULL,"arg_parser.add"); rc = arg_parser.parse(argc,argv); phCHECK_RC(rc,NULL,"arg_parser.parse"); count = test; if (glbl_disable_displays) nDisplays = 0; /* <>) Setup the capture device parameters */ capture->set(320,240,"/dev/video0"); capture->setChannel(0); capture->setBrightness(28000); capture->setContrast(28000); rc = system.addCapture(capture); phPRINT_RC(rc,NULL,"system.addCapture(capture)"); display = new phDisplayInterface *[nDisplays]; phCHECK_NULLPTR(display,"new","new phDisplayInterface *[nDisplays];"); for (i = 0; i < nDisplays; i++ ) { sprintf(title,"MotionVCR[%u]",i); display[i] = new X11Display(320,240,title); rc = system.addDisplay(display[i]); phPRINT_RC(rc,NULL,"system.addDisplay(display[i:%d])",i); } rc = system.addPipeline(pipeline); phPRINT_RC(rc,NULL,"system.addPipeline(pipeline)"); /* <> Attach the displays to the live sources */ /* Capture -> (b)phPipeline -> (a)Processed Output Display */ /* <> Attach the Pipeline output image to the display so the * display updates when a processing has been completed * for each loop */ if (nDisplays > 0) display[0]->setLiveSourceInput(pipeline->getLiveSourceOutput()); if (nDisplays > 1) display[1]->setLiveSourceInput(capture->getLiveSourceOutput()); rc = image.connect(capture->getLiveSourceOutput()); phPRINT_RC(rc,NULL,"image.connect(capture->output)"); /* <> Attach the capture image to the live source input of the * Pipeline so processing is done automatically once the * pipeline is started. */ pipeline->setLiveSourceInput(capture->getLiveSourceOutput()); /* <> Add the filter(s) into the pipeline in the order they will * be used */ pipeline->add(gauss); pipeline->add(convert); pipeline->add(ddimage); blob->set(&incolor_limit,&threshold_limit,1,&outcolor_limit,1,1); pipeline->add(blob); rc = blob_data.connect(blob->getLiveBlobOutput()); phPRINT_RC(rc,NULL,"blob_data.connect()"); /* <> Startup the system */ rc = system.startup(); phPRINT_RC(rc,NULL,"system.startup()"); /* <> Keep going until all the windows are closed */ while ((system.displaysActive() > 0) || (system.capturesActive() > 0)) { /* Yielding is optional. This gives up the thread's timeslice * to prevent slow response in other threads. It consumes more * CPU cycles than sleeping. Use it instead of sleeping if * this loop is processing anything */ phThread::yieldThread(); if ((rc = blob_data.update(phLiveObjectNOWAIT)) == phLiveObjectUPDATED) { //blob_data.print_data(minimum_blob_size); if (blob_data.getTotalBlobs(minimum_blob_size)) { //phPRINT("Motion\t"); capture_image_count = fps * record_seconds; } } else { phPRINT_RC(rc,NULL,"blob_data.update()"); } /* We're going to capture some number of frames ... */ if (capture_image_count > 0) { /* Get the latest image */ rc = image.update(phLiveObjectNOWAIT); phPRINT_RC(rc,NULL,"image.update()"); if (rc == phLiveObjectUPDATED) { /* decrement the total frames to capture */ capture_image_count--; /* Get the current time for the filename and timestamp */ time(&timep); /* create the time structure so we can get individual fields */ current_time = localtime_r(&timep,&result); /* Print the time fields into the string buffer */ sprintf(timestamp_string, "%04d_%02d_%02d_%02d:%02d:%02d", current_time->tm_year+1900, current_time->tm_mon, current_time->tm_mday, current_time->tm_hour, current_time->tm_min, current_time->tm_sec); /* Check to see if the timestamp has changed since the * last frame */ if (((int64_t)difftime(timep,last_time)) > 0) { /* phPRINT("%lld:%0.6lf:%d:%d\n", (int64_t)difftime(timep,last_time), difftime(timep,last_time), (int32_t)timep, (int32_t)last_time); */ last_time = timep; frame_index = 0; } /* If the timestamp hasn't changed than this is the <frame_count>th frame in the current second */ else { frame_index++; } /* Create the filename */ sprintf(filename, "%s_%s_%09d.jpg", filename_prefix, timestamp_string, frame_index); /* save the image */ rc = image.save(filename); phPRINT_RC(rc,NULL,"image.save(%s)",filename); phThread::usleep(sleeptime); } else { phThread::yieldThread(); } } /* Remove this if block when using this code as an example */ /* Set the loop control value to end the loop when testing */ if (test > 0) { if (test_loop_count > count) { sleep(test); /* test's value should be a time (in secs) value > 0*/ rc = system.shutdown(); phPRINT_RC(rc,NULL,"system.shutdown()"); } test_loop_count++; } } /* <> Shutdown the system */ rc = system.shutdown(); phPRINT_RC(rc,NULL,"system.shutdown()"); error: rc = blob_data.disconnect(); phPRINT_RC(rc,NULL,"blob_data.disconnect()"); for (i = 0; (i < nDisplays) && (display != NULL); i++) { phDelete(display[i]); } phDeleteArray(display); phDelete(capture); /* phDelete(sub); phDelete(add); phDelete(convert); phDelete(threshold); phDelete(mean); phDelete(gauss); */ phDelete(convert); phDelete(ddimage); phDelete(blob); phDelete(pipeline); return phSUCCESS; } |