From 051d18a4770bd6c647e23f61aa95074c94e3c4d4 Mon Sep 17 00:00:00 2001 From: Chanhee Lee Date: Wed, 9 Oct 2024 11:34:30 -0700 Subject: [PATCH] Enable the build of libdarknet.so. Signed-off-by: Chanhee Lee --- Makefile | 6 ++- include/darknet.h | 14 ++++- src/detector.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 167d071585b..da2c01c0e6d 100644 --- a/Makefile +++ b/Makefile @@ -75,6 +75,7 @@ OS := $(shell uname) # ARCH= -gencode arch=compute_90,code=[sm_90,compute_90] VPATH=./src/ +SLIB=libdarknet.so EXEC=darknet OBJDIR=./obj/ @@ -176,7 +177,7 @@ endif OBJS = $(addprefix $(OBJDIR), $(OBJ)) DEPS = $(wildcard src/*.h) Makefile include/darknet.h -all: $(OBJDIR) backup results setchmod $(EXEC) $(LIBNAMESO) $(APPNAMESO) +all: $(OBJDIR) backup results setchmod $(EXEC) $(SLIB) $(LIBNAMESO) $(APPNAMESO) ifeq ($(LIBSO), 1) CFLAGS+= -fPIC @@ -191,6 +192,9 @@ endif $(EXEC): $(OBJS) $(CPP) -std=c++11 $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) +$(SLIB): $(OBJS) + $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) + $(OBJDIR)%.o: %.c $(DEPS) $(CC) $(COMMON) $(CFLAGS) -c $< -o $@ diff --git a/include/darknet.h b/include/darknet.h index 55ab50d5da8..fbb3016aa3a 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -1015,12 +1015,16 @@ typedef struct box_label { // ----------------------------------------------------- +void yolo_detection(); + // parser.c LIB_API network *load_network(char *cfg, char *weights, int clear); LIB_API network *load_network_custom(char *cfg, char *weights, int clear, int batch); LIB_API void free_network(network net); LIB_API void free_network_ptr(network* net); +LIB_API void load_weights(network *net, char *filename); + // network.c LIB_API load_args get_base_args(network *net); @@ -1073,6 +1077,13 @@ LIB_API void free_image(image m); LIB_API image crop_image(image im, int dx, int dy, int w, int h); LIB_API image resize_min(image im, int min); +LIB_API void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes); +LIB_API void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output); +LIB_API void show_image(image p, const char *name); +LIB_API void save_image(image p, const char *name); +LIB_API image **load_alphabet(); +LIB_API void free_alphabet(image **alphabet); + // layer.h LIB_API void free_layer_custom(layer l, int keep_cudnn_desc); LIB_API void free_layer(layer l); @@ -1094,13 +1105,14 @@ LIB_API void *cuda_get_context(); LIB_API void free_ptrs(void **ptrs, int n); LIB_API void top_k(float *a, int n, int k, int *index); +LIB_API void replace_image_to_label(const char* input_path, char* output_path); + // tree.h LIB_API tree *read_tree(char *filename); // option_list.h LIB_API metadata get_metadata(char *file); - // http_stream.h LIB_API void delete_json_sender(); LIB_API void send_json_custom(char const* send_buf, int port, int timeout); diff --git a/src/detector.c b/src/detector.c index 0fc36142904..119bb0c11d7 100644 --- a/src/detector.c +++ b/src/detector.c @@ -2046,3 +2046,134 @@ void run_detector(int argc, char **argv) if (gpus && gpu_list && ngpus > 1) free(gpus); } + +int detect_person(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) { + + printf("\n[[[ detect_object ]]]\n"); + + list *options = read_data_cfg(datacfg); + char *name_list = option_find_str(options, "names", "data/names.list"); + int names_size = 0; + printf("\n1111111\n"); + char **names = get_labels_custom(name_list, &names_size); //get_labels(name_list); + + printf("[BEFORE load_alphabet]"); + image **alphabet = load_alphabet(); + network net = parse_network_cfg_custom(cfgfile, 1, 1); // set batch=1 + if (weightfile) { + load_weights(&net, weightfile); + } + if (net.letter_box) letter_box = 1; + net.benchmark_layers = benchmark_layers; + fuse_conv_batchnorm(net); + calculate_binary_weights(net); + if (net.layers[net.n - 1].classes != names_size) { + printf("\n Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n", + name_list, names_size, net.layers[net.n - 1].classes, cfgfile); + } + srand(2222222); + char buff[256]; + char *input = buff; + char *json_buf = NULL; + int json_image_id = 0; + FILE* json_file = NULL; + int j; + float nms = .45; // 0.4F + printf("\n[BEFORE load_image]\n"); + image im = load_image(filename, 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); + } + } + + float *X = sized.data; + + double time = get_time_point(); + network_predict(net, X); + printf("%s: Predicted in %lf milli-seconds.\n", filename, ((double)get_time_point() - time) / 1000); + + 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, "output"); + + 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 (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); + free_alphabet(alphabet); + free_network(net); +} + +void yolo_detection() { + int ret = detect_person("/home/chani/Ubuntu/Advisory/YOLO/rust_darknet/darknet/cfg/coco.data", + "/home/chani/Ubuntu/Advisory/YOLO/rust_darknet/darknet/cfg/yolov7-tiny.cfg", + "/home/chani/Ubuntu/Advisory/YOLO/rust_darknet/darknet/cfg/yolov7-tiny.weights", + "/home/chani/Ubuntu/Advisory/YOLO/rust_darknet/darknet/data/person.jpg", 0.24, 0.5, 0, + 0, 0, NULL, 0, + 0); +}