You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
9.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifdef __linux__
  11. #include <sys/time.h>
  12. #else
  13. #include <time.h>
  14. #endif
  15. namespace DN{
  16. #define DEMO 1
  17. #undef OPENCV
  18. #ifdef OPENCV
  19. static char **demo_names;
  20. static image **demo_alphabet;
  21. static int demo_classes;
  22. static float **probs;
  23. static box *boxes;
  24. static network net;
  25. static network net2;
  26. static float **probs2;
  27. static box *boxes2;
  28. static float **predictions2;
  29. static image buff [3];
  30. static image buff_letter[3];
  31. static int buff_index = 0;
  32. static CvCapture * cap;
  33. static IplImage * ipl;
  34. static float fps = 0;
  35. static float demo_thresh = 0;
  36. static float demo_hier = .5;
  37. static int running = 0;
  38. static int demo_frame = 3;
  39. static int demo_detections = 0;
  40. static float **predictions;
  41. static int demo_index = 0;
  42. static int demo_done = 0;
  43. static float *avg;
  44. double demo_time;
  45. double get_wall_time()
  46. {
  47. struct timeval time;
  48. if (gettimeofday(&time,NULL)){
  49. return 0;
  50. }
  51. return (double)time.tv_sec + (double)time.tv_usec * .000001;
  52. }
  53. void *detect_in_thread(void *ptr)
  54. {
  55. running = 1;
  56. float nms = .4;
  57. layer l = net.layers[net.n-1];
  58. float *X = buff_letter[(buff_index+2)%3].data;
  59. float *prediction = network_predict(net, X);
  60. memcpy(predictions[demo_index], prediction, l.outputs*sizeof(float));
  61. mean_arrays(predictions, demo_frame, l.outputs, avg);
  62. l.output = avg;
  63. if(l.type == DETECTION){
  64. get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
  65. } else if (l.type == REGION){
  66. get_region_boxes(l, buff[0].w, buff[0].h, net.w, net.h, demo_thresh, probs, boxes, 0, 0, 0, demo_hier, 1);
  67. } else {
  68. error("Last layer must produce detections\n");
  69. }
  70. if (nms > 0) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
  71. printf("\033[2J");
  72. printf("\033[1;1H");
  73. printf("\nFPS:%.1f\n",fps);
  74. printf("Objects:\n\n");
  75. image display = buff[(buff_index+2) % 3];
  76. draw_detections(display, demo_detections, demo_thresh, boxes, probs, 0, demo_names, demo_alphabet, demo_classes);
  77. demo_index = (demo_index + 1)%demo_frame;
  78. running = 0;
  79. return 0;
  80. }
  81. void *fetch_in_thread(void *ptr)
  82. {
  83. int status = fill_image_from_stream(cap, buff[buff_index]);
  84. letterbox_image_into(buff[buff_index], net.w, net.h, buff_letter[buff_index]);
  85. if(status == 0) demo_done = 1;
  86. return 0;
  87. }
  88. void *display_in_thread(void *ptr)
  89. {
  90. show_image_cv(buff[(buff_index + 1)%3], "Demo", ipl);
  91. int c = cvWaitKey(1);
  92. if (c != -1) c = c%256;
  93. if (c == 27) {
  94. demo_done = 1;
  95. return 0;
  96. } else if (c == 82) {
  97. demo_thresh += .02;
  98. } else if (c == 84) {
  99. demo_thresh -= .02;
  100. if(demo_thresh <= .02) demo_thresh = .02;
  101. } else if (c == 83) {
  102. demo_hier += .02;
  103. } else if (c == 81) {
  104. demo_hier -= .02;
  105. if(demo_hier <= .0) demo_hier = .0;
  106. }
  107. return 0;
  108. }
  109. void *display_loop(void *ptr)
  110. {
  111. while(1){
  112. display_in_thread(0);
  113. }
  114. }
  115. void *detect_loop(void *ptr)
  116. {
  117. while(1){
  118. detect_in_thread(0);
  119. }
  120. }
  121. 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)
  122. {
  123. demo_frame = avg_frames;
  124. predictions = (float**) calloc(demo_frame, sizeof(float*));
  125. image **alphabet = load_alphabet();
  126. demo_names = names;
  127. demo_alphabet = alphabet;
  128. demo_classes = classes;
  129. demo_thresh = thresh;
  130. demo_hier = hier;
  131. printf("Demo\n");
  132. net = parse_network_cfg(cfgfile);
  133. if(weightfile){
  134. load_weights(&net, weightfile);
  135. }
  136. set_batch_network(&net, 1);
  137. pthread_t detect_thread;
  138. pthread_t fetch_thread;
  139. srand(2222222);
  140. if(filename){
  141. printf("video file: %s\n", filename);
  142. cap = cvCaptureFromFile(filename);
  143. }else{
  144. cap = cvCaptureFromCAM(cam_index);
  145. if(w){
  146. cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
  147. }
  148. if(h){
  149. cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
  150. }
  151. if(frames){
  152. cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames);
  153. }
  154. }
  155. if(!cap) error("Couldn't connect to webcam.\n");
  156. layer l = net.layers[net.n-1];
  157. demo_detections = l.n*l.w*l.h;
  158. int j;
  159. avg = (float *) calloc(l.outputs, sizeof(float));
  160. for(j = 0; j < demo_frame; ++j) predictions[j] = (float *) calloc(l.outputs, sizeof(float));
  161. boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box));
  162. probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *));
  163. for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float *)calloc(l.classes+1, sizeof(float));
  164. buff[0] = get_image_from_stream(cap);
  165. buff[1] = copy_image(buff[0]);
  166. buff[2] = copy_image(buff[0]);
  167. buff_letter[0] = letterbox_image(buff[0], net.w, net.h);
  168. buff_letter[1] = letterbox_image(buff[0], net.w, net.h);
  169. buff_letter[2] = letterbox_image(buff[0], net.w, net.h);
  170. ipl = cvCreateImage(cvSize(buff[0].w,buff[0].h), IPL_DEPTH_8U, buff[0].c);
  171. int count = 0;
  172. if(!prefix){
  173. cvNamedWindow("Demo", CV_WINDOW_NORMAL);
  174. if(fullscreen){
  175. cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
  176. } else {
  177. cvMoveWindow("Demo", 0, 0);
  178. cvResizeWindow("Demo", 1352, 1013);
  179. }
  180. }
  181. demo_time = get_wall_time();
  182. while(!demo_done){
  183. buff_index = (buff_index + 1) %3;
  184. if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed");
  185. if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed");
  186. if(!prefix){
  187. fps = 1./(get_wall_time() - demo_time);
  188. demo_time = get_wall_time();
  189. display_in_thread(0);
  190. }else{
  191. char name[256];
  192. sprintf(name, "%s_%08d", prefix, count);
  193. save_image(buff[(buff_index + 1)%3], name);
  194. }
  195. pthread_join(fetch_thread, 0);
  196. pthread_join(detect_thread, 0);
  197. ++count;
  198. }
  199. }
  200. 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)
  201. {
  202. demo_frame = avg_frames;
  203. predictions = calloc(demo_frame, sizeof(float*));
  204. image **alphabet = load_alphabet();
  205. demo_names = names;
  206. demo_alphabet = alphabet;
  207. demo_classes = classes;
  208. demo_thresh = thresh;
  209. demo_hier = hier;
  210. printf("Demo\n");
  211. net = load_network(cfg1, weight1, 0);
  212. set_batch_network(&net, 1);
  213. pthread_t detect_thread;
  214. pthread_t fetch_thread;
  215. srand(2222222);
  216. if(filename){
  217. printf("video file: %s\n", filename);
  218. cap = cvCaptureFromFile(filename);
  219. }else{
  220. cap = cvCaptureFromCAM(cam_index);
  221. if(w){
  222. cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
  223. }
  224. if(h){
  225. cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
  226. }
  227. if(frames){
  228. cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames);
  229. }
  230. }
  231. if(!cap) error("Couldn't connect to webcam.\n");
  232. layer l = net.layers[net.n-1];
  233. demo_detections = l.n*l.w*l.h;
  234. int j;
  235. avg = (float *) calloc(l.outputs, sizeof(float));
  236. for(j = 0; j < demo_frame; ++j) predictions[j] = (float *) calloc(l.outputs, sizeof(float));
  237. boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box));
  238. probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *));
  239. for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float *)calloc(l.classes+1, sizeof(float));
  240. buff[0] = get_image_from_stream(cap);
  241. buff[1] = copy_image(buff[0]);
  242. buff[2] = copy_image(buff[0]);
  243. buff_letter[0] = letterbox_image(buff[0], net.w, net.h);
  244. buff_letter[1] = letterbox_image(buff[0], net.w, net.h);
  245. buff_letter[2] = letterbox_image(buff[0], net.w, net.h);
  246. ipl = cvCreateImage(cvSize(buff[0].w,buff[0].h), IPL_DEPTH_8U, buff[0].c);
  247. int count = 0;
  248. if(!prefix){
  249. cvNamedWindow("Demo", CV_WINDOW_NORMAL);
  250. if(fullscreen){
  251. cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
  252. } else {
  253. cvMoveWindow("Demo", 0, 0);
  254. cvResizeWindow("Demo", 1352, 1013);
  255. }
  256. }
  257. demo_time = get_wall_time();
  258. while(!demo_done){
  259. buff_index = (buff_index + 1) %3;
  260. if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed");
  261. if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed");
  262. if(!prefix){
  263. fps = 1./(get_wall_time() - demo_time);
  264. demo_time = get_wall_time();
  265. display_in_thread(0);
  266. }else{
  267. char name[256];
  268. sprintf(name, "%s_%08d", prefix, count);
  269. save_image(buff[(buff_index + 1)%3], name);
  270. }
  271. pthread_join(fetch_thread, 0);
  272. pthread_join(detect_thread, 0);
  273. ++count;
  274. }
  275. }
  276. #else
  277. 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)
  278. {
  279. fprintf(stderr, "Demo needs OpenCV for webcam images.\n");
  280. }
  281. #endif
  282. };