diff --git a/README.md b/README.md index d9de132a..0a1ae522 100644 --- a/README.md +++ b/README.md @@ -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 `.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` diff --git a/src/coco.c b/src/coco.c index 1ccb3bc8..8ad13834 100644 --- a/src/coco.c +++ b/src/coco.c @@ -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); } diff --git a/src/demo.c b/src/demo.c index 7a258f3e..0e4d3d90 100644 --- a/src/demo.c +++ b/src/demo.c @@ -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"); } diff --git a/src/demo.h b/src/demo.h index 380b72fe..15f359dd 100644 --- a/src/demo.h +++ b/src/demo.h @@ -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 diff --git a/src/detector.c b/src/detector.c index 5171ee86..8f3023d5 100644 --- a/src/detector.c +++ b/src/detector.c @@ -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); diff --git a/src/yolo.c b/src/yolo.c index dfd497df..ef68acab 100644 --- a/src/yolo.c +++ b/src/yolo.c @@ -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); }