将预测保存到 json 或 txt 文件,并在 yolov3 对象检测中保存输出视频文件 - Google colab

Save prediction to json or txt file and also save output video file in yolov3 object detection - Google colab

https://github.com/AlexeyAB/darknet/ 上的对象检测正在运行,输出已保存到 .avi 文件中。我还想将预测保存到 jsontxt 文件中。

这是我的代码 运行:

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights -dont_show test_vid.mp4 -i 0 -out result.json -out_filename output.avi -ext_output -dont_show 

但只保存了视频输出。我还希望将预测保存到 jsontxt 文件中。我在这里做错了什么?还有其他方法吗?

我是计算机视觉的新手,需要一些帮助。谢谢

看这里demo的函数定义:

void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, const char *filename, char **names, int classes, int avgframes,
    int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int dontdraw_bbox, int json_port, int dont_show, int ext_output, int letter_box_in, int time_limit_sec, char *http_post_host,
    int benchmark, int benchmark_layers)

它没有名为 -out 的参数。

如果您想使用演示,使用现有代码,您有两个选择:

  1. 将结果保存到视频文件:-out_filename res.avi
  2. 使用您的软件或 Web 浏览器通过网络在线获取结果:-json_port 8070 -mjpeg_port 8090

现有代码 -out 仅提供 detector test。来自 this 函数定义:

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, int benchmark_layers)

要处理图像列表 data/train.txt 并将检测结果保存到 result.json 文件:

./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt

注意,这是为了对一组输入图像进行检测并将结果保存到json

检查 here 所有可能的命令以及标志和参数,它们的用法解释得很好。

如果您想 运行 检测输入视频并将预测保存为 json,您有两个选择:

  1. 使用 opencv 将视频转换为一组输入图像并使用以下命令:

./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt

  1. 更改代码以在演示中包含 -out 功能:

您需要在 demo.h, yolo.c, detector.c, demo.c - 1 and demo.c - 2:

的演示函数中包含此参数
 `char *outfile`

将以下代码片段添加到 demo.c:

FILE* json_file = NULL;
if (outfile) {
    json_file = fopen(outfile, "wb");
    if(!json_file) {
      error("fopen failed");
    }
    char *tmp = "[\n";
    fwrite(tmp, sizeof(char), strlen(tmp), json_file);
}

添加此代码段 here:

    if (json_file) {
        if (json_buf) {
            char *tmp = ", \n";
            fwrite(tmp, sizeof(char), strlen(tmp), json_file);
        }
        ++json_image_id;
        json_buf = detection_to_json(dets, nboxes, l.classes, names, json_image_id, input);

        fwrite(json_buf, sizeof(char), strlen(json_buf), json_file);
        free(json_buf);
    }

关闭 json 文件 here:

 if (json_file) {
        char *tmp = "\n]";
        fwrite(tmp, sizeof(char), strlen(tmp), json_file);
        fclose(json_file);
    }

如果您添加到您的命令 >result.txt,它将保存所有结果。它对我有用。我在colab上训练yolov4,demo函数的所有显示信息都保存到文件result.txt