darknet批量测试并保存图片

darknet源代码是AB大神版本的,将darknet下detector.c的test_detector函数整体替换,注意中间的更改保存输出图片的路径为自己路径;重新编译darknet。

控制台测试命令:darknet.exe  detector test data/img.data yolo-obj-test.cfg yolo-obj_best.weights data/valid.txt

注意将yolo-obj-test.cfg中的batch及subdivison设为1, data/valid.txt为待测试图片路径
 

  1 void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh,
  2     float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile, int letter_box)
  3 {
  4     list *options = read_data_cfg(datacfg);
  5     char *name_list = option_find_str(options, "names", "data/names.list");
  6     int names_size = 0;
  7     char **names = get_labels_custom(name_list, &names_size); //get_labels(name_list);
  8 
  9     image **alphabet = load_alphabet();
 10     network net = parse_network_cfg_custom(cfgfile, 1, 1); // set batch=1
 11     if (weightfile) {
 12         load_weights(&net, weightfile);
 13     }
 14     fuse_conv_batchnorm(net);
 15     calculate_binary_weights(net);
 16     if (net.layers[net.n - 1].classes != names_size) {
 17         printf(" Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n",
 18             name_list, names_size, net.layers[net.n - 1].classes, cfgfile);
 19         if (net.layers[net.n - 1].classes > names_size) getchar();
 20     }
 21     srand(2222222);
 22     double time;
 23     char buff[256];
 24     char *input = buff;
 25     char *json_buf = NULL;
 26     int json_image_id = 0;
 27     FILE* json_file = NULL;
 28     if (outfile) {
 29         json_file = fopen(outfile, "wb");
 30         char *tmp = "[\n";
 31         fwrite(tmp, sizeof(char), strlen(tmp), json_file);
 32     }
 33     int j, i;
 34     float nms = .45;    // 0.4F
 35     if (filename) {
 36         strncpy(input, filename, 256);
 37         list *plist = get_paths(input);
 38         char **paths = (char **)list_to_array(plist);
 39         printf("Start Testing!\n");
 40         int m = plist->size;
 41 
 42         for (i = 0; i < m; ++i) {
 43             char *path = paths[i];
 44             image im = load_image(path, 0, 0, net.c);
 45             int letterbox = 0;
 46             image sized = resize_image(im, net.w, net.h);
 47             //image sized = letterbox_image(im, net.w, net.h); letterbox = 1;
 48             layer l = net.layers[net.n - 1];
 49             float *X = sized.data;
 50             double time = get_time_point();
 51             network_predict(net, X);
 52             printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000);
 53             printf("Try Very Hard:");
 54             printf("%s: Predicted in %lf milli-seconds.\n", path, ((double)get_time_point() - time) / 1000);
 55             int nboxes = 0;
 56             detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);
 57             if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
 58             // draw_detections_v3(basecfg(input), im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);
 59             draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);
 60 
 61             char b[2048];
 62             sprintf(b, "data/output/%d", i);     //   data/output/ 改成保存文件夹的路径
 63             save_image(im, b);
 64             printf("save %s successfully!\n", b);
 65 
 66             if (save_labels)
 67             {
 68                 char labelpath[4096];
 69                 replace_image_to_label(input, labelpath);
 70                 FILE* fw = fopen(labelpath, "wb");
 71                 int i;
 72                 for (i = 0; i < nboxes; ++i) {
 73                     char buff[1024];
 74                     int class_id = -1;
 75                     float prob = 0;
 76                     for (j = 0; j < l.classes; ++j) {
 77                         if (dets[i].prob[j] > thresh && dets[i].prob[j] > prob) {
 78                             prob = dets[i].prob[j];
 79                             class_id = j;
 80                         }
 81                     }
 82                     if (class_id >= 0) {
 83                         sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h);
 84                         fwrite(buff, sizeof(char), strlen(buff), fw);
 85                     }
 86                 }
 87                 fclose(fw);
 88             }
 89 
 90             free_detections(dets, nboxes);
 91             free_image(im);
 92             free_image(sized);
 93         }
 94     }
 95     printf("All Done!\n");
 96     exit(0);
 97     free_ptrs(names, net.layers[net.n - 1].classes);
 98     free_list_contents_kvp(options);
 99     free_list(options);
100 
101     const int nsize = 8;
102     for (j = 0; j < nsize; ++j) {
103         for (i = 32; i < 127; ++i) {
104             free_image(alphabet[j][i]);
105         }
106         free(alphabet[j]);
107     }
108 
109     free(alphabet);
110     free_network(net);
111     printf("All Done!\n");
112 }

 

posted @ 2019-11-18 16:35  Parallax  阅读(2285)  评论(0编辑  收藏  举报