#include #include #include #include // OpenCV Webcam Start #include #include // Needed only in C enum boolean { true = 1, false = 0 }; typedef enum boolean bool; FILE * logfile = NULL; /* char *timetochar(const struct tm *timeptr) { static char wday_name[7][3] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static char mon_name[12][3] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static char result[26]; sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", wday_name[timeptr->tm_wday], mon_name[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, 1900 + timeptr->tm_year); return result; }*/ char *replace(char *st, char *orig, char *repl) { static char buffer[4096]; char *ch; if (!(ch = strstr(st, orig))) return st; strncpy(buffer, st, ch-st); buffer[ch-st] = 0; sprintf(buffer+(ch-st), "%s%s", repl, ch+strlen(orig)); return buffer; } void write_log(char * str) { if ( logfile != NULL ) { char * text; struct tm *ptr; time_t tm; tm = time(NULL); ptr = localtime(&tm); fprintf( logfile, "[%s] [PID: %i] %s\n", replace(asctime(ptr),"\n",""), getpid(), str ); fflush( logfile ); } } void open_log() { char * logfilename = "civacam.log\0"; logfile = fopen (logfilename, "a"); } void close_log() { if ( logfile != NULL ) fclose( logfile ); } char msglog[1000]; bool debugging = false; bool black_image_detector(IplImage* inputImage) { if ( inputImage == 0 ) return true; // Go over all pixels in the detector int x = 0; for( x = 0 ; x < inputImage->width; x++) { int y = 0; for( y = 0; y < inputImage->height; y++) { // Get pixel value from the input image int inputVal = (int)(cvGet2D(inputImage,y,x).val[0]); if ( inputVal > 5 ) { if (debugging) sprintf( msglog, "[DEBUG] dentro do black_image_detector vou retornou false" ); if (debugging) write_log(&msglog[0]); return false; } } } return true; } int main(int argc, char *argv[]) { open_log(); if ( argc == 1 ) { write_log("[FATAL] PROCESSO INTERROMPIDO! Motivo: Processo civacam chamado sem argumentos, Obrigatoria a informacao do endereco de destino do arquivo."); return 1; } char * outfilename = argv[1]; sprintf( msglog, "[INFO] Processo civacam iniciado para capturar foto com nome de arquivo: [%s]...", outfilename ); write_log(&msglog[0]); /* // A way to set directshow input to composite - Only C++ videoInput VI; VI.setupDevice(0, VI_COMPOSITE); VI.setFormat(0, VI_NTSC_M); VI.stopDevice(0); sprintf( msglog, "[DEBUG] Terminou de configurar e fechar a camera 0 " ); write_log(&msglog[0]); */ CvCapture* capture = cvCaptureFromCAM(0); // open the default camera id == 0 // Exit if fail to open a Webcam if( capture == NULL ) { sprintf( msglog, "[FATAL] PROCESSO INTERROMPIDO! Motivo: Impossivel acessar a camera. Arquivo solicitado: [%s]...", outfilename ); write_log(&msglog[0]); exit(1); } if (debugging) write_log("[DEBUG] Camera aberta"); IplImage* img = 0; while ( black_image_detector(img) ) { if (debugging) sprintf( msglog, "[DEBUG] black_image_detector retornou true... continua loop..." ); if (debugging) write_log(&msglog[0]); if(!cvGrabFrame(capture)){ // capture a frame sprintf( msglog, "[FATAL] PROCESSO INTERROMPIDO! Motivo: Impossivel captura um frame da camera. Arquivo solicitado: [%s]...", outfilename ); write_log(&msglog[0]); exit(1); } if (debugging) sprintf( msglog, "[DEBUG] passou do cvGrabFrame ok ..." ); if (debugging) write_log(&msglog[0]); img=cvRetrieveFrame(capture,0); // retrieve the captured frame if (debugging) sprintf( msglog, "[DEBUG] passou do cvRetrieveFrame..." ); if (debugging) write_log(&msglog[0]); if ( img == NULL || img == 0 ) cvWaitKey(100); } if (debugging) sprintf( msglog, "[DEBUG] black_image_detector retornou false" ); if (debugging) write_log(&msglog[0]); cvSaveImage(outfilename, img, 0); // FECHANDO... sprintf( msglog, "[INFO] OK ! Processo terminado normalmente. Arquivo capturado: [%s]...", outfilename ); write_log(&msglog[0]); close_log(); cvReleaseCapture(&capture); return 0; }
Comentários