yolo遠嗎解析(3):視頻檢測流程
阿新 • • 發佈:2019-04-13
pre spa webcam led lov 技術分享 -h open ear ,darknet.c中根據detector參數,調用detector.c中的 run_detector(int argc, char **argv); 函數.
根據前文所說yolo代碼邏輯:
├── examples
│ ├── darknet.c(主程序)
│ │── xxx1.c
│ └── xxx2.c
│
├── include
│ ├── darknet.h
│
│
├── Makefile
│
│
└── src
├── yyy1.c
├── yyy2.h
└──......
視頻檢測入口時darknet.c文件。
輸入視頻檢測命令,如: ./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights ~/dataset/ca3.mp4
run_detector函數內容如下
void run_detector(int argc, char **argv) { char *prefix = find_char_arg(argc, argv, "-prefix", 0); float thresh = find_float_arg(argc, argv, "-thresh", .5); float hier_thresh = find_float_arg(argc, argv, "View Code-hier", .5); int cam_index = find_int_arg(argc, argv, "-c", 0); int frame_skip = find_int_arg(argc, argv, "-s", 0); int avg = find_int_arg(argc, argv, "-avg", 3); if(argc < 4){ fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);return; } char *gpu_list = find_char_arg(argc, argv, "-gpus", 0); char *outfile = find_char_arg(argc, argv, "-out", 0); int *gpus = 0; int gpu = 0; int ngpus = 0; if(gpu_list){ printf("%s\n", gpu_list); int len = strlen(gpu_list); ngpus = 1; int i; for(i = 0; i < len; ++i){ if (gpu_list[i] == ‘,‘) ++ngpus; } gpus = calloc(ngpus, sizeof(int)); for(i = 0; i < ngpus; ++i){ gpus[i] = atoi(gpu_list); gpu_list = strchr(gpu_list, ‘,‘)+1; } } else { gpu = gpu_index; gpus = &gpu; ngpus = 1; } int clear = find_arg(argc, argv, "-clear"); int fullscreen = find_arg(argc, argv, "-fullscreen"); int width = find_int_arg(argc, argv, "-w", 0); int height = find_int_arg(argc, argv, "-h", 0); int fps = find_int_arg(argc, argv, "-fps", 0); //int class = find_int_arg(argc, argv, "-class", 0);
代碼中定義了frame_skip等變量,默認值為0.然後繼續調用demo函數,demo函數位於/darknet/src/demo.c文件中,內容如下。
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) { //demo_frame = avg_frames; image **alphabet = load_alphabet(); demo_names = names; demo_alphabet = alphabet; demo_classes = classes; demo_thresh = thresh; demo_hier = hier; printf("Demo\n"); net = load_network(cfgfile, weightfile, 0); set_batch_network(net, 1); pthread_t detect_thread; pthread_t fetch_thread; srand(2222222); int i; demo_total = size_network(net); predictions = calloc(demo_frame, sizeof(float*)); for (i = 0; i < demo_frame; ++i){ predictions[i] = calloc(demo_total, sizeof(float)); } avg = calloc(demo_total, sizeof(float)); if(filename){ printf("video file: %s\n", filename); cap = open_video_stream(filename, 0, 0, 0, 0); }else{ cap = open_video_stream(0, cam_index, w, h, frames); } if(!cap) error("Couldn‘t connect to webcam.\n"); buff[0] = get_image_from_stream(cap); buff[1] = copy_image(buff[0]); buff[2] = copy_image(buff[0]); buff_letter[0] = letterbox_image(buff[0], net->w, net->h); buff_letter[1] = letterbox_image(buff[0], net->w, net->h); buff_letter[2] = letterbox_image(buff[0], net->w, net->h); int count = 0; if(!prefix){ make_window("Demo", 1352, 1013, fullscreen); } demo_time = what_time_is_it_now(); while(!demo_done){ buff_index = (buff_index + 1) %3; if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed"); if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed"); if(!prefix){ fps = 1./(what_time_is_it_now() - demo_time); demo_time = what_time_is_it_now(); display_in_thread(0); }else{ char name[256]; sprintf(name, "%s_%08d", prefix, count); save_image(buff[(buff_index + 1)%3], name); } pthread_join(fetch_thread, 0); pthread_join(detect_thread, 0); ++count; } }View Code
yolo遠嗎解析(3):視頻檢測流程