demo.c
Go to the documentation of this file.
00001 #include "network.h"
00002 #include "detection_layer.h"
00003 #include "region_layer.h"
00004 #include "cost_layer.h"
00005 #include "utils.h"
00006 #include "parser.h"
00007 #include "box.h"
00008 #include "image.h"
00009 #include "demo.h"
00010 #include <sys/time.h>
00011 
00012 #define FRAMES 3
00013 
00014 #ifdef OPENCV
00015 #include "opencv2/highgui/highgui_c.h"
00016 #include "opencv2/imgproc/imgproc_c.h"
00017 image get_image_from_stream(CvCapture *cap);
00018 
00019 static char **demo_names;
00020 static image **demo_alphabet;
00021 static int demo_classes;
00022 
00023 static float **probs;
00024 static box *boxes;
00025 static network net;
00026 static image in   ;
00027 static image in_s ;
00028 static image det  ;
00029 static image det_s;
00030 static image disp = {0};
00031 static CvCapture * cap;
00032 static float fps = 0;
00033 static float demo_thresh = 0;
00034 
00035 static float *predictions[FRAMES];
00036 static int demo_index = 0;
00037 static image images[FRAMES];
00038 static float *avg;
00039 
00040 void *fetch_in_thread(void *ptr)
00041 {
00042     in = get_image_from_stream(cap);
00043     if(!in.data){
00044         error("Stream closed.");
00045     }
00046     in_s = resize_image(in, net.w, net.h);
00047     return 0;
00048 }
00049 
00050 void *detect_in_thread(void *ptr)
00051 {
00052     float nms = .4;
00053 
00054     layer l = net.layers[net.n-1];
00055     float *X = det_s.data;
00056     float *prediction = network_predict(net, X);
00057 
00058     memcpy(predictions[demo_index], prediction, l.outputs*sizeof(float));
00059     mean_arrays(predictions, FRAMES, l.outputs, avg);
00060     l.output = avg;
00061 
00062     free_image(det_s);
00063     if(l.type == DETECTION){
00064         get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
00065     } else if (l.type == REGION){
00066         get_region_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
00067     } else {
00068         error("Last layer must produce detections\n");
00069     }
00070     if (nms > 0) do_nms(boxes, probs, l.w*l.h*l.n, l.classes, nms);
00071     printf("\033[2J");
00072     printf("\033[1;1H");
00073     printf("\nFPS:%.1f\n",fps);
00074     printf("Objects:\n\n");
00075 
00076     images[demo_index] = det;
00077     det = images[(demo_index + FRAMES/2 + 1)%FRAMES];
00078     demo_index = (demo_index + 1)%FRAMES;
00079 
00080     draw_detections(det, l.w*l.h*l.n, demo_thresh, boxes, probs, demo_names, demo_alphabet, demo_classes);
00081 
00082     return 0;
00083 }
00084 
00085 double get_wall_time()
00086 {
00087     struct timeval time;
00088     if (gettimeofday(&time,NULL)){
00089         return 0;
00090     }
00091     return (double)time.tv_sec + (double)time.tv_usec * .000001;
00092 }
00093 
00094 void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix)
00095 {
00096     //skip = frame_skip;
00097     image **alphabet = load_alphabet();
00098     int delay = frame_skip;
00099     demo_names = names;
00100     demo_alphabet = alphabet;
00101     demo_classes = classes;
00102     demo_thresh = thresh;
00103     printf("Demo\n");
00104     net = parse_network_cfg(cfgfile);
00105     if(weightfile){
00106         load_weights(&net, weightfile);
00107     }
00108     set_batch_network(&net, 1);
00109 
00110     srand(2222222);
00111 
00112     if(filename){
00113         printf("video file: %s\n", filename);
00114         cap = cvCaptureFromFile(filename);
00115     }else{
00116         cap = cvCaptureFromCAM(cam_index);
00117     }
00118 
00119     if(!cap) error("Couldn't connect to webcam.\n");
00120 
00121     layer l = net.layers[net.n-1];
00122     int j;
00123 
00124     avg = (float *) calloc(l.outputs, sizeof(float));
00125     for(j = 0; j < FRAMES; ++j) predictions[j] = (float *) calloc(l.outputs, sizeof(float));
00126     for(j = 0; j < FRAMES; ++j) images[j] = make_image(1,1,3);
00127 
00128     boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box));
00129     probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *));
00130     for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float *)calloc(l.classes, sizeof(float *));
00131 
00132     pthread_t fetch_thread;
00133     pthread_t detect_thread;
00134 
00135     fetch_in_thread(0);
00136     det = in;
00137     det_s = in_s;
00138 
00139     fetch_in_thread(0);
00140     detect_in_thread(0);
00141     disp = det;
00142     det = in;
00143     det_s = in_s;
00144 
00145     for(j = 0; j < FRAMES/2; ++j){
00146         fetch_in_thread(0);
00147         detect_in_thread(0);
00148         disp = det;
00149         det = in;
00150         det_s = in_s;
00151     }
00152 
00153     int count = 0;
00154     if(!prefix){
00155         cvNamedWindow("Demo", CV_WINDOW_NORMAL); 
00156         cvMoveWindow("Demo", 0, 0);
00157         cvResizeWindow("Demo", 1352, 1013);
00158     }
00159 
00160     double before = get_wall_time();
00161 
00162     while(1){
00163         ++count;
00164         if(1){
00165             if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed");
00166             if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed");
00167 
00168             if(!prefix){
00169                 show_image(disp, "Demo");
00170                 int c = cvWaitKey(1);
00171                 if (c == 10){
00172                     if(frame_skip == 0) frame_skip = 60;
00173                     else if(frame_skip == 4) frame_skip = 0;
00174                     else if(frame_skip == 60) frame_skip = 4;   
00175                     else frame_skip = 0;
00176                 }
00177             }else{
00178                 char buff[256];
00179                 sprintf(buff, "%s_%08d", prefix, count);
00180                 save_image(disp, buff);
00181             }
00182 
00183             pthread_join(fetch_thread, 0);
00184             pthread_join(detect_thread, 0);
00185 
00186             if(delay == 0){
00187                 free_image(disp);
00188                 disp  = det;
00189             }
00190             det   = in;
00191             det_s = in_s;
00192         }else {
00193             fetch_in_thread(0);
00194             det   = in;
00195             det_s = in_s;
00196             detect_in_thread(0);
00197             if(delay == 0) {
00198                 free_image(disp);
00199                 disp = det;
00200             }
00201             show_image(disp, "Demo");
00202             cvWaitKey(1);
00203         }
00204         --delay;
00205         if(delay < 0){
00206             delay = frame_skip;
00207 
00208             double after = get_wall_time();
00209             float curr = 1./(after - before);
00210             fps = curr;
00211             before = after;
00212         }
00213     }
00214 }
00215 #else
00216 void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix)
00217 {
00218     fprintf(stderr, "Demo needs OpenCV for webcam images.\n");
00219 }
00220 #endif
00221 


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30