diff --git a/README.md b/README.md index 52df11995a8..8fe2b12f99c 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window `darknet.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.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.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.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-lableing - 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.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -thresh 0.25 -dont_show -save_labels < data/new_train.txt` * To calculate anchors: `darknet.exe 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 c1535a35f19..c5b13c2d600 100644 --- a/src/coco.c +++ b/src/coco.c @@ -370,6 +370,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]); @@ -384,5 +385,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, frame_skip, - prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0); + prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0, json_file_output); } diff --git a/src/demo.c b/src/demo.c index 6c7f5d39848..863c00ad532 100644 --- a/src/demo.c +++ b/src/demo.c @@ -104,7 +104,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 frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in) + int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, char *json_file_output) { letter_box = letter_box_in; in_img = det_img = show_img = NULL; @@ -117,6 +117,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){ @@ -226,7 +235,17 @@ 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); } - draw_detections_cv_v3(show_img, local_dets, local_nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes, demo_ext_output); + 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); + } + + //draw_detections_cv_v3(show_img, local_dets, local_nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes, demo_ext_output); free_detections(local_dets, local_nboxes); printf("\nFPS:%.1f\n", fps); @@ -297,6 +316,12 @@ 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); + } + // free memory release_mat(&show_img); release_mat(&in_img); @@ -322,7 +347,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 frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in) + int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, char *json_file_output) { fprintf(stderr, "Demo needs OpenCV for webcam images.\n"); } diff --git a/src/demo.h b/src/demo.h index 1f749b899e5..cf9f1247929 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 frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in); + int frame_skip, char *prefix, char *out_filename, int mjpeg_port, int json_port, int dont_show, int ext_output, int letter_box_in, char *json_file_output); #ifdef __cplusplus } #endif diff --git a/src/detector.c b/src/detector.c index c9386914ced..2ee6d1bf718 100644 --- a/src/detector.c +++ b/src/detector.c @@ -1426,6 +1426,7 @@ void run_detector(int argc, char **argv) int mjpeg_port = find_int_arg(argc, argv, "-mjpeg_port", -1); int json_port = find_int_arg(argc, argv, "-json_port", -1); 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 @@ -1492,7 +1493,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, frame_skip, prefix, out_filename, - mjpeg_port, json_port, dont_show, ext_output, letter_box); + mjpeg_port, json_port, dont_show, ext_output, letter_box, json_file_output); free_list_contents_kvp(options); free_list(options); diff --git a/src/yolo.c b/src/yolo.c index 711470eade2..1bbd04068a1 100644 --- a/src/yolo.c +++ b/src/yolo.c @@ -338,6 +338,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; @@ -351,5 +352,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, frame_skip, - prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0); + prefix, out_filename, mjpeg_port, json_port, dont_show, ext_output, 0, json_file_output); }