【问题标题】:Save prediction to json or txt file and also save output video file in yolov3 object detection - Google colab将预测保存到 json 或 txt 文件,并将输出视频文件保存在 yolov3 对象检测中 - Google colab
【发布时间】:2020-11-20 00:29:58
【问题描述】:

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 文件中。我在这里做错了什么?还有其他方法吗?

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

【问题讨论】:

    标签: python opencv google-colaboratory yolo


    【解决方案1】:

    看这里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 &lt; data/train.txt

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

    您需要在demo.hyolo.cdetector.cdemo.c - 1demo.c - 2 的演示函数中包含此参数:

     `char *outfile`
    

    将以下代码sn-p添加到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);
    }
    

    添加这个sn-p 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);
        }
    

    【讨论】:

    • 您能告诉我应该在 demo.c 中的何处添加此代码吗?在演示函数里面? if (outfile) { json_file = fopen(outfile, "wb"); if(!json_file) { error("fopen failed"); } char *tmp = "[\n"; fwrite(tmp, sizeof(char), strlen(tmp), json_file); }
    • @SaranRaj 你可以添加它here
    • @VenkateshWadawadagi 我也做了同样的事情,但遇到了很多错误。错误,例如:“函数调用中的参数太少”、“标识符 'json_buf' 无法识别”等等。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多