Evo: add output to a json file option when running on a video (#3723)

This commit is contained in:
Thibault Durand 2023-09-21 01:46:17 -03:00 committed by GitHub
parent d30c13c38f
commit 4da0cd6d79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 6 deletions

View File

@ -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`

View File

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

View File

@ -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;
@ -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){
@ -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
@ -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);
@ -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");
}

View File

@ -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

View File

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

View File

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