Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ODCv2] Documentation - add output json file when running on a video to darknet #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<image_name>.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`
Expand Down
3 changes: 2 additions & 1 deletion src/coco.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
}
31 changes: 28 additions & 3 deletions src/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
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 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
Expand Down
3 changes: 2 additions & 1 deletion src/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/yolo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}