From 44aee6fa9b04139c95f2f9a7cca1e28ae1f5a5ea Mon Sep 17 00:00:00 2001 From: szykk1993 Date: Wed, 21 Nov 2018 18:17:05 -0500 Subject: [PATCH] Expose conversion functions between image and OpenCV. Change types of input strings to const char*. --- include/darknet.h | 48 +++++++++++++++++++++++++++++--------------- src/data.c | 6 +++--- src/data.h | 2 +- src/demo.c | 4 ++-- src/image.c | 4 +++- src/image_opencv.cpp | 1 - src/matrix.c | 2 +- src/network.c | 2 +- src/option_list.c | 18 ++++++++--------- src/option_list.h | 12 +++++------ src/parser.c | 14 ++++++------- src/utils.c | 2 +- src/utils.h | 2 +- 13 files changed, 67 insertions(+), 50 deletions(-) diff --git a/include/darknet.h b/include/darknet.h index 4390c619..a09f8f47 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -17,6 +17,16 @@ #endif #endif +#ifdef __cplusplus +#ifdef OPENCV +#include +#endif +#else +#ifdef OPENCV +#include +#endif +#endif + #ifdef __cplusplus extern "C" { #endif @@ -29,7 +39,7 @@ typedef struct{ char **names; } metadata; -metadata get_metadata(char *file); +metadata get_metadata(const char *file); typedef struct{ int *leaf; @@ -584,7 +594,7 @@ typedef struct{ } box_label; -network *load_network(char *cfg, char *weights, int clear); +network *load_network(const char *cfg, const char *weights, int clear); load_args get_base_args(network *net); void free_data(data d); @@ -602,8 +612,8 @@ typedef struct list{ } list; pthread_t load_data(load_args args); -list *read_data_cfg(char *filename); -list *read_cfg(char *filename); +list *read_data_cfg(const char *filename); +list *read_cfg(const char *filename); unsigned char *read_file(char *filename); data resize_data(data orig, int w, int h); data *tile_data(data orig, int divs, int size); @@ -656,11 +666,11 @@ float train_network_sgd(network *net, data d, int n); void rgbgr_image(image im); data copy_data(data d); data concat_data(data d1, data d2); -data load_cifar10_data(char *filename); +data load_cifar10_data(const char *filename); float matrix_topk_accuracy(matrix truth, matrix guess, int k); void matrix_add_matrix(matrix from, matrix to); void scale_matrix(matrix m, float scale); -matrix csv_to_matrix(char *filename); +matrix csv_to_matrix(const char *filename); float *network_accuracies(network *net, data d, int n); float train_network_datum(network *net); image make_random_image(int w, int h, int c); @@ -672,18 +682,18 @@ void rescale_weights(layer l, float scale, float trans); void rgbgr_weights(layer l); image *get_weights(layer l); -void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix, int avg, float hier_thresh, int w, int h, int fps, int fullscreen); +void demo(const char *cfgfile, const char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix, int avg, float hier_thresh, int w, int h, int fps, int fullscreen); void get_detection_detections(layer l, int w, int h, float thresh, detection *dets); -char *option_find_str(list *l, char *key, char *def); -int option_find_int(list *l, char *key, int def); -int option_find_int_quiet(list *l, char *key, int def); +char *option_find_str(list *l, const char *key, const char *def); +int option_find_int(list *l, const char *key, int def); +int option_find_int_quiet(list *l, const char *key, int def); -network *parse_network_cfg(char *filename); -void save_weights(network *net, char *filename); -void load_weights(network *net, char *filename); -void save_weights_upto(network *net, char *filename, int cutoff); -void load_weights_upto(network *net, char *filename, int start, int cutoff); +network *parse_network_cfg(const char *filename); +void save_weights(network *net, const char *filename); +void load_weights(network *net, const char *filename); +void save_weights_upto(network *net, const char *filename, int cutoff); +void load_weights_upto(network *net, const char *filename, int start, int cutoff); void zero_objectness(layer l); void get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets); @@ -748,13 +758,19 @@ void free_detections(detection *dets, int n); void reset_network_state(network *net, int b); -char **get_labels(char *filename); +char **get_labels(const char *filename); void do_nms_obj(detection *dets, int total, int classes, float thresh); void do_nms_sort(detection *dets, int total, int classes, float thresh); matrix make_matrix(int rows, int cols); #ifdef OPENCV +IplImage *image_to_ipl(image im); +image ipl_to_image(IplImage* src); +#ifdef __cplusplus +cv::Mat image_to_mat(image im); +image mat_to_image(cv::Mat m); +#endif void *open_video_stream(const char *f, int c, int w, int h, int fps); image get_image_from_stream(void *p); void make_window(char *name, int w, int h, int fullscreen); diff --git a/src/data.c b/src/data.c index 59051b4e..43bd6ad3 100644 --- a/src/data.c +++ b/src/data.c @@ -654,7 +654,7 @@ matrix load_tags_paths(char **paths, int n, int k) return y; } -char **get_labels(char *filename) +char **get_labels(const char *filename) { list *plist = get_paths(filename); char **labels = (char **)list_to_array(plist); @@ -1402,7 +1402,7 @@ data concat_datas(data *d, int n) return out; } -data load_categorical_data_csv(char *filename, int target, int k) +data load_categorical_data_csv(const char *filename, int target, int k) { data d = {0}; d.shallow = 0; @@ -1419,7 +1419,7 @@ data load_categorical_data_csv(char *filename, int target, int k) return d; } -data load_cifar10_data(char *filename) +data load_cifar10_data(const char *filename) { data d = {0}; d.shallow = 0; diff --git a/src/data.h b/src/data.h index 781906f8..845e3476 100644 --- a/src/data.h +++ b/src/data.h @@ -38,7 +38,7 @@ data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int void get_random_batch(data d, int n, float *X, float *y); data get_data_part(data d, int part, int total); data get_random_data(data d, int num); -data load_categorical_data_csv(char *filename, int target, int k); +data load_categorical_data_csv(const char *filename, int target, int k); void normalize_data_rows(data d); void scale_data_rows(data d, float s); void translate_data_rows(data d, float s); diff --git a/src/demo.c b/src/demo.c index b89efb8d..34d1553e 100644 --- a/src/demo.c +++ b/src/demo.c @@ -184,7 +184,7 @@ void *detect_loop(void *ptr) } } -void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen) +void demo(const char *cfgfile, const char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen) { //demo_frame = avg_frames; image **alphabet = load_alphabet(); @@ -341,7 +341,7 @@ pthread_join(detect_thread, 0); } */ #else -void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg, float hier, int w, int h, int frames, int fullscreen) +void demo(const char *cfgfile, const char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg, float hier, int w, int h, int frames, int fullscreen) { fprintf(stderr, "Demo needs OpenCV for webcam images.\n"); } diff --git a/src/image.c b/src/image.c index 4a2c6baf..3b7db623 100644 --- a/src/image.c +++ b/src/image.c @@ -225,11 +225,13 @@ image **load_alphabet() int i, j; const int nsize = 8; image **alphabets = calloc(nsize, sizeof(image)); + char darknet_src_dir[] = __FILE__; + *strrchr(darknet_src_dir, '/') = '\0'; for(j = 0; j < nsize; ++j){ alphabets[j] = calloc(128, sizeof(image)); for(i = 32; i < 127; ++i){ char buff[256]; - sprintf(buff, "data/labels/%d_%d.png", i, j); + sprintf(buff, "%s/../data/labels/%d_%d.png", darknet_src_dir, i, j); alphabets[j][i] = load_image_color(buff, 0, 0); } } diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp index 7511280b..337e7134 100644 --- a/src/image_opencv.cpp +++ b/src/image_opencv.cpp @@ -2,7 +2,6 @@ #include "stdio.h" #include "stdlib.h" -#include "opencv2/opencv.hpp" #include "image.h" using namespace cv; diff --git a/src/matrix.c b/src/matrix.c index 799916bf..6dd3174f 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -130,7 +130,7 @@ float *pop_column(matrix *m, int c) return col; } -matrix csv_to_matrix(char *filename) +matrix csv_to_matrix(const char *filename) { FILE *fp = fopen(filename, "r"); if(!fp) file_error(filename); diff --git a/src/network.c b/src/network.c index aaab7997..9ea7c837 100644 --- a/src/network.c +++ b/src/network.c @@ -50,7 +50,7 @@ load_args get_base_args(network *net) return args; } -network *load_network(char *cfg, char *weights, int clear) +network *load_network(const char *cfg, const char *weights, int clear) { network *net = parse_network_cfg(cfg); if(weights && weights[0] != 0){ diff --git a/src/option_list.c b/src/option_list.c index 2f52781f..047126fc 100644 --- a/src/option_list.c +++ b/src/option_list.c @@ -4,7 +4,7 @@ #include "option_list.h" #include "utils.h" -list *read_data_cfg(char *filename) +list *read_data_cfg(const char *filename) { FILE *file = fopen(filename, "r"); if(file == 0) file_error(filename); @@ -32,7 +32,7 @@ list *read_data_cfg(char *filename) return options; } -metadata get_metadata(char *file) +metadata get_metadata(const char *file) { metadata m = {0}; list *options = read_data_cfg(file); @@ -67,7 +67,7 @@ int read_option(char *s, list *options) return 1; } -void option_insert(list *l, char *key, char *val) +void option_insert(list *l, const char *key, const char *val) { kvp *p = malloc(sizeof(kvp)); p->key = key; @@ -88,7 +88,7 @@ void option_unused(list *l) } } -char *option_find(list *l, char *key) +char *option_find(list *l, const char *key) { node *n = l->front; while(n){ @@ -101,7 +101,7 @@ char *option_find(list *l, char *key) } return 0; } -char *option_find_str(list *l, char *key, char *def) +char *option_find_str(list *l, const char *key, const char *def) { char *v = option_find(l, key); if(v) return v; @@ -109,7 +109,7 @@ char *option_find_str(list *l, char *key, char *def) return def; } -int option_find_int(list *l, char *key, int def) +int option_find_int(list *l, const char *key, int def) { char *v = option_find(l, key); if(v) return atoi(v); @@ -117,21 +117,21 @@ int option_find_int(list *l, char *key, int def) return def; } -int option_find_int_quiet(list *l, char *key, int def) +int option_find_int_quiet(list *l, const char *key, int def) { char *v = option_find(l, key); if(v) return atoi(v); return def; } -float option_find_float_quiet(list *l, char *key, float def) +float option_find_float_quiet(list *l, const char *key, float def) { char *v = option_find(l, key); if(v) return atof(v); return def; } -float option_find_float(list *l, char *key, float def) +float option_find_float(list *l, const char *key, float def) { char *v = option_find(l, key); if(v) return atof(v); diff --git a/src/option_list.h b/src/option_list.h index 844bd872..826d8ff5 100644 --- a/src/option_list.h +++ b/src/option_list.h @@ -3,17 +3,17 @@ #include "list.h" typedef struct{ - char *key; - char *val; + const char *key; + const char *val; int used; } kvp; int read_option(char *s, list *options); -void option_insert(list *l, char *key, char *val); -char *option_find(list *l, char *key); -float option_find_float(list *l, char *key, float def); -float option_find_float_quiet(list *l, char *key, float def); +void option_insert(list *l, const char *key, const char *val); +char *option_find(list *l, const char *key); +float option_find_float(list *l, const char *key, float def); +float option_find_float_quiet(list *l, const char *key, float def); void option_unused(list *l); #endif diff --git a/src/parser.c b/src/parser.c index c8141c9f..41ca07d2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -42,7 +42,7 @@ typedef struct{ list *options; }section; -list *read_cfg(char *filename); +list *read_cfg(const char *filename); LAYER_TYPE string_to_layer_type(char * type) { @@ -739,7 +739,7 @@ int is_network(section *s) || strcmp(s->type, "[network]")==0); } -network *parse_network_cfg(char *filename) +network *parse_network_cfg(const char *filename) { list *sections = read_cfg(filename); node *n = sections->front; @@ -888,7 +888,7 @@ network *parse_network_cfg(char *filename) return net; } -list *read_cfg(char *filename) +list *read_cfg(const char *filename) { FILE *file = fopen(filename, "r"); if(file == 0) file_error(filename); @@ -1004,7 +1004,7 @@ void save_connected_weights(layer l, FILE *fp) } } -void save_weights_upto(network *net, char *filename, int cutoff) +void save_weights_upto(network *net, const char *filename, int cutoff) { #ifdef GPU if(net->gpu_index >= 0){ @@ -1077,7 +1077,7 @@ void save_weights_upto(network *net, char *filename, int cutoff) } fclose(fp); } -void save_weights(network *net, char *filename) +void save_weights(network *net, const char *filename) { save_weights_upto(net, filename, net->n); } @@ -1215,7 +1215,7 @@ void load_convolutional_weights(layer l, FILE *fp) } -void load_weights_upto(network *net, char *filename, int start, int cutoff) +void load_weights_upto(network *net, const char *filename, int start, int cutoff) { #ifdef GPU if(net->gpu_index >= 0){ @@ -1305,7 +1305,7 @@ void load_weights_upto(network *net, char *filename, int start, int cutoff) fclose(fp); } -void load_weights(network *net, char *filename) +void load_weights(network *net, const char *filename) { load_weights_upto(net, filename, 0, net->n); } diff --git a/src/utils.c b/src/utils.c index 626b4678..82b7e600 100644 --- a/src/utils.c +++ b/src/utils.c @@ -278,7 +278,7 @@ void malloc_error() exit(-1); } -void file_error(char *s) +void file_error(const char *s) { fprintf(stderr, "Couldn't open file: %s\n", s); exit(0); diff --git a/src/utils.h b/src/utils.h index ef24da79..90e2a048 100644 --- a/src/utils.h +++ b/src/utils.h @@ -28,7 +28,7 @@ int read_all_fail(int fd, char *buffer, size_t bytes); int write_all_fail(int fd, char *buffer, size_t bytes); void find_replace(char *str, char *orig, char *rep, char *output); void malloc_error(); -void file_error(char *s); +void file_error(const char *s); void strip(char *s); void strip_char(char *s, char bad); list *split_str(char *s, char delim);