darknet  v3
demo.c
Go to the documentation of this file.
1 #include "network.h"
2 #include "detection_layer.h"
3 #include "region_layer.h"
4 #include "cost_layer.h"
5 #include "utils.h"
6 #include "parser.h"
7 #include "box.h"
8 #include "image.h"
9 #include "demo.h"
10 #include <sys/time.h>
11 
12 #define DEMO 1
13 
14 #ifdef OPENCV
15 
16 static char **demo_names;
17 static image **demo_alphabet;
18 static int demo_classes;
19 
20 static network *net;
21 static image buff [3];
22 static image buff_letter[3];
23 static int buff_index = 0;
24 static CvCapture * cap;
25 static IplImage * ipl;
26 static float fps = 0;
27 static float demo_thresh = 0;
28 static float demo_hier = .5;
29 static int running = 0;
30 
31 static int demo_frame = 3;
32 static int demo_index = 0;
33 static float **predictions;
34 static float *avg;
35 static int demo_done = 0;
36 static int demo_total = 0;
37 double demo_time;
38 
39 detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num);
40 
41 int size_network(network *net)
42 {
43  int i;
44  int count = 0;
45  for(i = 0; i < net->n; ++i){
46  layer l = net->layers[i];
47  if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
48  count += l.outputs;
49  }
50  }
51  return count;
52 }
53 
54 void remember_network(network *net)
55 {
56  int i;
57  int count = 0;
58  for(i = 0; i < net->n; ++i){
59  layer l = net->layers[i];
60  if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
61  memcpy(predictions[demo_index] + count, net->layers[i].output, sizeof(float) * l.outputs);
62  count += l.outputs;
63  }
64  }
65 }
66 
67 detection *avg_predictions(network *net, int *nboxes)
68 {
69  int i, j;
70  int count = 0;
71  fill_cpu(demo_total, 0, avg, 1);
72  for(j = 0; j < demo_frame; ++j){
73  axpy_cpu(demo_total, 1./demo_frame, predictions[j], 1, avg, 1);
74  }
75  for(i = 0; i < net->n; ++i){
76  layer l = net->layers[i];
77  if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
78  memcpy(l.output, avg + count, sizeof(float) * l.outputs);
79  count += l.outputs;
80  }
81  }
82  detection *dets = get_network_boxes(net, buff[0].w, buff[0].h, demo_thresh, demo_hier, 0, 1, nboxes);
83  return dets;
84 }
85 
86 void *detect_in_thread(void *ptr)
87 {
88  running = 1;
89  float nms = .4;
90 
91  layer l = net->layers[net->n-1];
92  float *X = buff_letter[(buff_index+2)%3].data;
93  network_predict(net, X);
94 
95  /*
96  if(l.type == DETECTION){
97  get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
98  } else */
99  remember_network(net);
100  detection *dets = 0;
101  int nboxes = 0;
102  dets = avg_predictions(net, &nboxes);
103 
104 
105  /*
106  int i,j;
107  box zero = {0};
108  int classes = l.classes;
109  for(i = 0; i < demo_detections; ++i){
110  avg[i].objectness = 0;
111  avg[i].bbox = zero;
112  memset(avg[i].prob, 0, classes*sizeof(float));
113  for(j = 0; j < demo_frame; ++j){
114  axpy_cpu(classes, 1./demo_frame, dets[j][i].prob, 1, avg[i].prob, 1);
115  avg[i].objectness += dets[j][i].objectness * 1./demo_frame;
116  avg[i].bbox.x += dets[j][i].bbox.x * 1./demo_frame;
117  avg[i].bbox.y += dets[j][i].bbox.y * 1./demo_frame;
118  avg[i].bbox.w += dets[j][i].bbox.w * 1./demo_frame;
119  avg[i].bbox.h += dets[j][i].bbox.h * 1./demo_frame;
120  }
121  //copy_cpu(classes, dets[0][i].prob, 1, avg[i].prob, 1);
122  //avg[i].objectness = dets[0][i].objectness;
123  }
124  */
125 
126  if (nms > 0) do_nms_obj(dets, nboxes, l.classes, nms);
127 
128  printf("\033[2J");
129  printf("\033[1;1H");
130  printf("\nFPS:%.1f\n",fps);
131  printf("Objects:\n\n");
132  image display = buff[(buff_index+2) % 3];
133  draw_detections(display, dets, nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes);
134  free_detections(dets, nboxes);
135 
136  demo_index = (demo_index + 1)%demo_frame;
137  running = 0;
138  return 0;
139 }
140 
141 void *fetch_in_thread(void *ptr)
142 {
143  int status = fill_image_from_stream(cap, buff[buff_index]);
144  letterbox_image_into(buff[buff_index], net->w, net->h, buff_letter[buff_index]);
145  if(status == 0) demo_done = 1;
146  return 0;
147 }
148 
149 void *display_in_thread(void *ptr)
150 {
151  show_image_cv(buff[(buff_index + 1)%3], "Demo", ipl);
152  int c = cvWaitKey(1);
153  if (c != -1) c = c%256;
154  if (c == 27) {
155  demo_done = 1;
156  return 0;
157  } else if (c == 82) {
158  demo_thresh += .02;
159  } else if (c == 84) {
160  demo_thresh -= .02;
161  if(demo_thresh <= .02) demo_thresh = .02;
162  } else if (c == 83) {
163  demo_hier += .02;
164  } else if (c == 81) {
165  demo_hier -= .02;
166  if(demo_hier <= .0) demo_hier = .0;
167  }
168  return 0;
169 }
170 
171 void *display_loop(void *ptr)
172 {
173  while(1){
174  display_in_thread(0);
175  }
176 }
177 
178 void *detect_loop(void *ptr)
179 {
180  while(1){
181  detect_in_thread(0);
182  }
183 }
184 
185 void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen)
186 {
187  //demo_frame = avg_frames;
188  image **alphabet = load_alphabet();
189  demo_names = names;
190  demo_alphabet = alphabet;
191  demo_classes = classes;
192  demo_thresh = thresh;
193  demo_hier = hier;
194  printf("Demo\n");
195  net = load_network(cfgfile, weightfile, 0);
196  set_batch_network(net, 1);
197  pthread_t detect_thread;
198  pthread_t fetch_thread;
199 
200  srand(2222222);
201 
202  int i;
203  demo_total = size_network(net);
204  predictions = calloc(demo_frame, sizeof(float*));
205  for (i = 0; i < demo_frame; ++i){
206  predictions[i] = calloc(demo_total, sizeof(float));
207  }
208  avg = calloc(demo_total, sizeof(float));
209 
210  if(filename){
211  printf("video file: %s\n", filename);
212  cap = cvCaptureFromFile(filename);
213  }else{
214  cap = cvCaptureFromCAM(cam_index);
215 
216  if(w){
217  cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
218  }
219  if(h){
220  cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
221  }
222  if(frames){
223  cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames);
224  }
225  }
226 
227  if(!cap) error("Couldn't connect to webcam.\n");
228 
229  buff[0] = get_image_from_stream(cap);
230  buff[1] = copy_image(buff[0]);
231  buff[2] = copy_image(buff[0]);
232  buff_letter[0] = letterbox_image(buff[0], net->w, net->h);
233  buff_letter[1] = letterbox_image(buff[0], net->w, net->h);
234  buff_letter[2] = letterbox_image(buff[0], net->w, net->h);
235  ipl = cvCreateImage(cvSize(buff[0].w,buff[0].h), IPL_DEPTH_8U, buff[0].c);
236 
237  int count = 0;
238  if(!prefix){
239  cvNamedWindow("Demo", CV_WINDOW_NORMAL);
240  if(fullscreen){
241  cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
242  } else {
243  cvMoveWindow("Demo", 0, 0);
244  cvResizeWindow("Demo", 1352, 1013);
245  }
246  }
247 
248  demo_time = what_time_is_it_now();
249 
250  while(!demo_done){
251  buff_index = (buff_index + 1) %3;
252  if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed");
253  if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed");
254  if(!prefix){
255  fps = 1./(what_time_is_it_now() - demo_time);
256  demo_time = what_time_is_it_now();
257  display_in_thread(0);
258  }else{
259  char name[256];
260  sprintf(name, "%s_%08d", prefix, count);
261  save_image(buff[(buff_index + 1)%3], name);
262  }
263  pthread_join(fetch_thread, 0);
264  pthread_join(detect_thread, 0);
265  ++count;
266  }
267 }
268 
269 /*
270  void demo_compare(char *cfg1, char *weight1, char *cfg2, char *weight2, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen)
271  {
272  demo_frame = avg_frames;
273  predictions = calloc(demo_frame, sizeof(float*));
274  image **alphabet = load_alphabet();
275  demo_names = names;
276  demo_alphabet = alphabet;
277  demo_classes = classes;
278  demo_thresh = thresh;
279  demo_hier = hier;
280  printf("Demo\n");
281  net = load_network(cfg1, weight1, 0);
282  set_batch_network(net, 1);
283  pthread_t detect_thread;
284  pthread_t fetch_thread;
285 
286  srand(2222222);
287 
288  if(filename){
289  printf("video file: %s\n", filename);
290  cap = cvCaptureFromFile(filename);
291  }else{
292  cap = cvCaptureFromCAM(cam_index);
293 
294  if(w){
295  cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
296  }
297  if(h){
298  cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
299  }
300  if(frames){
301  cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames);
302  }
303  }
304 
305  if(!cap) error("Couldn't connect to webcam.\n");
306 
307  layer l = net->layers[net->n-1];
308  demo_detections = l.n*l.w*l.h;
309  int j;
310 
311  avg = (float *) calloc(l.outputs, sizeof(float));
312  for(j = 0; j < demo_frame; ++j) predictions[j] = (float *) calloc(l.outputs, sizeof(float));
313 
314  boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box));
315  probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *));
316  for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float *)calloc(l.classes+1, sizeof(float));
317 
318  buff[0] = get_image_from_stream(cap);
319  buff[1] = copy_image(buff[0]);
320  buff[2] = copy_image(buff[0]);
321  buff_letter[0] = letterbox_image(buff[0], net->w, net->h);
322  buff_letter[1] = letterbox_image(buff[0], net->w, net->h);
323  buff_letter[2] = letterbox_image(buff[0], net->w, net->h);
324  ipl = cvCreateImage(cvSize(buff[0].w,buff[0].h), IPL_DEPTH_8U, buff[0].c);
325 
326  int count = 0;
327  if(!prefix){
328  cvNamedWindow("Demo", CV_WINDOW_NORMAL);
329  if(fullscreen){
330  cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
331  } else {
332  cvMoveWindow("Demo", 0, 0);
333  cvResizeWindow("Demo", 1352, 1013);
334  }
335  }
336 
337  demo_time = what_time_is_it_now();
338 
339  while(!demo_done){
340 buff_index = (buff_index + 1) %3;
341 if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed");
342 if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed");
343 if(!prefix){
344  fps = 1./(what_time_is_it_now() - demo_time);
345  demo_time = what_time_is_it_now();
346  display_in_thread(0);
347 }else{
348  char name[256];
349  sprintf(name, "%s_%08d", prefix, count);
350  save_image(buff[(buff_index + 1)%3], name);
351 }
352 pthread_join(fetch_thread, 0);
353 pthread_join(detect_thread, 0);
354 ++count;
355 }
356 }
357 */
358 #else
359 void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg, float hier, int w, int h, int frames, int fullscreen)
360 {
361  fprintf(stderr, "Demo needs OpenCV for webcam images.\n");
362 }
363 #endif
364 
free_detections
Definition: darknet.py:73
image copy_image(image p)
Definition: image.c:519
Definition: darknet.h:87
void set_batch_network(network *net, int b)
Definition: network.c:339
Definition: darknet.h:512
void save_image(image p, const char *name)
Definition: image.c:717
network_predict
Definition: darknet.py:79
image ** load_alphabet()
Definition: image.c:223
letterbox_image
Definition: darknet.py:98
void fill_cpu(int N, float ALPHA, float *X, int INCX)
Definition: blas.c:190
layer * layers
Definition: darknet.h:441
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:178
float * output
Definition: darknet.h:246
void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg, float hier, int w, int h, int frames, int fullscreen)
Definition: demo.c:359
Definition: darknet.h:88
network * load_network(char *cfg, char *weights, int clear)
Definition: network.c:53
void letterbox_image_into(image im, int w, int h, image boxed)
Definition: image.c:948
int classes
Definition: darknet.h:172
LAYER_TYPE type
Definition: darknet.h:120
void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
Definition: image.c:239
int h
Definition: darknet.h:468
int n
Definition: darknet.h:435
get_network_boxes
Definition: darknet.py:65
int outputs
Definition: darknet.h:135
void error(const char *s)
Definition: utils.c:253
int w
Definition: darknet.h:468
list classes
Definition: voc_label.py:9
do_nms_obj
Definition: darknet.py:89
Definition: darknet.h:538
double what_time_is_it_now()
Definition: utils.c:27
Definition: darknet.h:119