From 2d3bc5e3e6027d58a4ad27ceff1614d035b720ab Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 24 Jan 2021 20:26:05 +0530 Subject: [PATCH 01/13] rectified the bug in src/image_opencv.cpp --- src/image_opencv.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp index add27c96ef0..8b0798f4a2a 100644 --- a/src/image_opencv.cpp +++ b/src/image_opencv.cpp @@ -977,12 +977,13 @@ extern "C" void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, flo color.val[2] = blue * 256; // you should create directory: result_img + //IplImage* ipltypeimg = new IplImage(*show_image); //static int copied_frame_id = -1; //static IplImage* copy_img = NULL; //if (copied_frame_id != frame_id) { // copied_frame_id = frame_id; - // if(copy_img == NULL) copy_img = cvCreateImage(cvSize(show_img->width, show_img->height), show_img->depth, show_img->nChannels); - // cvCopy(show_img, copy_img, 0); + // if(copy_img == NULL) copy_img = cvCreateImage(cvSize(ipltypeimg->width, ipltypeimg->height), ipltypeimg->depth, ipltypeimg->nChannels); + // cvCopy(ipltypeimg, copy_img, 0); //} //static int img_id = 0; //img_id++; From 95ec9bd9249a7c79bf2fea06dee964365f2fb61b Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 24 Jan 2021 20:30:09 +0530 Subject: [PATCH 02/13] added relevant comment --- src/image_opencv.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp index 8b0798f4a2a..7d3d499c8d6 100644 --- a/src/image_opencv.cpp +++ b/src/image_opencv.cpp @@ -977,6 +977,7 @@ extern "C" void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, flo color.val[2] = blue * 256; // you should create directory: result_img + //converting cv::Mat type to Iplimage type //IplImage* ipltypeimg = new IplImage(*show_image); //static int copied_frame_id = -1; //static IplImage* copy_img = NULL; From a465e8ae4633f847fb48dfdd57f6c39185bc9ade Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 24 Jan 2021 20:45:26 +0530 Subject: [PATCH 03/13] updated the Readme.md based on the changes applied --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 220eeab054b..190ee053341 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To calculate anchors: `darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416` * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` +* To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/shubham-shahh/darknet/blob/95ec9bd9249a7c79bf2fea06dee964365f2fb61b/src/image_opencv.cpp#L980-L996) ##### For using network video-camera mjpeg-stream with any Android smartphone From e5a6346610905378e38f2a6638bb9f69dd355dad Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 24 Jan 2021 21:19:57 +0530 Subject: [PATCH 04/13] fixed a typo --- src/image_opencv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp index 7d3d499c8d6..406dd1b23ef 100644 --- a/src/image_opencv.cpp +++ b/src/image_opencv.cpp @@ -978,7 +978,7 @@ extern "C" void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, flo // you should create directory: result_img //converting cv::Mat type to Iplimage type - //IplImage* ipltypeimg = new IplImage(*show_image); + //IplImage* ipltypeimg = new IplImage(*show_img); //static int copied_frame_id = -1; //static IplImage* copy_img = NULL; //if (copied_frame_id != frame_id) { From 29eb9862c275ab69e8d48ba040322f9189772113 Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Mon, 25 Jan 2021 01:50:54 +0530 Subject: [PATCH 05/13] fixed the link in Readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 190ee053341..e2cfb31f6ae 100644 --- a/README.md +++ b/README.md @@ -288,7 +288,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To calculate anchors: `darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416` * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` -* To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/shubham-shahh/darknet/blob/95ec9bd9249a7c79bf2fea06dee964365f2fb61b/src/image_opencv.cpp#L980-L996) +* To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/AlexeyAB/darknet/blob/64efa721ede91cd8ccc18257f98eeba43b73a6af/src/image_opencv.cpp#L980-L994) ##### For using network video-camera mjpeg-stream with any Android smartphone From 43e1833781fe1be8df9230de7fe64d9de41cc268 Mon Sep 17 00:00:00 2001 From: Shubham Shah <51359628+shubham-shahh@users.noreply.github.com> Date: Sun, 24 Jan 2021 20:31:13 +0530 Subject: [PATCH 06/13] Update README.md committed the changes suggested Co-authored-by: Stefano Sinigardi --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e2cfb31f6ae..38f8e4650fa 100644 --- a/README.md +++ b/README.md @@ -288,7 +288,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To calculate anchors: `darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416` * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` -* To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/AlexeyAB/darknet/blob/64efa721ede91cd8ccc18257f98eeba43b73a6af/src/image_opencv.cpp#L980-L994) +* To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/AlexeyAB/darknet/blob/master/src/image_opencv.cpp#L980-L996) ##### For using network video-camera mjpeg-stream with any Android smartphone From 317a898d9d772b5343c3eb26805d6aa3960bfc1c Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 7 Feb 2021 18:39:26 +0530 Subject: [PATCH 07/13] added a feature to run inference on all the images in a folder (non-terminating) --- include/darknet.h | 2 +- src/darknet.c | 3 +- src/detector.c | 356 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 271 insertions(+), 90 deletions(-) diff --git a/include/darknet.h b/include/darknet.h index 040d9e35999..f668acc3d23 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -1040,7 +1040,7 @@ LIB_API float *network_predict_image_letterbox(network *net, image im); LIB_API float validate_detector_map(char *datacfg, char *cfgfile, char *weightfile, float thresh_calc_avg_iou, const float iou_thresh, const int map_points, int letter_box, network *existing_net); LIB_API void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int dont_show, int calc_map, int mjpeg_port, int show_imgs, int benchmark_layers, char* chart_path); LIB_API 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, char *outfile, int letter_box, int benchmark_layers); + float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile, int letter_box, int benchmark_layers, char *folder_inference); LIB_API int network_width(network *net); LIB_API int network_height(network *net); LIB_API void optimize_picture(network *net, image orig, int max_layer, float scale, float rate, float thresh, int norm); diff --git a/src/darknet.c b/src/darknet.c index 13ab75f3d38..087cc2ec83c 100644 --- a/src/darknet.c +++ b/src/darknet.c @@ -495,7 +495,8 @@ int main(int argc, char **argv) float thresh = find_float_arg(argc, argv, "-thresh", .24); int ext_output = find_arg(argc, argv, "-ext_output"); char *filename = (argc > 4) ? argv[4]: 0; - test_detector("cfg/coco.data", argv[2], argv[3], filename, thresh, 0.5, 0, ext_output, 0, NULL, 0, 0); + char *folder_inference = find_char_arg(argc, argv, "-folder_inference", 0); + test_detector("cfg/coco.data", argv[2], argv[3], filename, thresh, 0.5, 0, ext_output, 0, NULL, 0, 0, folder_inference); } else if (0 == strcmp(argv[1], "cifar")){ run_cifar(argc, argv); } else if (0 == strcmp(argv[1], "go")){ diff --git a/src/detector.c b/src/detector.c index a2fdf0bf3cd..64fc9c0e6a0 100644 --- a/src/detector.c +++ b/src/detector.c @@ -8,6 +8,7 @@ #include "box.h" #include "demo.h" #include "option_list.h" +#include #ifndef __COMPAR_FN_T #define __COMPAR_FN_T @@ -19,6 +20,15 @@ typedef __compar_fn_t comparison_fn_t; #include "http_stream.h" +void removeSubstrr (char *string, char *sub) { + char *match; + int len = strlen(sub); + while ((match = strstr(string, sub))) { + *match = '\0'; + strcat(string, match+len); + } +} + int check_mistakes = 0; static int coco_ids[] = { 1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90 }; @@ -1601,7 +1611,7 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int 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, char *outfile, int letter_box, int benchmark_layers) + float hier_thresh, int dont_show, int ext_output, int save_labels, char *outfile, int letter_box, int benchmark_layers, char *folder_inference) { list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", "data/names.list"); @@ -1638,110 +1648,276 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam } int j; float nms = .45; // 0.4F - while (1) { - if (filename) { - strncpy(input, filename, 256); - if (strlen(input) > 0) - if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0; - } - else { - printf("Enter Image Path: "); - fflush(stdout); - input = fgets(input, 256, stdin); - if (!input) break; - strtok(input, "\n"); - } - //image im; - //image sized = load_image_resize(input, net.w, net.h, net.c, &im); - image im = load_image(input, 0, 0, net.c); - image sized; - if(letter_box) sized = letterbox_image(im, net.w, net.h); - else sized = resize_image(im, net.w, net.h); - - layer l = net.layers[net.n - 1]; - int k; - for (k = 0; k < net.n; ++k) { - layer lk = net.layers[k]; - if (lk.type == YOLO || lk.type == GAUSSIAN_YOLO || lk.type == REGION) { - l = lk; - printf(" Detection layer: %d - type = %d \n", k, l.type); + if(folder_inference==NULL){ + while (1) { + if (filename) { + strncpy(input, filename, 256); + if (strlen(input) > 0) + if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0; + } + else { + printf("Enter Image Path: "); + fflush(stdout); + input = fgets(input, 256, stdin); + if (!input) break; + strtok(input, "\n"); + } + //image im; + //image sized = load_image_resize(input, net.w, net.h, net.c, &im); + image im = load_image(input, 0, 0, net.c); + image sized; + if(letter_box) sized = letterbox_image(im, net.w, net.h); + else sized = resize_image(im, net.w, net.h); + + layer l = net.layers[net.n - 1]; + int k; + for (k = 0; k < net.n; ++k) { + layer lk = net.layers[k]; + if (lk.type == YOLO || lk.type == GAUSSIAN_YOLO || lk.type == REGION) { + l = lk; + printf(" Detection layer: %d - type = %d \n", k, l.type); + } } - } - - //box *boxes = calloc(l.w*l.h*l.n, sizeof(box)); - //float **probs = calloc(l.w*l.h*l.n, sizeof(float*)); - //for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)xcalloc(l.classes, sizeof(float)); - float *X = sized.data; + //box *boxes = calloc(l.w*l.h*l.n, sizeof(box)); + //float **probs = calloc(l.w*l.h*l.n, sizeof(float*)); + //for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)xcalloc(l.classes, sizeof(float)); - //time= what_time_is_it_now(); - double time = get_time_point(); - network_predict(net, X); - //network_predict_image(&net, im); letterbox = 1; - printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000); - //printf("%s: Predicted in %f seconds.\n", input, (what_time_is_it_now()-time)); + float *X = sized.data; - int nboxes = 0; - detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box); - if (nms) { - if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms); - else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_nms); - } - draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output); - save_image(im, "predictions"); - if (!dont_show) { - show_image(im, "predictions"); - } + //time= what_time_is_it_now(); + double time = get_time_point(); + network_predict(net, X); + //network_predict_image(&net, im); letterbox = 1; + printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000); + //printf("%s: Predicted in %f seconds.\n", input, (what_time_is_it_now()-time)); - if (json_file) { - if (json_buf) { - char *tmp = ", \n"; - fwrite(tmp, sizeof(char), strlen(tmp), json_file); + int nboxes = 0; + detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box); + if (nms) { + if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms); + else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_nms); + } + draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output); + save_image(im, "predictions"); + if (!dont_show) { + show_image(im, "predictions"); } - ++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); - } + if (json_file) { + 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); - // pseudo labeling concept - fast.ai - if (save_labels) - { - char labelpath[4096]; - replace_image_to_label(input, labelpath); + fwrite(json_buf, sizeof(char), strlen(json_buf), json_file); + free(json_buf); + } - FILE* fw = fopen(labelpath, "wb"); - int i; - for (i = 0; i < nboxes; ++i) { - char buff[1024]; - int class_id = -1; - float prob = 0; - for (j = 0; j < l.classes; ++j) { - if (dets[i].prob[j] > thresh && dets[i].prob[j] > prob) { - prob = dets[i].prob[j]; - class_id = j; + // pseudo labeling concept - fast.ai + if (save_labels) + { + char labelpath[4096]; + replace_image_to_label(input, labelpath); + + FILE* fw = fopen(labelpath, "wb"); + int i; + for (i = 0; i < nboxes; ++i) { + char buff[1024]; + int class_id = -1; + float prob = 0; + for (j = 0; j < l.classes; ++j) { + if (dets[i].prob[j] > thresh && dets[i].prob[j] > prob) { + prob = dets[i].prob[j]; + class_id = j; + } + } + if (class_id >= 0) { + sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h); + fwrite(buff, sizeof(char), strlen(buff), fw); } } - if (class_id >= 0) { - sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h); - fwrite(buff, sizeof(char), strlen(buff), fw); - } + fclose(fw); + } + + free_detections(dets, nboxes); + free_image(im); + free_image(sized); + + if (!dont_show) { + wait_until_press_key_cv(); + destroy_all_windows_cv(); } - fclose(fw); + + if (filename) break; } - free_detections(dets, nboxes); - free_image(im); - free_image(sized); + if (json_file) { + char *tmp = "\n]"; + fwrite(tmp, sizeof(char), strlen(tmp), json_file); + fclose(json_file); + } - if (!dont_show) { - wait_until_press_key_cv(); - destroy_all_windows_cv(); + // free memory + free_ptrs((void**)names, net.layers[net.n - 1].classes); + free_list_contents_kvp(options); + free_list(options); + + int i; + const int nsize = 8; + for (j = 0; j < nsize; ++j) { + for (i = 32; i < 127; ++i) { + free_image(alphabet[j][i]); + } + free(alphabet[j]); } + free(alphabet); - if (filename) break; + free_network(net); } + else{ + DIR *folder; + struct dirent *entry; + int files = 0; + + while (1) { + folder = opendir(folder_inference); + printf("first while\n"); + //char str1[100] = "./result_img/"; + if(folder){ + while( (entry=readdir(folder)) != NULL){ + printf("second while\n"); + //closedir(folder); + //folder = opendir("./result_img/"); + printf("%s\n",entry->d_name); + //char str1[100] = "./result_img/"; + char str1[1024] = ""; + strncpy(str1,folder_inference,strlen(folder_inference)); + char cwd[100]=""; + strncpy(cwd,entry->d_name,strlen(entry->d_name)); + if(strcmp(cwd,".")==0 || strcmp(cwd,"..")==0){ + printf("issue %s\n",entry->d_name); + //sleep(0.5); + continue; + } + //if (filename) { + + strcat(str1, entry->d_name); + strncpy(input, str1, 256); + //closedir(folder); + if (strlen(input) > 0) + if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0; + //} + //else { + //printf("Enter Image Path: "); + //fflush(stdout); + //input = fgets(input, 256, stdin); + //if (!input) break; + //strtok(input, "\n"); + + //} + //image im; + //image sized = load_image_resize(input, net.w, net.h, net.c, &im); + image im = load_image(input, 0, 0, net.c); + image sized; + if(letter_box) sized = letterbox_image(im, net.w, net.h); + else sized = resize_image(im, net.w, net.h); + + layer l = net.layers[net.n - 1]; + int k; + for (k = 0; k < net.n; ++k) { + layer lk = net.layers[k]; + if (lk.type == YOLO || lk.type == GAUSSIAN_YOLO || lk.type == REGION) { + l = lk; + printf(" Detection layer: %d - type = %d \n", k, l.type); + } + } + + //box *boxes = calloc(l.w*l.h*l.n, sizeof(box)); + //float **probs = calloc(l.w*l.h*l.n, sizeof(float*)); + //for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)xcalloc(l.classes, sizeof(float)); + + float *X = sized.data; + + //time= what_time_is_it_now(); + double time = get_time_point(); + network_predict(net, X); + //network_predict_image(&net, im); letterbox = 1; + printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000); + //printf("%s: Predicted in %f seconds.\n", input, (what_time_is_it_now()-time)); + + int nboxes = 0; + detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box); + if (nms) { + if (l.nms_kind == DEFAULT_NMS) do_nms_sort(dets, nboxes, l.classes, nms); + else diounms_sort(dets, nboxes, l.classes, nms, l.nms_kind, l.beta_nms); + } + draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output); + save_image(im, "predictions"); + if (!dont_show) { + show_image(im, "predictions"); + } + + if (json_file) { + 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){ + char labelpath[4096]; + replace_image_to_label(input, labelpath); + + FILE* fw = fopen(labelpath, "wb"); + int i; + for (i = 0; i < nboxes; ++i) { + char buff[1024]; + int class_id = -1; + float prob = 0; + for (j = 0; j < l.classes; ++j) { + if (dets[i].prob[j] > thresh && dets[i].prob[j] > prob) { + prob = dets[i].prob[j]; + class_id = j; + } + } + if (class_id >= 0) { + sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h); + fwrite(buff, sizeof(char), strlen(buff), fw); + } + } + fclose(fw); + } + + + free_detections(dets, nboxes); + free_image(im); + free_image(sized); + + if (dont_show) { + wait_until_press_key_cv(); + destroy_all_windows_cv(); + } + + //if (filename) break; + //remove(input); + char newname[100]; + removeSubstrr(str1, folder_inference); + sprintf(newname, "./pfiles/%s",str1); + //printf("newname %s\n",newname); + rename (input, newname); + } + closedir(folder); + } + } if (json_file) { char *tmp = "\n]"; @@ -1765,6 +1941,9 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam free(alphabet); free_network(net); + + + } } #if defined(OPENCV) && defined(GPU) @@ -1962,6 +2141,7 @@ void run_detector(int argc, char **argv) char *out_filename = find_char_arg(argc, argv, "-out_filename", 0); char *outfile = find_char_arg(argc, argv, "-out", 0); char *prefix = find_char_arg(argc, argv, "-prefix", 0); + char *folder_inference = find_char_arg(argc, argv, "-folder_inference", 0); float thresh = find_float_arg(argc, argv, "-thresh", .25); // 0.24 float iou_thresh = find_float_arg(argc, argv, "-iou_thresh", .5); // 0.5 for mAP float hier_thresh = find_float_arg(argc, argv, "-hier", .5); @@ -2012,7 +2192,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, outfile, letter_box, benchmark_layers); + if (0 == strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, dont_show, ext_output, save_labels, outfile, letter_box, benchmark_layers, folder_inference); else if (0 == strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear, dont_show, calc_map, mjpeg_port, show_imgs, benchmark_layers, chart_path); 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); From ebc7b6f3d575402fbcd67cf214243d437f580fe1 Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 7 Feb 2021 18:59:10 +0530 Subject: [PATCH 08/13] updated the readme.md and removed unwanted code --- README.md | 1 + src/detector.c | 28 ++++------------------------ 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 38f8e4650fa..b5d7cdfa63c 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` * To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/AlexeyAB/darknet/blob/master/src/image_opencv.cpp#L980-L996) +* To run inference on all the images in a folder: `./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights -i 0 -thresh 0.25 --folder_inference ./result_img/` here `./result_img/` is the path of the folder in which the inference has to be done. the folder name can be anything, as long as it exists. After the inference the images will be moved to `./processed_files/`. To change the folder name or path, the changes cane be done here. ##### For using network video-camera mjpeg-stream with any Android smartphone diff --git a/src/detector.c b/src/detector.c index 64fc9c0e6a0..2129e1de44f 100644 --- a/src/detector.c +++ b/src/detector.c @@ -1785,41 +1785,25 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam while (1) { folder = opendir(folder_inference); printf("first while\n"); - //char str1[100] = "./result_img/"; if(folder){ while( (entry=readdir(folder)) != NULL){ - printf("second while\n"); - //closedir(folder); - //folder = opendir("./result_img/"); - printf("%s\n",entry->d_name); - //char str1[100] = "./result_img/"; char str1[1024] = ""; strncpy(str1,folder_inference,strlen(folder_inference)); char cwd[100]=""; strncpy(cwd,entry->d_name,strlen(entry->d_name)); if(strcmp(cwd,".")==0 || strcmp(cwd,"..")==0){ - printf("issue %s\n",entry->d_name); - //sleep(0.5); continue; } - //if (filename) { strcat(str1, entry->d_name); strncpy(input, str1, 256); //closedir(folder); if (strlen(input) > 0) if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0; - //} - //else { - //printf("Enter Image Path: "); - //fflush(stdout); - //input = fgets(input, 256, stdin); - //if (!input) break; - //strtok(input, "\n"); - //} - //image im; - //image sized = load_image_resize(input, net.w, net.h, net.c, &im); + + //image im; + //image sized = load_image_resize(input, net.w, net.h, net.c, &im); image im = load_image(input, 0, 0, net.c); image sized; if(letter_box) sized = letterbox_image(im, net.w, net.h); @@ -1906,13 +1890,9 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam wait_until_press_key_cv(); destroy_all_windows_cv(); } - - //if (filename) break; - //remove(input); char newname[100]; removeSubstrr(str1, folder_inference); - sprintf(newname, "./pfiles/%s",str1); - //printf("newname %s\n",newname); + sprintf(newname, "./processed_files/%s",str1); rename (input, newname); } closedir(folder); From 661010dc4fc3a0d0171cc3258e3ed9ff741a75df Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 7 Feb 2021 19:02:12 +0530 Subject: [PATCH 09/13] updated the readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5d7cdfa63c..f8e31a66df8 100644 --- a/README.md +++ b/README.md @@ -289,7 +289,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` * To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/AlexeyAB/darknet/blob/master/src/image_opencv.cpp#L980-L996) -* To run inference on all the images in a folder: `./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights -i 0 -thresh 0.25 --folder_inference ./result_img/` here `./result_img/` is the path of the folder in which the inference has to be done. the folder name can be anything, as long as it exists. After the inference the images will be moved to `./processed_files/`. To change the folder name or path, the changes cane be done here. +* To run inference on all the images in a folder: `./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights -i 0 -thresh 0.25 --folder_inference ./result_img/` here `./result_img/` is the path of the folder in which the inference has to be done. the folder name can be anything, as long as it exists. After the inference the images will be moved to `./processed_files/`. To change the folder name or path, the changes cane be done [`here`](https://github.com/shubham-shahh/darknet/blob/ebc7b6f3d575402fbcd67cf214243d437f580fe1/src/detector.c#L1895). ##### For using network video-camera mjpeg-stream with any Android smartphone From e1b46becc14373de8241b7cced13969d5f0f9dac Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 7 Feb 2021 19:11:34 +0530 Subject: [PATCH 10/13] removed unnecessary code --- src/detector.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/detector.c b/src/detector.c index 2129e1de44f..ad42f02cc9e 100644 --- a/src/detector.c +++ b/src/detector.c @@ -1784,7 +1784,6 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam while (1) { folder = opendir(folder_inference); - printf("first while\n"); if(folder){ while( (entry=readdir(folder)) != NULL){ char str1[1024] = ""; From cb6676457f2e1d8f0cd61e0176795ad154106445 Mon Sep 17 00:00:00 2001 From: Shubham Shah Date: Sun, 7 Feb 2021 19:48:41 +0530 Subject: [PATCH 11/13] updated readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8e31a66df8..4c5a1bf074b 100644 --- a/README.md +++ b/README.md @@ -289,7 +289,7 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` * To save the cropped image of the object detected: [`uncomment the following lines of code`](https://github.com/AlexeyAB/darknet/blob/master/src/image_opencv.cpp#L980-L996) -* To run inference on all the images in a folder: `./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights -i 0 -thresh 0.25 --folder_inference ./result_img/` here `./result_img/` is the path of the folder in which the inference has to be done. the folder name can be anything, as long as it exists. After the inference the images will be moved to `./processed_files/`. To change the folder name or path, the changes cane be done [`here`](https://github.com/shubham-shahh/darknet/blob/ebc7b6f3d575402fbcd67cf214243d437f580fe1/src/detector.c#L1895). +* To run inference on all the images in a folder: `./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights -i 0 -thresh 0.25 --folder_inference ./result_img/` here `./result_img/` is the path of the folder in which the inference has to be done. the folder name can be anything, as long as it exists. After the inference the images will be moved to `./processed_files/`. To change the folder name or path, the changes cane be done [`here`](https://github.com/AlexeyAB/darknet/blob/master/src/detector.c#L1894). ##### For using network video-camera mjpeg-stream with any Android smartphone From 6b703cba9102c86a1560aebb7c2e639cb23d1444 Mon Sep 17 00:00:00 2001 From: Stefano Sinigardi Date: Fri, 1 Sep 2023 14:11:27 +0200 Subject: [PATCH 12/13] restore building compatibility --- src/detector.c | 71 +++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/detector.c b/src/detector.c index 5a133f6dffb..32e5274fe24 100644 --- a/src/detector.c +++ b/src/detector.c @@ -8,7 +8,10 @@ #include "box.h" #include "demo.h" #include "option_list.h" + +#ifndef _WIN32 #include +#endif #ifndef __COMPAR_FN_T #define __COMPAR_FN_T @@ -21,12 +24,12 @@ typedef __compar_fn_t comparison_fn_t; #include "http_stream.h" void removeSubstrr (char *string, char *sub) { - char *match; - int len = strlen(sub); - while ((match = strstr(string, sub))) { - *match = '\0'; - strcat(string, match+len); - } + char *match; + int len = strlen(sub); + while ((match = strstr(string, sub))) { + *match = '\0'; + strcat(string, match+len); + } } int check_mistakes = 0; @@ -110,7 +113,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i } int save_after_iterations = option_find_int(options, "saveweights", (net.max_batches < 10000) ? 1000 : 10000 ); // configure when to write weights. Very useful for smaller datasets! - int save_last_weights_after = option_find_int(options, "savelast", 100); + int save_last_weights_after = option_find_int(options, "savelast", 100); printf("Weights are saved after: %d iterations. Last weights (*_last.weight) are stored every %d iterations. \n", save_after_iterations, save_last_weights_after ); @@ -1673,7 +1676,9 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam } int j; float nms = .45; // 0.4F +#ifndef _WIN32 if(folder_inference==NULL){ +#endif while (1) { if (filename) { strncpy(input, filename, 256); @@ -1801,6 +1806,7 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam free(alphabet); free_network(net); +#ifndef _WIN32 } else{ DIR *folder; @@ -1818,14 +1824,14 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam if(strcmp(cwd,".")==0 || strcmp(cwd,"..")==0){ continue; } - + strcat(str1, entry->d_name); strncpy(input, str1, 256); //closedir(folder); if (strlen(input) > 0) if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0; - - + + //image im; //image sized = load_image_resize(input, net.w, net.h, net.c, &im); image im = load_image(input, 0, 0, net.c); @@ -1901,10 +1907,10 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h); fwrite(buff, sizeof(char), strlen(buff), fw); } - } + } fclose(fw); } - + free_detections(dets, nboxes); free_image(im); @@ -1923,31 +1929,30 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam } } - if (json_file) { - char *tmp = "\n]"; - fwrite(tmp, sizeof(char), strlen(tmp), json_file); - fclose(json_file); - } - - // free memory - free_ptrs((void**)names, net.layers[net.n - 1].classes); - free_list_contents_kvp(options); - free_list(options); - - int i; - const int nsize = 8; - for (j = 0; j < nsize; ++j) { - for (i = 32; i < 127; ++i) { - free_image(alphabet[j][i]); + if (json_file) { + char *tmp = "\n]"; + fwrite(tmp, sizeof(char), strlen(tmp), json_file); + fclose(json_file); } - free(alphabet[j]); - } - free(alphabet); - free_network(net); + // free memory + free_ptrs((void**)names, net.layers[net.n - 1].classes); + free_list_contents_kvp(options); + free_list(options); + int i; + const int nsize = 8; + for (j = 0; j < nsize; ++j) { + for (i = 32; i < 127; ++i) { + free_image(alphabet[j][i]); + } + free(alphabet[j]); + } + free(alphabet); + free_network(net); } +#endif } #if defined(OPENCV) && defined(GPU) @@ -2197,7 +2202,7 @@ void run_detector(int argc, char **argv) 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, outfile, letter_box, benchmark_layers, folder_inference); - else if (0 == strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear, dont_show, calc_map, mjpeg_port, show_imgs, benchmark_layers, chart_path); + else if (0 == strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear, dont_show, calc_map, thresh, iou_thresh,mjpeg_port, show_imgs, benchmark_layers, chart_path); 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); else if (0 == strcmp(argv[2], "map")) validate_detector_map(datacfg, cfg, weights, thresh, iou_thresh, map_points, letter_box, NULL); From 4aad8949e73c4637e8b13f28eabc5684202d2bf4 Mon Sep 17 00:00:00 2001 From: Stefano Sinigardi Date: Sun, 1 Oct 2023 22:10:23 +0200 Subject: [PATCH 13/13] Restore build --- src/detector.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/detector.c b/src/detector.c index 1867a7c20e4..2e628f8791e 100644 --- a/src/detector.c +++ b/src/detector.c @@ -1800,6 +1800,7 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam #ifndef _WIN32 } else{ + DIR *folder; struct dirent *entry; int files = 0; @@ -1944,6 +1945,8 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam free_alphabet(alphabet); free_network(net); } +#endif +} #if defined(OPENCV) && defined(GPU)