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

Enable the build of libdarknet.so. #8923

Closed
wants to merge 1 commit 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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down Expand Up @@ -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
Expand All @@ -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 $@

Expand Down
14 changes: 13 additions & 1 deletion include/darknet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
131 changes: 131 additions & 0 deletions src/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}