Saran Raj
Saran Raj

Reputation: 595

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 code I ran:

./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 

But only the video output is saved. I want the predictions also to be saved to json or txt file. What am I doing wrong here? Is there any other way to do it?

I'm new to computer vision and need some help with this. Thanks

Upvotes: 2

Views: 10464

Answers (2)

Ghofrane Ghanmi
Ghofrane Ghanmi

Reputation: 1

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

Upvotes: 0

Venkatesh Wadawadagi
Venkatesh Wadawadagi

Reputation: 2943

Look at the function definition of demo here:

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)

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:

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)

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);
    }

Upvotes: 5

Related Questions