当前位置: 代码迷 >> 综合 >> Yolov4批量测试并保存图片
  详细解决方案

Yolov4批量测试并保存图片

热度:70   发布时间:2024-03-08 07:08:15.0

首先在源码文件夹中找到src/detector.c源文件 将detector.c的test_detector函数整体替换,然后重新编译darknet,生成新的darknet.exe文件。控制台测试命令:darknet.exe detector test data/obj.data cfg/yolo-obj.cfg backup/yolo-obj_final.weights data/test.txt
注意将yolo-obj.cfg配置文件中的batch及subdivison设为1, data/test.txt为测试图片路径,检测完成后的图片保存在你测试图片的文件夹下的最终结果目录中。

void test_detector(char* datacfg, char* cfgfile, char* weightfile, char* filename, float thresh,float hier_thresh, int dont_show, int ext_output, int save_labels, char* outfile, int letter_box)
{list* options = read_data_cfg(datacfg);char* name_list = option_find_str(options, "names", "data/names.list");int names_size = 0;char** names = get_labels_custom(name_list, &names_size); //get_labels(name_list);image** alphabet = load_alphabet();network net = parse_network_cfg_custom(cfgfile, 1, 1); // set batch=1if (weightfile) {load_weights(&net, weightfile);}fuse_conv_batchnorm(net);calculate_binary_weights(net);if (net.layers[net.n - 1].classes != names_size) {printf(" Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n",name_list, names_size, net.layers[net.n - 1].classes, cfgfile);if (net.layers[net.n - 1].classes > names_size) getchar();}srand(2222222);double time;char buff[256];char* input = buff;char* json_buf = NULL;int json_image_id = 0;FILE* json_file = NULL;if (outfile) {json_file = fopen(outfile, "wb");char* tmp = "[\n";fwrite(tmp, sizeof(char), strlen(tmp), json_file);}int j, i;float nms = .45;    // 0.4Fomp_set_num_threads(5);
#pragma omp parallel{if (filename) {strncpy(input, filename, 256);list* plist = get_paths(input);char** paths = (char**)list_to_array(plist);printf("Start Testing!\n");int m = plist->size;for (i = 0; i < m; ++i) {//向文件写入当前进度FILE* filewriter = fopen("cjprocess2.txt", "wb");char tmp[5];sprintf(tmp, "%d", i+1);fwrite(tmp, sizeof(char), strlen(tmp), filewriter);fclose(filewriter);//写入完毕char* path = paths[i];image im = load_image(path, 0, 0, net.c);int letterbox = 0;image sized = resize_image(im, net.w, net.h);//image sized = letterbox_image(im, net.w, net.h); letterbox = 1;layer l = net.layers[net.n - 1];float* X = sized.data;double time = get_time_point();network_predict(net, X);printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000);printf("Try Very Hard:");printf("%s: Predicted in %lf milli-seconds.\n", path, ((double)get_time_point() - time) / 1000);int nboxes = 0;detection* dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);if (nms) do_nms_sort(dets, nboxes, l.classes, nms);// draw_detections_v3(basecfg(input), im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);char b[2048];char filename[256], * p;strcpy(filename, (p = strrchr(path, '\\')) ? p + 1 : path);StrReplace(path, filename, "");left(filename,filename,strlen(filename)-4);// printf(filename);strcat(path, "最终结果");if (_access(path, 0) == -1){_mkdir(path);}char t[2048];strcpy(t,path);//printf(t);strcat(path, "\\%s");sprintf(b, path, filename);     //   改成保存文件夹的路径if (nboxes > 0) {save_image(im, b); }printf("save %s successfully!\n", b);if (save_labels && nboxes > 0){char labelpath[4096];strcat(t, "\\txt");if (_access(t, 0) == -1){_mkdir(t);}//replace_image_to_label(input, labelpath);strcat(t, "\\%s.txt");sprintf(labelpath, t, filename);FILE* fw = fopen(labelpath, "wb");int i;for (i = 0; i < nboxes; ++i) {char buff[1024];int class_id = -1;float prob = 0;for (j = 0; j < l.classes; ++j) {if (dets[i].prob[j] > thresh && dets[i].prob[j] > prob) {prob = dets[i].prob[j];class_id = j;}}if (class_id >= 0) {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);fwrite(buff, sizeof(char), strlen(buff), fw);}}fclose(fw);}

 

  相关解决方案