Skip to content

Commit

Permalink
Evo: add output to a json file option when running on a video (#3723)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurand authored Sep 21, 2023
1 parent d30c13c commit 4da0cd6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ If you customize build with CMake GUI, darknet executable will be installed in y
`./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt`
- To process a list of images `data/train.txt` and save results of detection to `result.txt` use:
`./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output < data/train.txt > result.txt`
- To process a video and output results to a json file use: `darknet.exe detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights file.mp4 -dont_show -json_file_output results.json`
- Pseudo-labelling - to process a list of images `data/new_train.txt` and save results of detection in Yolo training format for each image as label `<image_name>.txt` (in this way you can increase the amount of training data) use:
`./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -thresh 0.25 -dont_show -save_labels < data/new_train.txt`
- To calculate anchors: `./darknet detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416`
Expand Down
3 changes: 2 additions & 1 deletion src/coco.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ void run_coco(int argc, char **argv)
int cam_index = find_int_arg(argc, argv, "-c", 0);
int frame_skip = find_int_arg(argc, argv, "-s", 0);
int ext_output = find_arg(argc, argv, "-ext_output");
char *json_file_output = find_char_arg(argc, argv, "-json_file_output", 0);

if(argc < 4){
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
Expand All @@ -412,5 +413,5 @@ void run_coco(int argc, char **argv)
else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights);
else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, coco_classes, 80, 1, frame_skip,
prefix, out_filename, mjpeg_port, 0, json_port, dont_show, ext_output, 0, 0, 0, 0, 0);
prefix, out_filename, mjpeg_port, 0, json_port, dont_show, ext_output, 0, 0, 0, 0, 0, json_file_output);
}
28 changes: 26 additions & 2 deletions src/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ double get_wall_time()

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)
int benchmark, int benchmark_layers, char *json_file_output)
{
if (avgframes < 1) avgframes = 1;
avg_frames = avgframes;
Expand All @@ -157,6 +157,15 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
demo_thresh = thresh;
demo_ext_output = ext_output;
demo_json_port = json_port;
char *json_buf = NULL;
FILE* json_file = NULL;

if (json_file_output) {
json_file = fopen(json_file_output, "wb");
char *tmp = "[\n";
fwrite(tmp, sizeof(char), strlen(tmp), json_file);
}

printf("Demo\n");
net = parse_network_cfg_custom(cfgfile, 1, 1); // set batch=1
if(weightfile){
Expand Down Expand Up @@ -290,6 +299,16 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
send_json(local_dets, local_nboxes, l.classes, demo_names, frame_id, demo_json_port, timeout);
}

if (json_file_output) {
if (json_buf) {
char *tmp = ", \n";
fwrite(tmp, sizeof(char), strlen(tmp), json_file);
}
json_buf = detection_to_json(local_dets, local_nboxes, l.classes, demo_names, frame_id, NULL);
fwrite(json_buf, sizeof(char), strlen(json_buf), json_file);
free(json_buf);
}

//char *http_post_server = "webhook.site/898bbd9b-0ddd-49cf-b81d-1f56be98d870";
if (http_post_host && !send_http_post_once) {
int timeout = 3; // 3 seconds
Expand Down Expand Up @@ -396,6 +415,11 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
printf("output_video_writer closed. \n");
}

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

custom_join(detect_thread, 0);
Expand All @@ -420,7 +444,7 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
#else
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)
int benchmark, int benchmark_layers, char *json_file_output)
{
fprintf(stderr, "Demo needs OpenCV for webcam images.\n");
}
Expand Down
2 changes: 1 addition & 1 deletion src/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern "C" {
#endif
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);
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, char *json_file_output);
#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,7 @@ void run_detector(int argc, char **argv)
char *http_post_host = find_char_arg(argc, argv, "-http_post_host", 0);
int time_limit_sec = find_int_arg(argc, argv, "-time_limit_sec", 0);
char *out_filename = find_char_arg(argc, argv, "-out_filename", 0);
char *json_file_output = find_char_arg(argc, argv, "-json_file_output", 0);
char *outfile = find_char_arg(argc, argv, "-out", 0);
char *prefix = find_char_arg(argc, argv, "-prefix", 0);
float thresh = find_float_arg(argc, argv, "-thresh", .25); // 0.24
Expand Down Expand Up @@ -2048,7 +2049,7 @@ void run_detector(int argc, char **argv)
if (strlen(filename) > 0)
if (filename[strlen(filename) - 1] == 0x0d) filename[strlen(filename) - 1] = 0;
demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, avgframes, frame_skip, prefix, out_filename,
mjpeg_port, dontdraw_bbox, json_port, dont_show, ext_output, letter_box, time_limit_sec, http_post_host, benchmark, benchmark_layers);
mjpeg_port, dontdraw_bbox, json_port, dont_show, ext_output, letter_box, time_limit_sec, http_post_host, benchmark, benchmark_layers, json_file_output);

free_list_contents_kvp(options);
free_list(options);
Expand Down
3 changes: 2 additions & 1 deletion src/yolo.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ void run_yolo(int argc, char **argv)
int cam_index = find_int_arg(argc, argv, "-c", 0);
int frame_skip = find_int_arg(argc, argv, "-s", 0);
int ext_output = find_arg(argc, argv, "-ext_output");
char *json_file_output = find_char_arg(argc, argv, "-json_file_output", 0);
if(argc < 4){
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
return;
Expand All @@ -366,5 +367,5 @@ void run_yolo(int argc, char **argv)
else if(0==strcmp(argv[2], "valid")) validate_yolo(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights);
else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, voc_names, 20, 1, frame_skip,
prefix, out_filename, mjpeg_port, 0, json_port, dont_show, ext_output, 0, 0, 0, 0, 0);
prefix, out_filename, mjpeg_port, 0, json_port, dont_show, ext_output, 0, 0, 0, 0, 0, json_file_output);
}

0 comments on commit 4da0cd6

Please sign in to comment.