Skip to content Skip to sidebar Skip to footer

Save Prediction To Json Or Txt File And Also Save Output Video File In Yolov3 Object Detection - Google Colab

Object detection on https://github.com/AlexeyAB/darknet/ is working and the output is saved to an .avi file. I also want to save the predictions to a json or txt file. This is the

Solution 1:

Look at the function definition of demo here:

voiddemo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int cam_index, constchar *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)

It does not have an argument called -out.

If demo is what you want to use, with the existing code you have two options:

  1. Save results to video file: -out_filename res.avi
  2. Get results online over the network by using your soft or Web-browser: -json_port 8070 -mjpeg_port 8090

With existing code -out is provided with detector test only. From this function definition:

voidtest_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)

To process a list of images data/train.txt and save results of detection to result.json file:

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

Note that, this is meant for doing detection on set of input images and save results to json.

Check here for all possible commands along with flags and arguments, their usage is explained well.

If you want to run detection on input video and save predictions as json, you have two options:

  1. Convert video to set of input images using opencv and use following command:

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

  1. Change the code to include -out functionality in demo:

You need to include this argument to demo function in demo.h, yolo.c, detector.c, demo.c - 1 and demo.c - 2:

 `char *outfile`

Add following code snippet to 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);
}

Add this snippet 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);
    }

Close json file here:

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

Solution 2:

If you add to your command >result.txt it will save all results in it. It worked for me. I was training yolov4 on colab and all display info of demo function were saved to the file result.txt

Post a Comment for "Save Prediction To Json Or Txt File And Also Save Output Video File In Yolov3 Object Detection - Google Colab"