diff --git a/build/darknet/x64/darknet_json_reslut.cmd b/build/darknet/x64/darknet_json_reslut.cmd new file mode 100644 index 00000000..3ec6db73 --- /dev/null +++ b/build/darknet/x64/darknet_json_reslut.cmd @@ -0,0 +1,3 @@ +darknet.exe detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -ext_output -dont_show -out result.json < data/train.txt + +pause \ No newline at end of file diff --git a/src/detector.c b/src/detector.c index 3ae5c0fa..2ae92ea7 100644 --- a/src/detector.c +++ b/src/detector.c @@ -1220,7 +1220,7 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int //#endif // OPENCV void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, - float hier_thresh, int dont_show, int ext_output, int save_labels) + float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile) { list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", "data/names.list"); @@ -1244,6 +1244,14 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam double time; char buff[256]; char *input = buff; + char *json_buf = NULL; + int json_image_id = 0; + FILE* json_file = NULL; + if (outfile) { + json_file = fopen(outfile, "wb"); + char *tmp = "[\n"; + fwrite(tmp, sizeof(char), strlen(tmp), json_file); + } int j; float nms = .45; // 0.4F while (1) { @@ -1256,7 +1264,7 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); - if (!input) return; + if (!input) break; strtok(input, "\n"); } image im = load_image(input, 0, 0, net.c); @@ -1287,6 +1295,18 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam show_image(im, "predictions"); } + if (outfile) { + if (json_buf) { + char *tmp = ", \n"; + fwrite(tmp, sizeof(char), strlen(tmp), json_file); + } + ++json_image_id; + json_buf = detection_to_json(dets, nboxes, l.classes, names, json_image_id, input); + + fwrite(json_buf, sizeof(char), strlen(json_buf), json_file); + free(json_buf); + } + // pseudo labeling concept - fast.ai if (save_labels) { @@ -1327,6 +1347,12 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam if (filename) break; } + if (outfile) { + char *tmp = "\n]"; + fwrite(tmp, sizeof(char), strlen(tmp), json_file); + fclose(json_file); + } + // free memory free_ptrs(names, net.layers[net.n - 1].classes); free_list_contents_kvp(options); @@ -1405,7 +1431,7 @@ void run_detector(int argc, char **argv) if (strlen(weights) > 0) if (weights[strlen(weights) - 1] == 0x0d) weights[strlen(weights) - 1] = 0; char *filename = (argc > 6) ? argv[6] : 0; - if (0 == strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, dont_show, ext_output, save_labels); + if (0 == strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, dont_show, ext_output, save_labels, outfile); else if (0 == strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear, dont_show, calc_map); else if (0 == strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile); else if (0 == strcmp(argv[2], "recall")) validate_detector_recall(datacfg, cfg, weights); diff --git a/src/http_stream.cpp b/src/http_stream.cpp index 1f6a7a4e..b4c88a32 100644 --- a/src/http_stream.cpp +++ b/src/http_stream.cpp @@ -274,8 +274,7 @@ public: void close_all() { close_all_sockets = 1; - char tmp[1]; - write(tmp); + write("\n]"); // close JSON array } bool open(int port) @@ -357,10 +356,26 @@ public: "Content-Type: application/json\r\n" //"Content-Type: multipart/x-mixed-replace; boundary=boundary\r\n" "\r\n", 0); + _write(client, "[\n", 0); // open JSON array cerr << "JSON_sender: new client " << client << endl; } else // existing client, just stream pix { + //char head[400]; + // application/x-resource+json or application/x-collection+json - when you are representing REST resources and collections + // application/json or text/json or text/javascript or text/plain. + // https://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type + //sprintf(head, "\r\nContent-Length: %zu\r\n\r\n", outlen); + //sprintf(head, "--boundary\r\nContent-Type: application/json\r\nContent-Length: %zu\r\n\r\n", outlen); + //_write(s, head, 0); + int n = _write(s, outputbuf, outlen); + if (n < outlen) + { + cerr << "JSON_sender: kill client " << s << endl; + ::shutdown(s, 2); + FD_CLR(s, &master); + } + // Graceful closes will first close their output channels and then wait for the peer // on the other side of the connection to close its output channels. When both sides are done telling // each other they won’t be sending any more data (i.e., closing output channels), @@ -372,24 +387,6 @@ public: free(buf); //::closesocket(s); printf("JSON_sender: close clinet: %d \n", result); - continue; - } - - //char head[400]; - // application/json - // application/x-resource+json or application/x-collection+json - when you are representing REST resources and collections - // text/json or text/javascript or text/plain. - // https://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type - //sprintf(head, "\r\nContent-Length: %zu\r\n\r\n", outlen); - //sprintf(head, "--boundary\r\nContent-Type: application/json\r\nContent-Length: %zu\r\n\r\n", outlen); - //_write(s, head, 0); - int n = _write(s, outputbuf, outlen); - //cerr << "known client " << s << " " << n << endl; - if (n < outlen) - { - cerr << "JSON_sender: kill client " << s << endl; - ::shutdown(s, 2); - FD_CLR(s, &master); } } } @@ -401,7 +398,9 @@ public: void send_json(detection *dets, int nboxes, int classes, char **names, long long int frame_id, int port, int timeout) { static JSON_sender js(port, timeout); - char *send_buf = detection_to_json(dets, nboxes, classes, names, frame_id, NULL); + static char *send_buf = NULL; + if(send_buf) js.write(", \n"); + send_buf = detection_to_json(dets, nboxes, classes, names, frame_id, NULL); js.write(send_buf); std::cout << " JSON-stream sent. \n"; diff --git a/src/network.c b/src/network.c index 74ad70ed..4eeed0a8 100644 --- a/src/network.c +++ b/src/network.c @@ -705,7 +705,8 @@ char *detection_to_json(detection *dets, int nboxes, int classes, char **names, } } } - strcat(send_buf, "\n ] \n}, \n"); + //strcat(send_buf, "\n ] \n}, \n"); + strcat(send_buf, "\n ] \n}"); return send_buf; }