darknet之yolo测试一批图片并保存结果

  • 原则:修改src/detector.c, 尽量不删除源代码
  • 用法:将要测试图片的绝对路径存入txt,例如000.txt,然后执行./darknet detector test data/candle.data cfg/yolov3.cfg backup/yolov3_1000.weights 000.txt,结果保存在results目录下。此法可代替原版的两种方式。
  • 可使用ls ${PWD}/*jpg > 000.txt将当前目录下的jpg图片的绝对路径存入000.txt
  • 添加函数get_image_name
void get_image_name(char *filename, char *image, int l) {
    int j, i = l - 1, k = 8;
    while(i > 0) {
        if(filename[i] == '/') {
            j = i + 1;
            break;
        }
        --i;
    }
    while(j < l - 4) {
        image[k] = filename[j];
        ++k;
        ++j;
    }
}
  • 修改test_detector函数:
  • while (1)上面添加
char lwd[256];
size_t llen = 0;
strncpy(lwd, filename, 256);
if(lwd[strlen(lwd)-1] != 't') {
        printf("Need txt file of image path\n");
        return;
}
FILE* fl = fopen(lwd, "r");
  • while (1)刚开始添加
if(getline(&filename, &llen, fl) == -1) {
            fclose(fl);
            break;
}
filename[strlen(filename)-1]='\0';
  • save_image(im, "predictions");改为
char lwd_name[256] = {"results/"};
get_image_name(filename, lwd_name, strlen(filename));
save_image(im, lwd_name);
  • 注释以下几行
show_image(im, "predictions");
wait_until_press_key_cv();
destroy_all_windows_cv();
if (filename) break;
  • 如果要在图片上打印概率,则修改src/image.c函数draw_detections_v3的if (alphabet)部分
if (alphabet) {
                char labelstr[4096] = { 0 };
                int lwd_score = selected_detections[i].det.prob[selected_detections[i].best_class] * 100;
                char lwd_str[10];
                snprintf(lwd_str, sizeof(lwd_str), "%d", lwd_score);
                strcat(labelstr, names[selected_detections[i].best_class]);
                strcat(labelstr, lwd_str);
                int j;
                for (j = 0; j < classes; ++j) {
                    if (selected_detections[i].det.prob[j] > thresh && j != selected_detections[i].best_class) {
                        strcat(labelstr, ", ");
                        strcat(labelstr, names[j]);
                    }
                }
                image label = get_label_v3(alphabet, labelstr, (im.h*.03));
                draw_label(im, top + width, left, label, rgb);
                free_image(label);
            }

你可能感兴趣的:(deeplearning)