diff --git a/.circleci/config.yml b/.circleci/config.yml
index 8b7e27f3..dc004b3f 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -13,6 +13,8 @@ jobs:
- run: make clean
- run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=1 -j 8
- run: make clean
+ - run: make LIBSO=1 GPU=1 CUDNN=0 OPENCV=1 -j 8
+ - run: make clean
- run: make LIBSO=1 GPU=1 CUDNN=1 OPENCV=1 -j 8
- run: make clean
- run: make LIBSO=1 GPU=1 CUDNN=1 OPENCV=1 CUDNN_HALF=1 -j 8
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e4157464..323c1972 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -224,6 +224,7 @@ file(GLOB sources "${CMAKE_CURRENT_LIST_DIR}/src/*.c")
#add also .cpp files
list(APPEND sources
${CMAKE_CURRENT_LIST_DIR}/src/http_stream.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/src/image_opencv.cpp
)
#remove darknet.c file which is necessary only for the executable, not for the lib
list(REMOVE_ITEM sources
diff --git a/Makefile b/Makefile
index d184ca17..85e1a3e8 100644
--- a/Makefile
+++ b/Makefile
@@ -118,7 +118,7 @@ LDFLAGS+= -L/usr/local/zed/lib -lsl_core -lsl_input -lsl_zed
#-lstdc++ -D_GLIBCXX_USE_CXX11_ABI=0
endif
-OBJ=http_stream.o gemm.o utils.o dark_cuda.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o darknet.o detection_layer.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o detector.o layer.o compare.o classifier.o local_layer.o swag.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o rnn.o rnn_vid.o crnn_layer.o demo.o tag.o cifar.o go.o batchnorm_layer.o art.o region_layer.o reorg_layer.o reorg_old_layer.o super.o voxel.o tree.o yolo_layer.o upsample_layer.o lstm_layer.o
+OBJ=image_opencv.o http_stream.o gemm.o utils.o dark_cuda.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o darknet.o detection_layer.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o detector.o layer.o compare.o classifier.o local_layer.o swag.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o rnn.o rnn_vid.o crnn_layer.o demo.o tag.o cifar.o go.o batchnorm_layer.o art.o region_layer.o reorg_layer.o reorg_old_layer.o super.o voxel.o tree.o yolo_layer.o upsample_layer.o lstm_layer.o
ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o network_kernels.o avgpool_layer_kernels.o
diff --git a/build/darknet/darknet.vcxproj b/build/darknet/darknet.vcxproj
index 2f93f454..29d69fe3 100644
--- a/build/darknet/darknet.vcxproj
+++ b/build/darknet/darknet.vcxproj
@@ -205,6 +205,7 @@
+
@@ -264,6 +265,7 @@
+
diff --git a/build/darknet/darknet_no_gpu.vcxproj b/build/darknet/darknet_no_gpu.vcxproj
index e2fd6fa6..ec4cea99 100644
--- a/build/darknet/darknet_no_gpu.vcxproj
+++ b/build/darknet/darknet_no_gpu.vcxproj
@@ -210,6 +210,7 @@
+
@@ -269,6 +270,7 @@
+
diff --git a/build/darknet/yolo_cpp_dll.vcxproj b/build/darknet/yolo_cpp_dll.vcxproj
index 499d186c..f2c22b03 100644
--- a/build/darknet/yolo_cpp_dll.vcxproj
+++ b/build/darknet/yolo_cpp_dll.vcxproj
@@ -208,6 +208,7 @@
+
@@ -269,6 +270,7 @@
+
diff --git a/build/darknet/yolo_cpp_dll_no_gpu.vcxproj b/build/darknet/yolo_cpp_dll_no_gpu.vcxproj
index 3b3c70a4..5300e40b 100644
--- a/build/darknet/yolo_cpp_dll_no_gpu.vcxproj
+++ b/build/darknet/yolo_cpp_dll_no_gpu.vcxproj
@@ -194,6 +194,7 @@
+
@@ -255,6 +256,7 @@
+
diff --git a/include/yolo_v2_class.hpp b/include/yolo_v2_class.hpp
index bf3b28aa..fcaddcf0 100644
--- a/include/yolo_v2_class.hpp
+++ b/include/yolo_v2_class.hpp
@@ -48,6 +48,7 @@ struct bbox_t_container {
#include
#include
#include
+#include
#ifdef OPENCV
#include // C++
@@ -61,6 +62,7 @@ extern "C" LIB_API int detect_mat(const uint8_t* data, const size_t data_length,
extern "C" LIB_API int dispose();
extern "C" LIB_API int get_device_count();
extern "C" LIB_API int get_device_name(int gpu, char* deviceName);
+extern "C" LIB_API void send_json_custom(char const* send_buf, int port, int timeout);
class Detector {
std::shared_ptr detector_gpu_ptr;
@@ -86,8 +88,8 @@ public:
LIB_API void *get_cuda_context();
- LIB_API bool send_json_http(std::vector cur_bbox_vec, std::vector obj_names, int frame_id,
- std::string filename = std::string(), int timeout = 400000, int port = 8070);
+ //LIB_API bool send_json_http(std::vector cur_bbox_vec, std::vector obj_names, int frame_id,
+ // std::string filename = std::string(), int timeout = 400000, int port = 8070);
std::vector detect_resized(image_t img, int init_w, int init_h, float thresh = 0.2, bool use_mean = false)
{
@@ -178,6 +180,51 @@ private:
#endif // OPENCV
+public:
+
+ bool send_json_http(std::vector cur_bbox_vec, std::vector obj_names, int frame_id,
+ std::string filename = std::string(), int timeout = 400000, int port = 8070)
+ {
+ std::string send_str;
+
+ char *tmp_buf = (char *)calloc(1024, sizeof(char));
+ if (!filename.empty()) {
+ sprintf(tmp_buf, "{\n \"frame_id\":%d, \n \"filename\":\"%s\", \n \"objects\": [ \n", frame_id, filename.c_str());
+ }
+ else {
+ sprintf(tmp_buf, "{\n \"frame_id\":%d, \n \"objects\": [ \n", frame_id);
+ }
+ send_str = tmp_buf;
+ free(tmp_buf);
+
+ for (auto & i : cur_bbox_vec) {
+ char *buf = (char *)calloc(2048, sizeof(char));
+
+ sprintf(buf, " {\"class_id\":%d, \"name\":\"%s\", \"absolute_coordinates\":{\"center_x\":%d, \"center_y\":%d, \"width\":%d, \"height\":%d}, \"confidence\":%f",
+ i.obj_id, obj_names[i.obj_id].c_str(), i.x, i.y, i.w, i.h, i.prob);
+
+ //sprintf(buf, " {\"class_id\":%d, \"name\":\"%s\", \"relative_coordinates\":{\"center_x\":%f, \"center_y\":%f, \"width\":%f, \"height\":%f}, \"confidence\":%f",
+ // i.obj_id, obj_names[i.obj_id], i.x, i.y, i.w, i.h, i.prob);
+
+ send_str += buf;
+
+ if (!std::isnan(i.z_3d)) {
+ sprintf(buf, "\n , \"coordinates_in_meters\":{\"x_3d\":%.2f, \"y_3d\":%.2f, \"z_3d\":%.2f}",
+ i.x_3d, i.y_3d, i.z_3d);
+ send_str += buf;
+ }
+
+ send_str += "}\n";
+
+ free(buf);
+ }
+
+ //send_str += "\n ] \n}, \n";
+ send_str += "\n ] \n}";
+
+ send_json_custom(send_str.c_str(), port, timeout);
+ return true;
+ }
};
// --------------------------------------------------------------------------------
diff --git a/scripts/README.md b/scripts/README.md
index 36985f81..5debe7f9 100644
--- a/scripts/README.md
+++ b/scripts/README.md
@@ -10,6 +10,8 @@ ImageNet: http://imagenet.stanford.edu/download.php
ImageNet (ILSVRC2012): http://www.image-net.org/challenges/LSVRC/2012/nonpub-downloads
+ImageNet (ILSVRC2015): http://image-net.org/small/download.php
+
Open Images: https://storage.googleapis.com/openimages/web/download.html
Cityscapes: https://www.cityscapes-dataset.com/
diff --git a/src/art.c b/src/art.c
index e4471767..91fc94d6 100644
--- a/src/art.c
+++ b/src/art.c
@@ -12,16 +12,6 @@
#endif
-#ifdef OPENCV
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
-image get_image_from_stream(CvCapture *cap);
-#endif
-
-
void demo_art(char *cfgfile, char *weightfile, int cam_index)
{
#ifdef OPENCV
@@ -32,14 +22,13 @@ void demo_art(char *cfgfile, char *weightfile, int cam_index)
set_batch_network(&net, 1);
srand(2222222);
- CvCapture * cap;
+ cap_cv * cap;
- cap = cvCaptureFromCAM(cam_index);
+ cap = get_capture_webcam(cam_index);
char *window = "ArtJudgementBot9000!!!";
if(!cap) error("Couldn't connect to webcam.\n");
- cvNamedWindow(window, CV_WINDOW_NORMAL);
- cvResizeWindow(window, 512, 512);
+ create_window_cv(window, 0, 512, 512);
int i;
int idx[] = {37, 401, 434};
int n = sizeof(idx)/sizeof(idx[0]);
@@ -71,7 +60,7 @@ void demo_art(char *cfgfile, char *weightfile, int cam_index)
free_image(in_s);
free_image(in);
- cvWaitKey(1);
+ wait_key_cv(1);
}
#endif
}
diff --git a/src/cifar.c b/src/cifar.c
index 24a13a57..1b87cb5f 100644
--- a/src/cifar.c
+++ b/src/cifar.c
@@ -4,10 +4,6 @@
#include "option_list.h"
#include "blas.h"
-#ifdef OPENCV
-#include
-#endif
-
void train_cifar(char *cfgfile, char *weightfile)
{
srand(time(0));
diff --git a/src/classifier.c b/src/classifier.c
index 22471d61..ca196c66 100644
--- a/src/classifier.c
+++ b/src/classifier.c
@@ -13,21 +13,6 @@
#include
#endif
-#ifdef OPENCV
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
-#include "image.h"
-#include "http_stream.h"
-IplImage* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show);
-
-void draw_train_loss(IplImage* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
- float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port);
-
-#endif
-
float validate_classifier_single(char *datacfg, char *filename, char *weightfile, network *existing_net, int topk_custom);
float *get_regression_values(char **labels, int n)
@@ -111,7 +96,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
#ifdef OPENCV
//args.threads = 3;
- IplImage* img = NULL;
+ mat_cv* img = NULL;
float max_img_loss = 10;
int number_of_lines = 100;
int img_size = 1000;
@@ -211,8 +196,8 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
save_weights(net, buff);
#ifdef OPENCV
- cvReleaseImage(&img);
- cvDestroyAllWindows();
+ release_ipl(&img);
+ destroy_all_windows_cv();
#endif
free_network(net);
@@ -958,7 +943,7 @@ void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_i
list *options = read_data_cfg(datacfg);
srand(2222222);
- CvCapture * cap;
+ cap_cv * cap;
if (filename) {
//cap = cvCaptureFromFile(filename);
@@ -979,8 +964,7 @@ void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_i
int* indexes = (int*)calloc(top, sizeof(int));
if(!cap) error("Couldn't connect to webcam.\n");
- //cvNamedWindow("Threat", CV_WINDOW_NORMAL);
- //cvResizeWindow("Threat", 512, 512);
+ create_window_cv("Threat", 0, 512, 512);
float fps = 0;
int i;
@@ -1070,7 +1054,7 @@ void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_i
if(1){
show_image(out, "Threat");
- cvWaitKey(10);
+ wait_key_cv(10);
}
free_image(in_s);
free_image(in);
@@ -1086,7 +1070,7 @@ void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_i
void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
{
-#ifdef OPENCV
+#ifdef OPENCV_DISABLE
int bad_cats[] = {218, 539, 540, 1213, 1501, 1742, 1911, 2415, 4348, 19223, 368, 369, 370, 1133, 1200, 1306, 2122, 2301, 2537, 2823, 3179, 3596, 3639, 4489, 5107, 5140, 5289, 6240, 6631, 6762, 7048, 7171, 7969, 7984, 7989, 8824, 8927, 9915, 10270, 10448, 13401, 15205, 18358, 18894, 18895, 19249, 19697};
printf("Classifier Demo\n");
@@ -1184,7 +1168,7 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
calculate_binary_weights(net);
srand(2222222);
- CvCapture * cap;
+ cap_cv * cap;
if(filename){
//cap = cvCaptureFromFile(filename);
@@ -1204,8 +1188,7 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
int* indexes = (int*)calloc(top, sizeof(int));
if(!cap) error("Couldn't connect to webcam.\n");
- cvNamedWindow("Classifier", CV_WINDOW_NORMAL);
- cvResizeWindow("Classifier", 512, 512);
+ create_window_cv("Classifier", 0, 512, 512);
float fps = 0;
int i;
@@ -1236,7 +1219,7 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
free_image(in_s);
free_image(in);
- cvWaitKey(10);
+ wait_key_cv(10);// cvWaitKey(10);
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
diff --git a/src/coco.c b/src/coco.c
index e3cfccea..931c406c 100644
--- a/src/coco.c
+++ b/src/coco.c
@@ -8,10 +8,6 @@
#include "box.h"
#include "demo.h"
-#ifdef OPENCV
-#include
-#endif
-
char *coco_classes[] = {"person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"};
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};
@@ -354,10 +350,10 @@ void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
show_image(im, "predictions");
free_image(im);
free_image(sized);
-#ifdef OPENCV
- cvWaitKey(0);
- cvDestroyAllWindows();
-#endif
+
+ wait_until_press_key_cv();
+ destroy_all_windows_cv();
+
if (filename) break;
}
}
diff --git a/src/crnn_layer.c b/src/crnn_layer.c
index a4016dfb..c591276a 100644
--- a/src/crnn_layer.c
+++ b/src/crnn_layer.c
@@ -263,13 +263,13 @@ void forward_crnn_layer_gpu(layer l, network_state state)
s.train = state.train;
s.workspace = state.workspace;
s.net = state.net;
- //if(!state.train) s.index = state.index; // don't use TC at all (especially without cuda_convert_f32_to_f16() )
+ if(!state.train) s.index = state.index; // don't use TC for training (especially without cuda_convert_f32_to_f16() )
int i;
layer input_layer = *(l.input_layer);
layer self_layer = *(l.self_layer);
layer output_layer = *(l.output_layer);
-
+/*
#ifdef CUDNN_HALF // slow and bad for training
if (!state.train && state.net.cudnn_half) {
s.index = state.index;
@@ -278,7 +278,7 @@ void forward_crnn_layer_gpu(layer l, network_state state)
cuda_convert_f32_to_f16(output_layer.weights_gpu, output_layer.c*output_layer.n*output_layer.size*output_layer.size, output_layer.weights_gpu16);
}
#endif //CUDNN_HALF
-
+*/
if (state.train) {
fill_ongpu(l.outputs * l.batch * l.steps, 0, output_layer.delta_gpu, 1);
diff --git a/src/darknet.c b/src/darknet.c
index 67c7d9a4..1c5c08b6 100644
--- a/src/darknet.c
+++ b/src/darknet.c
@@ -12,9 +12,6 @@
#include "blas.h"
#include "connected_layer.h"
-#ifdef OPENCV
-#include
-#endif
extern void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top);
extern void run_voxel(int argc, char **argv);
@@ -426,7 +423,7 @@ void visualize(char *cfgfile, char *weightfile)
}
visualize_network(net);
#ifdef OPENCV
- cvWaitKey(0);
+ wait_until_press_key_cv();
#endif
}
diff --git a/src/data.c b/src/data.c
index a3612519..221a6211 100644
--- a/src/data.c
+++ b/src/data.c
@@ -766,13 +766,6 @@ static box float_to_box_stride(float *f, int stride)
}
#ifdef OPENCV
-#include
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#include
-#endif
#include "http_stream.h"
@@ -799,8 +792,8 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
const char *filename = random_paths[i];
int flag = (c >= 3);
- IplImage *src;
- if ((src = cvLoadImage(filename, flag)) == 0)
+ mat_cv *src;
+ if ((src = load_image_mat_cv(filename, flag)) == 0)
{
fprintf(stderr, "Cannot load image \"%s\"\n", filename);
char buff[256];
@@ -811,8 +804,8 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
//exit(0);
}
- int oh = src->height;
- int ow = src->width;
+ int oh = get_height_cv(src);
+ int ow = get_width_cv(src);
int dw = (ow*jitter);
int dh = (oh*jitter);
@@ -851,7 +844,8 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
fill_truth_detection(filename, boxes, d.y.vals[i], classes, flip, dx, dy, 1./sx, 1./sy, w, h);
- /*
+ const int show_augmented_images = 0;
+ if(show_augmented_images)
{
char buff[10];
sprintf(buff, "aug_%s_%d", random_paths[i], random_gen());
@@ -867,10 +861,11 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
}
show_image(ai, buff);
- cvWaitKey(0);
- }*/
+ wait_until_press_key_cv();
+ }
- cvReleaseImage(&src);
+
+ release_ipl(&src);
}
free(random_paths);
return d;
@@ -959,7 +954,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
}
show_image(sized, buff);
- cvWaitKey(0);
+ wait_until_press_key_cv();
}*/
free_image(orig);
diff --git a/src/demo.c b/src/demo.c
index 5950091b..61e2380f 100644
--- a/src/demo.c
+++ b/src/demo.c
@@ -16,14 +16,8 @@
#ifdef OPENCV
-#include
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
+
#include "http_stream.h"
-image get_image_from_stream(CvCapture *cap);
static char **demo_names;
static image **demo_alphabet;
@@ -35,7 +29,8 @@ static detection *dets = NULL;
static network net;
static image in_s ;
static image det_s;
-static CvCapture * cap;
+
+static cap_cv *cap;
static int cpp_video_capture = 0;
static float fps = 0;
static float demo_thresh = 0;
@@ -46,33 +41,24 @@ static int demo_json_port = -1;
static float* predictions[NFRAMES];
static int demo_index = 0;
static image images[NFRAMES];
-static IplImage* ipl_images[NFRAMES];
+static mat_cv* cv_images[NFRAMES];
static float *avg;
-void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output);
-void show_image_cv_ipl(IplImage *disp, const char *name);
-void save_cv_png(IplImage *img, const char *name);
-void save_cv_jpg(IplImage *img, const char *name);
-image get_image_from_stream_resize(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close);
-image get_image_from_stream_letterbox(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close);
-int get_stream_fps(CvCapture *cap, int cpp_video_capture);
-IplImage* in_img;
-IplImage* det_img;
-IplImage* show_img;
+mat_cv* in_img;
+mat_cv* det_img;
+mat_cv* show_img;
-static int flag_exit;
-static int letter_box = 0;
+static volatile int flag_exit;
+static volatile int letter_box = 0;
void *fetch_in_thread(void *ptr)
{
- //in = get_image_from_stream(cap);
int dont_close_stream = 0; // set 1 if your IP-camera periodically turns off and turns on video-stream
if(letter_box)
in_s = get_image_from_stream_letterbox(cap, net.w, net.h, net.c, &in_img, cpp_video_capture, dont_close_stream);
else
in_s = get_image_from_stream_resize(cap, net.w, net.h, net.c, &in_img, cpp_video_capture, dont_close_stream);
if(!in_s.data){
- //error("Stream closed.");
printf("Stream closed.\n");
flag_exit = 1;
//exit(EXIT_FAILURE);
@@ -95,12 +81,12 @@ void *detect_in_thread(void *ptr)
free_image(det_s);
- ipl_images[demo_index] = det_img;
- det_img = ipl_images[(demo_index + NFRAMES / 2 + 1) % NFRAMES];
+ cv_images[demo_index] = det_img;
+ det_img = cv_images[(demo_index + NFRAMES / 2 + 1) % NFRAMES];
demo_index = (demo_index + 1) % NFRAMES;
if (letter_box)
- dets = get_network_boxes(&net, in_img->width, in_img->height, demo_thresh, demo_thresh, 0, 1, &nboxes, 1); // letter box
+ dets = get_network_boxes(&net, get_width_cv(in_img), get_height_cv(in_img), demo_thresh, demo_thresh, 0, 1, &nboxes, 1); // letter box
else
dets = get_network_boxes(&net, net.w, net.h, demo_thresh, demo_thresh, 0, 1, &nboxes, 0); // resized
@@ -134,7 +120,6 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
if(weightfile){
load_weights(&net, weightfile);
}
- //set_batch_network(&net, 1);
fuse_conv_batchnorm(net);
calculate_binary_weights(net);
srand(2222222);
@@ -193,27 +178,26 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
int count = 0;
if(!prefix && !dont_show){
- cvNamedWindow("Demo", CV_WINDOW_NORMAL);
- cvMoveWindow("Demo", 0, 0);
- cvResizeWindow("Demo", 1352, 1013);
+ int full_screen = 0;
+ create_window_cv("Demo", full_screen, 1352, 1013);
}
- CvVideoWriter* output_video_writer = NULL; // cv::VideoWriter output_video;
+
+ write_cv* output_video_writer = NULL;
if (out_filename && !flag_exit)
{
- CvSize size;
- size.width = det_img->width, size.height = det_img->height;
int src_fps = 25;
src_fps = get_stream_fps(cap, cpp_video_capture);
+ output_video_writer =
+ create_video_writer(out_filename, 'D', 'I', 'V', 'X', src_fps, get_width_cv(det_img), get_height_cv(det_img), 1);
- //const char* output_name = "test_dnn_out.avi";
- //output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('H', '2', '6', '4'), src_fps, size, 1);
- output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('D', 'I', 'V', 'X'), src_fps, size, 1);
- //output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('M', 'J', 'P', 'G'), src_fps, size, 1);
- //output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('M', 'P', '4', 'V'), src_fps, size, 1);
- //output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('M', 'P', '4', '2'), src_fps, size, 1);
- //output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('X', 'V', 'I', 'D'), src_fps, size, 1);
- //output_video_writer = cvCreateVideoWriter(out_filename, CV_FOURCC('W', 'M', 'V', '2'), src_fps, size, 1);
+ //'H', '2', '6', '4'
+ //'D', 'I', 'V', 'X'
+ //'M', 'J', 'P', 'G'
+ //'M', 'P', '4', 'V'
+ //'M', 'P', '4', '2'
+ //'X', 'V', 'I', 'D'
+ //'W', 'M', 'V', '2'
}
double before = get_wall_time();
@@ -231,9 +215,9 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
//if (nms) do_nms_obj(local_dets, local_nboxes, l.classes, nms); // bad results
if (nms) do_nms_sort(local_dets, local_nboxes, l.classes, nms);
- printf("\033[2J");
- printf("\033[1;1H");
- printf("\nFPS:%.1f\n", fps);
+ //printf("\033[2J");
+ //printf("\033[1;1H");
+ //printf("\nFPS:%.1f\n", fps);
printf("Objects:\n\n");
++frame_id;
@@ -245,10 +229,12 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
draw_detections_cv_v3(show_img, local_dets, local_nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes, demo_ext_output);
free_detections(local_dets, local_nboxes);
+ printf("\nFPS:%.1f\n", fps);
+
if(!prefix){
if (!dont_show) {
show_image_cv_ipl(show_img, "Demo");
- int c = cvWaitKey(1);
+ int c = wait_key_cv(1);
if (c == 10) {
if (frame_skip == 0) frame_skip = 60;
else if (frame_skip == 4) frame_skip = 0;
@@ -276,11 +262,11 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
// save video file
if (output_video_writer && show_img) {
- cvWriteFrame(output_video_writer, show_img);
+ write_frame_cv(output_video_writer, show_img);
printf("\n cvWriteFrame \n");
}
- cvReleaseImage(&show_img);
+ release_ipl(&show_img);
pthread_join(fetch_thread, 0);
pthread_join(detect_thread, 0);
@@ -307,13 +293,13 @@ void demo(char *cfgfile, char *weightfile, float thresh, float hier_thresh, int
}
printf("input video stream closed. \n");
if (output_video_writer) {
- cvReleaseVideoWriter(&output_video_writer);
+ release_video_writer(&output_video_writer);
printf("output_video_writer closed. \n");
}
// free memory
- cvReleaseImage(&show_img);
- cvReleaseImage(&in_img);
+ release_ipl(&show_img);
+ release_ipl(&in_img);
free_image(in_s);
free(avg);
diff --git a/src/detector.c b/src/detector.c
index 46d19615..179498a8 100644
--- a/src/detector.c
+++ b/src/detector.c
@@ -16,37 +16,6 @@ typedef __compar_fn_t comparison_fn_t;
#endif
#endif
-#ifdef OPENCV
-#include
-#include
-//#include "opencv2/core/core.hpp"
-#include
-#include
-
-#ifndef CV_VERSION_EPOCH
-#include
-#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)"" CVAUX_STR(CV_VERSION_REVISION)
-#ifndef USE_CMAKE_LIBS
-#pragma comment(lib, "opencv_world" OPENCV_VERSION ".lib")
-#endif // USE_CMAKE_LIBS
-#else
-#define OPENCV_VERSION CVAUX_STR(CV_VERSION_EPOCH)"" CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)
-#ifndef USE_CMAKE_LIBS
-#pragma comment(lib, "opencv_core" OPENCV_VERSION ".lib")
-#pragma comment(lib, "opencv_imgproc" OPENCV_VERSION ".lib")
-#pragma comment(lib, "opencv_highgui" OPENCV_VERSION ".lib")
-#endif // USE_CMAKE_LIBS
-#endif
-IplImage* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show);
-
-void draw_train_loss(IplImage* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
- float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port);
-#endif // OPENCV
-
-#ifndef CV_RGB
-#define CV_RGB(r, g, b) cvScalar( (b), (g), (r), 0 )
-#endif // OPENCV
-
#include "http_stream.h"
int check_mistakes;
@@ -83,16 +52,6 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
for (k = 0; k < net_map.n; ++k) {
free_layer(net_map.layers[k]);
}
- /*
-#ifdef GPU
- cuda_free(net_map.workspace);
- cuda_free(net_map.input_state_gpu);
- if (*net_map.input16_gpu) cuda_free(*net_map.input16_gpu);
- if (*net_map.output16_gpu) cuda_free(*net_map.output16_gpu);
-#else
- free(net_map.workspace);
-#endif
- */
}
srand(time(0));
@@ -172,7 +131,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
#ifdef OPENCV
args.threads = 3 * ngpus; // Amazon EC2 Tesla V100: p3.2xlarge (8 logical cores) - p3.16xlarge
//args.threads = 12 * ngpus; // Ryzen 7 2700X (16 logical cores)
- IplImage* img = NULL;
+ mat_cv* img = NULL;
float max_img_loss = 5;
int number_of_lines = 100;
int img_size = 1000;
@@ -249,7 +208,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
float loss = 0;
#ifdef GPU
if (ngpus == 1) {
- loss = train_network(net, train);
+ loss = train_network_waitkey(net, train, 1);
}
else {
loss = train_networks(nets, ngpus, train, 4);
@@ -340,8 +299,8 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
save_weights(net, buff);
#ifdef OPENCV
- cvReleaseImage(&img);
- cvDestroyAllWindows();
+ release_ipl(&img);
+ destroy_all_windows_cv();
#endif
// free memory
@@ -1263,56 +1222,7 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int
if (show) {
#ifdef OPENCV
- CvMat* labels = cvCreateMat(number_of_boxes, 1, CV_32SC1);
- CvMat* points = cvCreateMat(number_of_boxes, 2, CV_32FC1);
- CvMat* centers = cvCreateMat(num_of_clusters, 2, CV_32FC1);
-
- for (i = 0; i < number_of_boxes; ++i) {
- points->data.fl[i * 2] = rel_width_height_array[i * 2];
- points->data.fl[i * 2 + 1] = rel_width_height_array[i * 2 + 1];
- //cvSet1D(points, i * 2, cvScalar(rel_width_height_array[i * 2], 0, 0, 0));
- //cvSet1D(points, i * 2 + 1, cvScalar(rel_width_height_array[i * 2 + 1], 0, 0, 0));
- }
-
- for (i = 0; i < num_of_clusters; ++i) {
- centers->data.fl[i * 2] = anchors_data.centers.vals[i][0];
- centers->data.fl[i * 2 + 1] = anchors_data.centers.vals[i][1];
- }
-
- for (i = 0; i < number_of_boxes; ++i) {
- labels->data.i[i] = anchors_data.assignments[i];
- }
-
- size_t img_size = 700;
- IplImage* img = cvCreateImage(cvSize(img_size, img_size), 8, 3);
- cvZero(img);
- for (j = 0; j < num_of_clusters; ++j) {
- CvPoint pt1, pt2;
- pt1.x = pt1.y = 0;
- pt2.x = centers->data.fl[j * 2] * img_size / width;
- pt2.y = centers->data.fl[j * 2 + 1] * img_size / height;
- cvRectangle(img, pt1, pt2, CV_RGB(255, 255, 255), 1, 8, 0);
- }
-
- for (i = 0; i < number_of_boxes; ++i) {
- CvPoint pt;
- pt.x = points->data.fl[i * 2] * img_size / width;
- pt.y = points->data.fl[i * 2 + 1] * img_size / height;
- int cluster_idx = labels->data.i[i];
- int red_id = (cluster_idx * (uint64_t)123 + 55) % 255;
- int green_id = (cluster_idx * (uint64_t)321 + 33) % 255;
- int blue_id = (cluster_idx * (uint64_t)11 + 99) % 255;
- cvCircle(img, pt, 1, CV_RGB(red_id, green_id, blue_id), CV_FILLED, 8, 0);
- //if(pt.x > img_size || pt.y > img_size) printf("\n pt.x = %d, pt.y = %d \n", pt.x, pt.y);
- }
- save_cv_png(img, "cloud.png");
- cvShowImage("clusters", img);
- cvWaitKey(0);
- cvReleaseImage(&img);
- cvDestroyAllWindows();
- cvReleaseMat(&labels);
- cvReleaseMat(&points);
- cvReleaseMat(¢ers);
+ show_acnhors(number_of_boxes, num_of_clusters, rel_width_height_array, anchors_data, width, height);
#endif // OPENCV
}
free(rel_width_height_array);
@@ -1334,7 +1244,6 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
if (weightfile) {
load_weights(&net, weightfile);
}
- //set_batch_network(&net, 1);
fuse_conv_batchnorm(net);
calculate_binary_weights(net);
if (net.layers[net.n - 1].classes != names_size) {
@@ -1439,14 +1348,12 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
free_detections(dets, nboxes);
free_image(im);
free_image(sized);
- //free(boxes);
- //free_ptrs((void **)probs, l.w*l.h*l.n);
-#ifdef OPENCV
+
if (!dont_show) {
- cvWaitKey(0);
- cvDestroyAllWindows();
+ wait_until_press_key_cv();
+ destroy_all_windows_cv();
}
-#endif
+
if (filename) break;
}
diff --git a/src/go.c b/src/go.c
index 79529432..5d507768 100644
--- a/src/go.c
+++ b/src/go.c
@@ -4,9 +4,6 @@
#include "option_list.h"
#include "blas.h"
-#ifdef OPENCV
-#include
-#endif
int inverted = 1;
int noi = 1;
diff --git a/src/http_stream.cpp b/src/http_stream.cpp
index 77096d2b..ec9d115d 100644
--- a/src/http_stream.cpp
+++ b/src/http_stream.cpp
@@ -489,7 +489,9 @@ public:
};
// ----------------------------------------
-void send_mjpeg(IplImage* ipl, int port, int timeout, int quality)
+struct mat_cv :IplImage { int a[0]; };
+
+void send_mjpeg(mat_cv* ipl, int port, int timeout, int quality)
{
try {
static MJPG_sender wri(port, timeout, quality);
@@ -503,170 +505,6 @@ void send_mjpeg(IplImage* ipl, int port, int timeout, int quality)
}
// ----------------------------------------
-CvCapture* get_capture_video_stream(const char *path) {
- CvCapture* cap = NULL;
- try {
- cap = (CvCapture*)new cv::VideoCapture(path);
- }
- catch (...) {
- cerr << " Error: video-stream " << path << " can't be opened! \n";
- }
- return cap;
-}
-// ----------------------------------------
-
-CvCapture* get_capture_webcam(int index) {
- CvCapture* cap = NULL;
- try {
- cap = (CvCapture*)new cv::VideoCapture(index);
- //((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_WIDTH, 1280);
- //((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
- }
- catch (...) {
- cerr << " Error: Web-camera " << index << " can't be opened! \n";
- }
- return cap;
-}
-// ----------------------------------------
-
-IplImage* get_webcam_frame(CvCapture *cap) {
- IplImage* src = NULL;
- try {
- cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
- cv::Mat frame;
- if (cpp_cap.isOpened())
- {
- cpp_cap >> frame;
- IplImage tmp = frame;
- src = cvCloneImage(&tmp);
- }
- else {
- std::cout << " Video-stream stopped! \n";
- }
- }
- catch (...) {
- std::cout << " Video-stream stoped! \n";
- }
- return src;
-}
-
-int get_stream_fps_cpp(CvCapture *cap) {
- int fps = 25;
- try {
- cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
-#ifndef CV_VERSION_EPOCH // OpenCV 3.x
- fps = cpp_cap.get(CAP_PROP_FPS);
-#else // OpenCV 2.x
- fps = cpp_cap.get(CV_CAP_PROP_FPS);
-#endif
- }
- catch (...) {
- cerr << " Can't get FPS of source videofile. For output video FPS = 25 by default. \n";
- }
- return fps;
-}
-// ----------------------------------------
-
-image image_data_augmentation(IplImage* ipl, int w, int h,
- int pleft, int ptop, int swidth, int sheight, int flip,
- float jitter, float dhue, float dsat, float dexp)
-{
- image out;
- try {
- cv::Mat img = cv::cvarrToMat(ipl);
-
- // crop
- cv::Rect src_rect(pleft, ptop, swidth, sheight);
- cv::Rect img_rect(cv::Point2i(0, 0), img.size());
- cv::Rect new_src_rect = src_rect & img_rect;
-
- cv::Rect dst_rect(cv::Point2i(std::max(0, -pleft), std::max(0, -ptop)), new_src_rect.size());
-
- cv::Mat cropped(cv::Size(src_rect.width, src_rect.height), img.type());
- cropped.setTo(cv::Scalar::all(0));
-
- img(new_src_rect).copyTo(cropped(dst_rect));
-
- // resize
- cv::Mat sized;
- cv::resize(cropped, sized, cv::Size(w, h), 0, 0, INTER_LINEAR);
-
- // flip
- if (flip) {
- cv::flip(sized, cropped, 1); // 0 - x-axis, 1 - y-axis, -1 - both axes (x & y)
- sized = cropped.clone();
- }
-
- // HSV augmentation
- // CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
- if (ipl->nChannels >= 3)
- {
- cv::Mat hsv_src;
- cvtColor(sized, hsv_src, CV_BGR2HSV); // also BGR -> RGB
-
- std::vector hsv;
- cv::split(hsv_src, hsv);
-
- hsv[1] *= dsat;
- hsv[2] *= dexp;
- hsv[0] += 179 * dhue;
-
- cv::merge(hsv, hsv_src);
-
- cvtColor(hsv_src, sized, CV_HSV2RGB); // now RGB instead of BGR
- }
- else
- {
- sized *= dexp;
- }
-
- //std::stringstream window_name;
- //window_name << "augmentation - " << ipl;
- //cv::imshow(window_name.str(), sized);
- //cv::waitKey(0);
-
- // Mat -> IplImage -> image
- IplImage src = sized;
- out = ipl_to_image(&src);
- }
- catch (...) {
- cerr << "OpenCV can't augment image: " << w << " x " << h << " \n";
- out = ipl_to_image(ipl);
- }
- return out;
-}
-
-
-image load_image_resize(char *filename, int w, int h, int c, image *im)
-{
- image out;
- cv::Mat img(h, w, CV_8UC3);
- try {
- int flag = -1;
- if (c == 0) flag = 1;
- else if (c == 1) { flag = 0; img = cv::Mat(h, w, CV_8UC1); }
- else if (c == 3) { flag = 1; img = cv::Mat(h, w, CV_8UC3); }
- else {
- cerr << "OpenCV can't force load with " << c << " channels\n";
- }
- //throw std::runtime_error("runtime_error");
- cv::Mat loaded_image = cv::imread(filename, flag);
- cv::cvtColor(loaded_image, loaded_image, cv::COLOR_RGB2BGR);
- IplImage tmp1 = loaded_image;
- *im = ipl_to_image(&tmp1);
-
- cv::resize(loaded_image, img, cv::Size(w, h), 0, 0, CV_INTER_LINEAR);
-
- IplImage tmp2 = img;
- out = ipl_to_image(&tmp2);
- }
- catch (...) {
- cerr << "OpenCV can't load image %s " << filename << " \n";
- out = make_image(w, h, c);
- *im = make_image(w, h, c);
- }
- return out;
-}
#endif // OPENCV
diff --git a/src/http_stream.h b/src/http_stream.h
index 1e9a3cf1..03b06510 100644
--- a/src/http_stream.h
+++ b/src/http_stream.h
@@ -2,34 +2,17 @@
#define HTTP_STREAM_H
#include "darknet.h"
-#ifdef OPENCV
-#include
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
#include "image.h"
#include
-#ifdef OPENCV
void send_json(detection *dets, int nboxes, int classes, char **names, long long int frame_id, int port, int timeout);
-void send_mjpeg(IplImage* ipl, int port, int timeout, int quality);
-CvCapture* get_capture_webcam(int index);
-CvCapture* get_capture_video_stream(const char *path);
-IplImage* get_webcam_frame(CvCapture *cap);
-int get_stream_fps_cpp(CvCapture *cap);
-image image_data_augmentation(IplImage* ipl, int w, int h,
- int pleft, int ptop, int swidth, int sheight, int flip,
- float jitter, float dhue, float dsat, float dexp);
+#ifdef OPENCV
+void send_mjpeg(mat_cv* ipl, int port, int timeout, int quality);
-image load_image_resize(char *filename, int w, int h, int c, image *im);
#endif // OPENCV
#ifdef __cplusplus
diff --git a/src/image.c b/src/image.c
index 72b4bdfb..2f085801 100644
--- a/src/image.c
+++ b/src/image.c
@@ -17,25 +17,8 @@
#include "stb_image_write.h"
#endif
-#ifdef OPENCV
-#include
-#include
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#include
-#include "http_stream.h"
-#endif
-#include "http_stream.h"
-
-#ifndef CV_RGB
-#define CV_RGB(r, g, b) cvScalar( (b), (g), (r), 0 )
-#endif
-#endif
-
extern int check_mistakes;
-int windows = 0;
+//int windows = 0;
float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
@@ -506,333 +489,6 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,
}
}
-#ifdef OPENCV
-
-void save_cv_png(IplImage *img, const char *name)
-{
- IplImage* img_rgb = cvCreateImage(cvSize(img->width, img->height), 8, 3);
- cvCvtColor(img, img_rgb, CV_RGB2BGR);
- stbi_write_png(name, img_rgb->width, img_rgb->height, 3, (char *)img_rgb->imageData, 0);
- cvRelease((void**)&img_rgb);
-}
-
-void save_cv_jpg(IplImage *img, const char *name)
-{
- IplImage* img_rgb = cvCreateImage(cvSize(img->width, img->height), 8, 3);
- cvCvtColor(img, img_rgb, CV_RGB2BGR);
- stbi_write_jpg(name, img_rgb->width, img_rgb->height, 3, (char *)img_rgb->imageData, 80);
- cvRelease((void**)&img_rgb);
-}
-
-void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output)
-{
- int i, j;
- if (!show_img) return;
- static int frame_id = 0;
- frame_id++;
-
- for (i = 0; i < num; ++i) {
- char labelstr[4096] = { 0 };
- int class_id = -1;
- for (j = 0; j < classes; ++j) {
- int show = strncmp(names[j], "dont_show", 9);
- if (dets[i].prob[j] > thresh && show) {
- if (class_id < 0) {
- strcat(labelstr, names[j]);
- class_id = j;
- }
- else {
- strcat(labelstr, ", ");
- strcat(labelstr, names[j]);
- }
- printf("%s: %.0f%% ", names[j], dets[i].prob[j] * 100);
- }
- }
- if (class_id >= 0) {
- int width = show_img->height * .006;
-
- //if(0){
- //width = pow(prob, 1./2.)*10+1;
- //alphabet = 0;
- //}
-
- //printf("%d %s: %.0f%%\n", i, names[class_id], prob*100);
- int offset = class_id * 123457 % classes;
- float red = get_color(2, offset, classes);
- float green = get_color(1, offset, classes);
- float blue = get_color(0, offset, classes);
- float rgb[3];
-
- //width = prob*20+2;
-
- rgb[0] = red;
- rgb[1] = green;
- rgb[2] = blue;
- box b = dets[i].bbox;
- b.w = (b.w < 1) ? b.w : 1;
- b.h = (b.h < 1) ? b.h : 1;
- b.x = (b.x < 1) ? b.x : 1;
- b.y = (b.y < 1) ? b.y : 1;
- //printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
-
- int left = (b.x - b.w / 2.)*show_img->width;
- int right = (b.x + b.w / 2.)*show_img->width;
- int top = (b.y - b.h / 2.)*show_img->height;
- int bot = (b.y + b.h / 2.)*show_img->height;
-
- if (left < 0) left = 0;
- if (right > show_img->width - 1) right = show_img->width - 1;
- if (top < 0) top = 0;
- if (bot > show_img->height - 1) bot = show_img->height - 1;
-
- //int b_x_center = (left + right) / 2;
- //int b_y_center = (top + bot) / 2;
- //int b_width = right - left;
- //int b_height = bot - top;
- //sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height);
-
- float const font_size = show_img->height / 1000.F;
- CvPoint pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
- pt1.x = left;
- pt1.y = top;
- pt2.x = right;
- pt2.y = bot;
- pt_text.x = left;
- pt_text.y = top - 12;
- pt_text_bg1.x = left;
- pt_text_bg1.y = top - (10 + 25 * font_size);
- pt_text_bg2.x = right;
- pt_text_bg2.y = top;
- CvScalar color;
- color.val[0] = red * 256;
- color.val[1] = green * 256;
- color.val[2] = blue * 256;
-
- // you should create directory: result_img
- //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);
- //}
- //static int img_id = 0;
- //img_id++;
- //char image_name[1024];
- //sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, class_id, names[class_id]);
- //CvRect rect = cvRect(pt1.x, pt1.y, pt2.x - pt1.x, pt2.y - pt1.y);
- //cvSetImageROI(copy_img, rect);
- //cvSaveImage(image_name, copy_img, 0);
- //cvResetImageROI(copy_img);
-
- cvRectangle(show_img, pt1, pt2, color, width, 8, 0);
- if (ext_output)
- printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
- (float)left, (float)top, b.w*show_img->width, b.h*show_img->height);
- else
- printf("\n");
-
- cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
- cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled
- CvScalar black_color;
- black_color.val[0] = 0;
- CvFont font;
- cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, 0, font_size * 3, 8);
- cvPutText(show_img, labelstr, pt_text, &font, black_color);
- }
- }
- if (ext_output) {
- fflush(stdout);
- }
-}
-
-void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
-{
- int i;
-
- for (i = 0; i < num; ++i) {
- int class_id = max_index(probs[i], classes);
- float prob = probs[i][class_id];
- if (prob > thresh) {
-
- int width = show_img->height * .012;
-
- if (0) {
- width = pow(prob, 1. / 2.) * 10 + 1;
- alphabet = 0;
- }
-
- printf("%s: %.0f%%\n", names[class_id], prob * 100);
- int offset = class_id * 123457 % classes;
- float red = get_color(2, offset, classes);
- float green = get_color(1, offset, classes);
- float blue = get_color(0, offset, classes);
- float rgb[3];
-
- //width = prob*20+2;
-
- rgb[0] = red;
- rgb[1] = green;
- rgb[2] = blue;
- box b = boxes[i];
-
- int left = (b.x - b.w / 2.)*show_img->width;
- int right = (b.x + b.w / 2.)*show_img->width;
- int top = (b.y - b.h / 2.)*show_img->height;
- int bot = (b.y + b.h / 2.)*show_img->height;
-
- if (left < 0) left = 0;
- if (right > show_img->width - 1) right = show_img->width - 1;
- if (top < 0) top = 0;
- if (bot > show_img->height - 1) bot = show_img->height - 1;
-
- float const font_size = show_img->height / 1000.F;
- CvPoint pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
- pt1.x = left;
- pt1.y = top;
- pt2.x = right;
- pt2.y = bot;
- pt_text.x = left;
- pt_text.y = top - 12;
- pt_text_bg1.x = left;
- pt_text_bg1.y = top - (10+25*font_size);
- pt_text_bg2.x = right;
- pt_text_bg2.y = top;
- CvScalar color;
- color.val[0] = red * 256;
- color.val[1] = green * 256;
- color.val[2] = blue * 256;
-
- cvRectangle(show_img, pt1, pt2, color, width, 8, 0);
- //printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class_id, names[class_id]);
- cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
- cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled
- CvScalar black_color;
- black_color.val[0] = 0;
- CvFont font;
- cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, 0, font_size * 3, 8);
- cvPutText(show_img, names[class_id], pt_text, &font, black_color);
- }
- }
-}
-
-IplImage* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show)
-{
- int img_offset = 50;
- int draw_size = img_size - img_offset;
- IplImage* img = cvCreateImage(cvSize(img_size, img_size), 8, 3);
- cvSet(img, CV_RGB(255, 255, 255), 0);
- CvPoint pt1, pt2, pt_text;
- CvFont font;
- cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 1, CV_AA);
- char char_buff[100];
- int i;
- // vertical lines
- pt1.x = img_offset; pt2.x = img_size, pt_text.x = 10;
- for (i = 1; i <= number_of_lines; ++i) {
- pt1.y = pt2.y = (float)i * draw_size / number_of_lines;
- cvLine(img, pt1, pt2, CV_RGB(224, 224, 224), 1, 8, 0);
- if (i % 10 == 0) {
- sprintf(char_buff, "%2.1f", max_img_loss*(number_of_lines - i) / number_of_lines);
- pt_text.y = pt1.y + 5;
- cvPutText(img, char_buff, pt_text, &font, CV_RGB(0, 0, 0));
- cvLine(img, pt1, pt2, CV_RGB(128, 128, 128), 1, 8, 0);
- }
- }
- // horizontal lines
- pt1.y = draw_size; pt2.y = 0, pt_text.y = draw_size + 15;
- for (i = 0; i <= number_of_lines; ++i) {
- pt1.x = pt2.x = img_offset + (float)i * draw_size / number_of_lines;
- cvLine(img, pt1, pt2, CV_RGB(224, 224, 224), 1, 8, 0);
- if (i % 10 == 0) {
- sprintf(char_buff, "%d", max_batches * i / number_of_lines);
- pt_text.x = pt1.x - 20;
- cvPutText(img, char_buff, pt_text, &font, CV_RGB(0, 0, 0));
- cvLine(img, pt1, pt2, CV_RGB(128, 128, 128), 1, 8, 0);
- }
- }
-
- cvPutText(img, "Loss", cvPoint(0, 35), &font, CV_RGB(0, 0, 255));
- cvPutText(img, "Iteration number", cvPoint(draw_size / 2, img_size - 10), &font, CV_RGB(0, 0, 0));
- char max_batches_buff[100];
- sprintf(max_batches_buff, "in cfg max_batches=%d", max_batches);
- cvPutText(img, max_batches_buff, cvPoint(draw_size - 195, img_size - 10), &font, CV_RGB(0, 0, 0));
- cvPutText(img, "Press 's' to save: chart.png", cvPoint(5, img_size - 10), &font, CV_RGB(0, 0, 0));
- if (!dont_show) {
- printf(" If error occurs - run training with flag: -dont_show \n");
- cvNamedWindow("average loss", CV_WINDOW_NORMAL);
- cvMoveWindow("average loss", 0, 0);
- cvResizeWindow("average loss", img_size, img_size);
- cvShowImage("average loss", img);
- cvWaitKey(20);
- }
- return img;
-}
-
-void draw_train_loss(IplImage* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
- float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port)
-{
- int img_offset = 50;
- int draw_size = img_size - img_offset;
- CvFont font;
- cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 1, CV_AA);
- char char_buff[100];
- CvPoint pt1, pt2;
- pt1.x = img_offset + draw_size * (float)current_batch / max_batches;
- pt1.y = draw_size * (1 - avg_loss / max_img_loss);
- if (pt1.y < 0) pt1.y = 1;
- cvCircle(img, pt1, 1, CV_RGB(0, 0, 255), CV_FILLED, 8, 0);
-
- // precision
- if (draw_precision) {
- static float old_precision = 0;
- static int iteration_old = 0;
- static int text_iteration_old = 0;
- if(iteration_old == 0) cvPutText(img, accuracy_name, cvPoint(0, 12), &font, CV_RGB(255, 0, 0));
-
- cvLine(img,
- cvPoint(img_offset + draw_size * (float)iteration_old / max_batches, draw_size * (1 - old_precision)),
- cvPoint(img_offset + draw_size * (float)current_batch / max_batches, draw_size * (1 - precision)),
- CV_RGB(255, 0, 0), 1, 8, 0);
-
- if (((int)(old_precision*10) != (int)(precision*10)) || (current_batch - text_iteration_old) >= max_batches/10) {
- text_iteration_old = current_batch;
- sprintf(char_buff, "%2.0f%% ", precision * 100);
- CvFont font3;
- cvInitFont(&font3, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 5, CV_AA);
- cvPutText(img, char_buff, cvPoint(pt1.x - 30, draw_size * (1 - precision) + 15), &font3, CV_RGB(255, 255, 255));
-
- CvFont font2;
- cvInitFont(&font2, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 1, CV_AA);
- cvPutText(img, char_buff, cvPoint(pt1.x - 30, draw_size * (1 - precision) + 15), &font2, CV_RGB(200, 0, 0));
- }
- old_precision = precision;
- iteration_old = current_batch;
- }
-
- sprintf(char_buff, "current avg loss = %2.4f iteration = %d", avg_loss, current_batch);
- pt1.x = 55, pt1.y = 10;
- pt2.x = pt1.x + 460, pt2.y = pt1.y + 20;
- cvRectangle(img, pt1, pt2, CV_RGB(255, 255, 255), CV_FILLED, 8, 0);
- pt1.y += 15;
- cvPutText(img, char_buff, pt1, &font, CV_RGB(0, 0, 0));
-
- int k = 0;
- if (!dont_show) {
- cvShowImage("average loss", img);
- k = cvWaitKey(20);
- }
- if (k == 's' || current_batch == (max_batches - 1) || current_batch % 100 == 0) {
- save_cv_png(img, "chart.png");
- cvPutText(img, "- Saved", cvPoint(250, img_size - 10), &font, CV_RGB(255, 0, 0));
- }
- else
- cvPutText(img, "- Saved", cvPoint(250, img_size - 10), &font, CV_RGB(255, 255, 255));
-
- if (mjpeg_port > 0) send_mjpeg(img, mjpeg_port, 500000, 100);
-}
-#endif // OPENCV
-
void transpose_image(image im)
{
assert(im.w == im.h);
@@ -1005,64 +661,6 @@ void rgbgr_image(image im)
}
}
-#ifdef OPENCV
-void show_image_cv(image p, const char *name)
-{
- int x,y,k;
- image copy = copy_image(p);
- constrain_image(copy);
- if(p.c == 3) rgbgr_image(copy);
- //normalize_image(copy);
-
- char buff[256];
- //sprintf(buff, "%s (%d)", name, windows);
- sprintf(buff, "%s", name);
-
- IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
- int step = disp->widthStep;
- cvNamedWindow(buff, CV_WINDOW_NORMAL);
- //cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
- ++windows;
- for(y = 0; y < p.h; ++y){
- for(x = 0; x < p.w; ++x){
- for(k= 0; k < p.c; ++k){
- disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy,x,y,k)*255);
- }
- }
- }
- free_image(copy);
- if(0){
- int w = 448;
- int h = w*p.h/p.w;
- if(h > 1000){
- h = 1000;
- w = h*p.w/p.h;
- }
- IplImage *buffer = disp;
- disp = cvCreateImage(cvSize(w, h), buffer->depth, buffer->nChannels);
- cvResize(buffer, disp, CV_INTER_LINEAR);
- cvReleaseImage(&buffer);
- }
- cvShowImage(buff, disp);
-
- cvReleaseImage(&disp);
-}
-
-
-void show_image_cv_ipl(IplImage *disp, const char *name)
-{
- if (disp == NULL) return;
- char buff[256];
- //sprintf(buff, "%s (%d)", name, windows);
- sprintf(buff, "%s", name);
- cvNamedWindow(buff, CV_WINDOW_NORMAL);
- //cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
- ++windows;
- cvShowImage(buff, disp);
- //cvReleaseImage(&disp);
-}
-#endif // OPENCV
-
void show_image(image p, const char *name)
{
#ifdef OPENCV
@@ -1070,190 +668,8 @@ void show_image(image p, const char *name)
#else
fprintf(stderr, "Not compiled with OpenCV, saving to %s.png instead\n", name);
save_image(p, name);
-#endif
-}
-
-#ifdef OPENCV
-
-image ipl_to_image(IplImage* src)
-{
- unsigned char *data = (unsigned char *)src->imageData;
- int h = src->height;
- int w = src->width;
- int c = src->nChannels;
- int step = src->widthStep;
- image out = make_image(w, h, c);
- int i, j, k, count=0;;
-
- for(k= 0; k < c; ++k){
- for(i = 0; i < h; ++i){
- for(j = 0; j < w; ++j){
- out.data[count++] = data[i*step + j*c + k]/255.;
- }
- }
- }
- return out;
-}
-
-image load_image_cv(char *filename, int channels)
-{
- IplImage* src = 0;
- int flag = -1;
- if (channels == 0) flag = 1;
- else if (channels == 1) flag = 0;
- else if (channels == 3) flag = 1;
- else {
- fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
- }
-
- if( (src = cvLoadImage(filename, flag)) == 0 )
- {
- char shrinked_filename[1024];
- if (strlen(filename) >= 1024) sprintf(shrinked_filename, "name is too long");
- else sprintf(shrinked_filename, "%s", filename);
- fprintf(stderr, "Cannot load image \"%s\"\n", shrinked_filename);
- FILE* fw = fopen("bad.list", "a");
- fwrite(shrinked_filename, sizeof(char), strlen(shrinked_filename), fw);
- char *new_line = "\n";
- fwrite(new_line, sizeof(char), strlen(new_line), fw);
- fclose(fw);
- if (check_mistakes) getchar();
- return make_image(10,10,3);
- //exit(EXIT_FAILURE);
- }
- image out = ipl_to_image(src);
- cvReleaseImage(&src);
- if (out.c > 1)
- rgbgr_image(out);
- return out;
-}
-
-image get_image_from_stream(CvCapture *cap)
-{
- IplImage* src = cvQueryFrame(cap);
- if (!src) return make_empty_image(0,0,0);
- image im = ipl_to_image(src);
- rgbgr_image(im);
- return im;
-}
-
-image get_image_from_stream_cpp(CvCapture *cap)
-{
- //IplImage* src = cvQueryFrame(cap);
- IplImage* src;
- static int once = 1;
- if (once) {
- once = 0;
- do {
- src = get_webcam_frame(cap);
- if (!src) return make_empty_image(0, 0, 0);
- } while (src->width < 1 || src->height < 1 || src->nChannels < 1);
- printf("Video stream: %d x %d \n", src->width, src->height);
- }
- else
- src = get_webcam_frame(cap);
-
- if (!src) return make_empty_image(0, 0, 0);
- image im = ipl_to_image(src);
- rgbgr_image(im);
- return im;
-}
-
-int wait_for_stream(CvCapture *cap, IplImage* src, int dont_close) {
- if (!src) {
- if (dont_close) src = cvCreateImage(cvSize(416, 416), IPL_DEPTH_8U, 3);
- else return 0;
- }
- if (src->width < 1 || src->height < 1 || src->nChannels < 1) {
- if (dont_close) {
- cvReleaseImage(&src);
- int z = 0;
- for (z = 0; z < 20; ++z) {
- get_webcam_frame(cap);
- cvReleaseImage(&src);
- }
- src = cvCreateImage(cvSize(416, 416), IPL_DEPTH_8U, 3);
- }
- else return 0;
- }
- return 1;
-}
-
-image get_image_from_stream_resize(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close)
-{
- c = c ? c : 3;
- IplImage* src;
- if (cpp_video_capture) {
- static int once = 1;
- if (once) {
- once = 0;
- do {
- src = get_webcam_frame(cap);
- if (!src) return make_empty_image(0, 0, 0);
- } while (src->width < 1 || src->height < 1 || src->nChannels < 1);
- printf("Video stream: %d x %d \n", src->width, src->height);
- } else
- src = get_webcam_frame(cap);
- }
- else src = cvQueryFrame(cap);
-
- if (cpp_video_capture)
- if(!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0);
- IplImage* new_img = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, c);
- *in_img = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, c);
- cvResize(src, *in_img, CV_INTER_LINEAR);
- cvResize(src, new_img, CV_INTER_LINEAR);
- image im = ipl_to_image(new_img);
- cvReleaseImage(&new_img);
- if (cpp_video_capture) cvReleaseImage(&src);
- if (c>1)
- rgbgr_image(im);
- return im;
-}
-
-image get_image_from_stream_letterbox(CvCapture *cap, int w, int h, int c, IplImage** in_img, int cpp_video_capture, int dont_close)
-{
- c = c ? c : 3;
- IplImage* src;
- if (cpp_video_capture) {
- static int once = 1;
- if (once) {
- once = 0;
- do {
- src = get_webcam_frame(cap);
- if (!src) return make_empty_image(0, 0, 0);
- } while (src->width < 1 || src->height < 1 || src->nChannels < 1);
- printf("Video stream: %d x %d \n", src->width, src->height);
- }
- else
- src = get_webcam_frame(cap);
- }
- else src = cvQueryFrame(cap);
-
- if (cpp_video_capture)
- if (!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0);
- *in_img = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, c);
- cvResize(src, *in_img, CV_INTER_LINEAR);
- image tmp = ipl_to_image(src);
- image im = letterbox_image(tmp, w, h);
- free_image(tmp);
- if (cpp_video_capture) cvReleaseImage(&src);
- if (c>1) rgbgr_image(im);
- return im;
-}
-
-int get_stream_fps(CvCapture *cap, int cpp_video_capture)
-{
- int fps = 25;
- if (cpp_video_capture) {
- fps = get_stream_fps_cpp(cap);
- }
- else {
- fps = cvGetCaptureProperty(cap, CV_CAP_PROP_FPS);
- }
- return fps;
-}
#endif // OPENCV
+}
void save_image_png(image im, const char *name)
{
@@ -1925,6 +1341,7 @@ void test_resize(char *filename)
show_image(c2, "C2");
show_image(c3, "C3");
show_image(c4, "C4");
+
#ifdef OPENCV
while(1){
image aug = random_augment_image(im, 0, .75, 320, 448, 320);
@@ -1946,7 +1363,7 @@ void test_resize(char *filename)
show_image(c, "rand");
printf("%f %f %f\n", dhue, dsat, dexp);
free_image(c);
- cvWaitKey(0);
+ wait_until_press_key_cv();
}
#endif
}
@@ -1989,17 +1406,11 @@ image load_image_stb(char *filename, int channels)
image load_image(char *filename, int w, int h, int c)
{
#ifdef OPENCV
-
-#ifndef CV_VERSION_EPOCH
- //image out = load_image_stb(filename, c); // OpenCV 3.x
+ //image out = load_image_stb(filename, c);
image out = load_image_cv(filename, c);
-#else
- image out = load_image_cv(filename, c); // OpenCV 2.4.x
-#endif
-
#else
image out = load_image_stb(filename, c); // without OpenCV
-#endif
+#endif // OPENCV
if((h && w) && (h != out.h || w != out.w)){
image resized = resize_image(out, w, h);
diff --git a/src/image.h b/src/image.h
index 91c6fde7..3a1c5b9a 100644
--- a/src/image.h
+++ b/src/image.h
@@ -7,16 +7,9 @@
#include
#include
#include
-#ifdef OPENCV
-#include
-#include
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#include
-#endif
-#endif
+
+#include "image_opencv.h"
+
#include "box.h"
#ifdef __cplusplus
extern "C" {
@@ -92,12 +85,6 @@ image load_image(char *filename, int w, int h, int c);
//LIB_API image load_image_color(char *filename, int w, int h);
image **load_alphabet();
-#ifdef OPENCV
-image get_image_from_stream(CvCapture* cap);
-image get_image_from_stream_cpp(CvCapture* cap);
-image ipl_to_image(IplImage* src);
-void save_cv_png(IplImage *img, const char *name);
-#endif
//float get_pixel(image m, int x, int y, int c);
//float get_pixel_extend(image m, int x, int y, int c);
//void set_pixel(image m, int x, int y, int c, float val);
diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp
new file mode 100644
index 00000000..d09e3018
--- /dev/null
+++ b/src/image_opencv.cpp
@@ -0,0 +1,1165 @@
+#ifdef OPENCV
+
+#include "image_opencv.h"
+#include "utils.h"
+
+#include
+#include
+#include
+
+#include "opencv2/opencv.hpp"
+
+using namespace cv;
+
+using std::cerr;
+using std::endl;
+
+// OpenCV libraries
+#ifndef CV_VERSION_EPOCH
+#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)"" CVAUX_STR(CV_VERSION_REVISION)
+#ifndef USE_CMAKE_LIBS
+#pragma comment(lib, "opencv_world" OPENCV_VERSION ".lib")
+#endif // USE_CMAKE_LIBS
+#else // CV_VERSION_EPOCH
+#define OPENCV_VERSION CVAUX_STR(CV_VERSION_EPOCH)"" CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)
+#ifndef USE_CMAKE_LIBS
+#pragma comment(lib, "opencv_core" OPENCV_VERSION ".lib")
+#pragma comment(lib, "opencv_imgproc" OPENCV_VERSION ".lib")
+#pragma comment(lib, "opencv_highgui" OPENCV_VERSION ".lib")
+#endif // USE_CMAKE_LIBS
+#endif // CV_VERSION_EPOCH
+
+// OpenCV includes
+#include
+#include
+#include
+#include
+#ifndef CV_VERSION_EPOCH
+#include
+#include
+#include "http_stream.h"
+#endif
+#include "http_stream.h"
+
+#ifndef CV_RGB
+#define CV_RGB(r, g, b) cvScalar( (b), (g), (r), 0 )
+#endif
+
+extern "C" {
+
+ struct mat_cv :IplImage { int a[0]; };
+ //struct cap_cv : CvCapture { int a[0]; };
+ //struct write_cv : CvVideoWriter { int a[0]; };
+
+// ====================================================================
+// cv::Mat / IplImage
+// ====================================================================
+image ipl_to_image(IplImage* src);
+
+image load_image_cv(char *filename, int channels)
+{
+ IplImage* src = 0;
+ int flag = -1;
+ if (channels == 0) flag = 1;
+ else if (channels == 1) flag = 0;
+ else if (channels == 3) flag = 1;
+ else {
+ fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
+ }
+
+ if ((src = cvLoadImage(filename, flag)) == 0)
+ {
+ char shrinked_filename[1024];
+ if (strlen(filename) >= 1024) sprintf(shrinked_filename, "name is too long");
+ else sprintf(shrinked_filename, "%s", filename);
+ fprintf(stderr, "Cannot load image \"%s\"\n", shrinked_filename);
+ FILE* fw = fopen("bad.list", "a");
+ fwrite(shrinked_filename, sizeof(char), strlen(shrinked_filename), fw);
+ char *new_line = "\n";
+ fwrite(new_line, sizeof(char), strlen(new_line), fw);
+ fclose(fw);
+ //if (check_mistakes) getchar();
+ return make_image(10, 10, 3);
+ //exit(EXIT_FAILURE);
+ }
+ image out = ipl_to_image(src);
+ cvReleaseImage(&src);
+ if (out.c > 1)
+ rgbgr_image(out);
+ return out;
+}
+// ----------------------------------------
+
+mat_cv *load_image_mat_cv(const char *filename, int flag)
+{
+ return (mat_cv *)cvLoadImage(filename, flag);
+}
+// ----------------------------------------
+
+
+image load_image_resize(char *filename, int w, int h, int c, image *im)
+{
+ image out;
+ cv::Mat img(h, w, CV_8UC3);
+ try {
+ int flag = -1;
+ if (c == 0) flag = 1;
+ else if (c == 1) { flag = 0; img = cv::Mat(h, w, CV_8UC1); }
+ else if (c == 3) { flag = 1; img = cv::Mat(h, w, CV_8UC3); }
+ else {
+ cerr << "OpenCV can't force load with " << c << " channels\n";
+ }
+ //throw std::runtime_error("runtime_error");
+ cv::Mat loaded_image = cv::imread(filename, flag);
+ cv::cvtColor(loaded_image, loaded_image, cv::COLOR_RGB2BGR);
+ IplImage tmp1 = loaded_image;
+ *im = ipl_to_image(&tmp1);
+
+ cv::resize(loaded_image, img, cv::Size(w, h), 0, 0, CV_INTER_LINEAR);
+
+ IplImage tmp2 = img;
+ out = ipl_to_image(&tmp2);
+ }
+ catch (...) {
+ cerr << "OpenCV can't load image %s " << filename << " \n";
+ out = make_image(w, h, c);
+ *im = make_image(w, h, c);
+ }
+ return out;
+}
+// ----------------------------------------
+
+int get_width_cv(mat_cv *ipl_src)
+{
+ IplImage *ipl = (IplImage *)ipl_src;
+ return ipl->width;
+}
+// ----------------------------------------
+
+int get_height_cv(mat_cv *ipl)
+{
+ //IplImage *ipl = (IplImage *)ipl_src;
+ return ipl->height;
+}
+// ----------------------------------------
+
+void release_ipl(mat_cv **ipl)
+{
+ IplImage **ipl_img = (IplImage **)ipl;
+ if (*ipl_img) cvReleaseImage(ipl_img);
+ *ipl_img = NULL;
+}
+
+// ====================================================================
+// image-to-ipl, ipl-to-image
+// ====================================================================
+mat_cv *image_to_ipl(image im)
+{
+ int x, y, c;
+ IplImage *disp = cvCreateImage(cvSize(im.w, im.h), IPL_DEPTH_8U, im.c);
+ int step = disp->widthStep;
+ for (y = 0; y < im.h; ++y) {
+ for (x = 0; x < im.w; ++x) {
+ for (c = 0; c < im.c; ++c) {
+ float val = im.data[c*im.h*im.w + y*im.w + x];
+ disp->imageData[y*step + x*im.c + c] = (unsigned char)(val * 255);
+ }
+ }
+ }
+ return (mat_cv *)disp;
+}
+// ----------------------------------------
+
+image ipl_to_image(IplImage* src)
+{
+ int h = src->height;
+ int w = src->width;
+ int c = src->nChannels;
+ image im = make_image(w, h, c);
+ unsigned char *data = (unsigned char *)src->imageData;
+ int step = src->widthStep;
+ int i, j, k;
+
+ for (i = 0; i < h; ++i) {
+ for (k = 0; k < c; ++k) {
+ for (j = 0; j < w; ++j) {
+ im.data[k*w*h + i*w + j] = data[i*step + j*c + k] / 255.;
+ }
+ }
+ }
+ return im;
+}
+// ----------------------------------------
+
+image ipl_to_image_custom(mat_cv* src)
+{
+ return ipl_to_image(src);
+}
+// ----------------------------------------
+
+Mat image_to_mat(image im)
+{
+ image copy = copy_image(im);
+ constrain_image(copy);
+ if (im.c == 3) rgbgr_image(copy);
+
+ IplImage *ipl = image_to_ipl(copy);
+ Mat m = cvarrToMat(ipl, true);
+ cvReleaseImage(&ipl);
+ free_image(copy);
+ return m;
+}
+// ----------------------------------------
+
+image mat_to_image(Mat m)
+{
+ IplImage ipl = m;
+ image im = ipl_to_image((mat_cv *)&ipl);
+ rgbgr_image(im);
+ return im;
+}
+
+// ====================================================================
+// Window
+// ====================================================================
+void create_window_cv(char const* window_name, int full_screen, int width, int height)
+{
+ int window_type = CV_WINDOW_NORMAL;
+ if (full_screen) window_type = CV_WINDOW_FULLSCREEN;
+
+ cvNamedWindow(window_name, window_type);
+ cvMoveWindow(window_name, 0, 0);
+ cvResizeWindow(window_name, width, height);
+}
+// ----------------------------------------
+
+void destroy_all_windows_cv()
+{
+ cvDestroyAllWindows();
+}
+// ----------------------------------------
+
+int wait_key_cv(int delay)
+{
+ return cvWaitKey(delay);
+}
+// ----------------------------------------
+
+int wait_until_press_key_cv()
+{
+ return wait_key_cv(0);
+}
+// ----------------------------------------
+
+void make_window(char *name, int w, int h, int fullscreen)
+{
+ namedWindow(name, WINDOW_NORMAL);
+ if (fullscreen) {
+ setWindowProperty(name, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
+ }
+ else {
+ resizeWindow(name, w, h);
+ if (strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);
+ }
+}
+// ----------------------------------------
+
+static float get_pixel(image m, int x, int y, int c)
+{
+ assert(x < m.w && y < m.h && c < m.c);
+ return m.data[c*m.h*m.w + y*m.w + x];
+}
+// ----------------------------------------
+
+void show_image_cv(image p, const char *name)
+{
+ int x, y, k;
+ image copy = copy_image(p);
+ constrain_image(copy);
+ if (p.c == 3) rgbgr_image(copy);
+ //normalize_image(copy);
+
+ char buff[256];
+ //sprintf(buff, "%s (%d)", name, windows);
+ sprintf(buff, "%s", name);
+
+ IplImage *disp = cvCreateImage(cvSize(p.w, p.h), IPL_DEPTH_8U, p.c);
+ int step = disp->widthStep;
+ cvNamedWindow(buff, CV_WINDOW_NORMAL);
+ //cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
+ //++windows;
+ for (y = 0; y < p.h; ++y) {
+ for (x = 0; x < p.w; ++x) {
+ for (k = 0; k < p.c; ++k) {
+ disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy, x, y, k) * 255);
+ }
+ }
+ }
+ free_image(copy);
+ if (0) {
+ int w = 448;
+ int h = w*p.h / p.w;
+ if (h > 1000) {
+ h = 1000;
+ w = h*p.w / p.h;
+ }
+ IplImage *buffer = disp;
+ disp = cvCreateImage(cvSize(w, h), buffer->depth, buffer->nChannels);
+ cvResize(buffer, disp, CV_INTER_LINEAR);
+ cvReleaseImage(&buffer);
+ }
+ cvShowImage(buff, disp);
+
+ cvReleaseImage(&disp);
+}
+// ----------------------------------------
+
+void show_image_cv_ipl(mat_cv *disp, const char *name)
+{
+ if (disp == NULL) return;
+ char buff[256];
+ //sprintf(buff, "%s (%d)", name, windows);
+ sprintf(buff, "%s", name);
+ cvNamedWindow(buff, CV_WINDOW_NORMAL);
+ //cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
+ //++windows;
+ cvShowImage(buff, disp);
+ //cvReleaseImage(&disp);
+}
+
+
+// ====================================================================
+// Video Writer
+// ====================================================================
+write_cv *create_video_writer(char *out_filename, char c1, char c2, char c3, char c4, int fps, int width, int height, int is_color)
+{
+ CvVideoWriter * output_video_writer =
+ cvCreateVideoWriter(out_filename, CV_FOURCC(c1, c2, c3, c4), fps, cvSize(width, height), is_color);
+
+ return (write_cv *)output_video_writer;
+}
+
+int write_frame_cv(write_cv *output_video_writer, mat_cv *show_img)
+{
+ return cvWriteFrame((CvVideoWriter *)output_video_writer, show_img);
+}
+
+void release_video_writer(write_cv **output_video_writer)
+{
+ cvReleaseVideoWriter((CvVideoWriter **)output_video_writer);
+}
+
+/*
+void *open_video_stream(const char *f, int c, int w, int h, int fps)
+{
+ VideoCapture *cap;
+ if(f) cap = new VideoCapture(f);
+ else cap = new VideoCapture(c);
+ if(!cap->isOpened()) return 0;
+ if(w) cap->set(CV_CAP_PROP_FRAME_WIDTH, w);
+ if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
+ if(fps) cap->set(CV_CAP_PROP_FPS, w);
+ return (void *) cap;
+}
+
+
+image get_image_from_stream(void *p)
+{
+ VideoCapture *cap = (VideoCapture *)p;
+ Mat m;
+ *cap >> m;
+ if(m.empty()) return make_empty_image(0,0,0);
+ return mat_to_image(m);
+}
+
+
+
+int show_image_cv(image im, const char* name, int ms)
+{
+ Mat m = image_to_mat(im);
+ imshow(name, m);
+ int c = waitKey(ms);
+ if (c != -1) c = c%256;
+ return c;
+}
+*/
+
+
+// ====================================================================
+// Video Capture
+// ====================================================================
+
+cap_cv* get_capture_video_stream(const char *path) {
+ CvCapture* cap = NULL;
+ try {
+ cap = (CvCapture*)new cv::VideoCapture(path);
+ }
+ catch (...) {
+ cerr << " Error: video-stream " << path << " can't be opened! \n";
+ }
+ return (cap_cv*)cap;
+}
+// ----------------------------------------
+
+cap_cv* get_capture_webcam(int index)
+{
+ CvCapture* cap = NULL;
+ try {
+ cap = (CvCapture*)new cv::VideoCapture(index);
+ //((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_WIDTH, 1280);
+ //((cv::VideoCapture*)cap)->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
+ }
+ catch (...) {
+ cerr << " Error: Web-camera " << index << " can't be opened! \n";
+ }
+ return (cap_cv*)cap;
+}
+// ----------------------------------------
+
+void release_capture(cap_cv* cap)
+{
+ try {
+ cv::VideoCapture *cpp_cap = (cv::VideoCapture *)cap;
+ delete cpp_cap;
+ }
+ catch (...) {
+ cerr << " Error: cv::VideoCapture " << cap << " can't be released! \n";
+ }
+}
+// ----------------------------------------
+
+mat_cv* get_capture_frame_cv(cap_cv *cap) {
+ IplImage* src = NULL;
+ try {
+ cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+ cv::Mat frame;
+ if (cpp_cap.isOpened())
+ {
+ cpp_cap >> frame;
+ IplImage tmp = frame;
+ src = cvCloneImage(&tmp);
+ }
+ else {
+ std::cout << " Video-stream stopped! \n";
+ }
+ }
+ catch (...) {
+ std::cout << " Video-stream stoped! \n";
+ }
+ return (mat_cv *)src;
+}
+// ----------------------------------------
+
+int get_stream_fps_cpp_cv(cap_cv *cap)
+{
+ int fps = 25;
+ try {
+ cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+#ifndef CV_VERSION_EPOCH // OpenCV 3.x
+ fps = cpp_cap.get(CAP_PROP_FPS);
+#else // OpenCV 2.x
+ fps = cpp_cap.get(CV_CAP_PROP_FPS);
+#endif
+ }
+ catch (...) {
+ cerr << " Can't get FPS of source videofile. For output video FPS = 25 by default. \n";
+ }
+ return fps;
+}
+// ----------------------------------------
+
+double get_capture_property_cv(cap_cv *cap, int property_id)
+{
+ try {
+ cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+ return cpp_cap.get(property_id);
+ }
+ catch (...) {
+ cerr << " Can't get property of source video-stream. \n";
+ }
+ return 0;
+}
+// ----------------------------------------
+
+double get_capture_frame_count_cv(cap_cv *cap)
+{
+ try {
+ cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+#ifndef CV_VERSION_EPOCH // OpenCV 3.x
+ return cpp_cap.get(CAP_PROP_FRAME_COUNT);
+#else // OpenCV 2.x
+ return cpp_cap.get(CV_CAP_PROP_FRAME_COUNT);
+#endif
+ }
+ catch (...) {
+ cerr << " Can't get CAP_PROP_FRAME_COUNT of source videofile. \n";
+ }
+ return 0;
+}
+// ----------------------------------------
+
+int set_capture_property_cv(cap_cv *cap, int property_id, double value)
+{
+ try {
+ cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+ return cpp_cap.set(property_id, value);
+ }
+ catch (...) {
+ cerr << " Can't set property of source video-stream. \n";
+ }
+ return false;
+}
+// ----------------------------------------
+
+int set_capture_position_frame_cv(cap_cv *cap, int index)
+{
+ try {
+ cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
+#ifndef CV_VERSION_EPOCH // OpenCV 3.x
+ return cpp_cap.set(CAP_PROP_POS_FRAMES, index);
+#else // OpenCV 2.x
+ return cpp_cap.set(CV_CAP_PROP_POS_FRAMES, index);
+#endif
+ }
+ catch (...) {
+ cerr << " Can't set CAP_PROP_POS_FRAMES of source videofile. \n";
+ }
+ return false;
+}
+// ----------------------------------------
+
+// only for using in image_opencv.cpp
+mat_cv* get_capture_frame(CvCapture *cap)
+{
+ return get_capture_frame_cv((cap_cv *)cap);
+}
+// ----------------------------------------
+
+// only for using in image_opencv.cpp
+int get_stream_fps_cpp(CvCapture *cap)
+{
+ return get_stream_fps_cpp_cv((cap_cv *)cap);
+}
+
+
+// ====================================================================
+// ... Video Capture
+// ====================================================================
+image get_image_from_stream(cap_cv *cap_src)
+{
+ CvCapture *cap = (CvCapture *)cap_src;
+ IplImage* src = cvQueryFrame(cap);
+ if (!src) return make_empty_image(0, 0, 0);
+ image im = ipl_to_image(src);
+ rgbgr_image(im);
+ return im;
+}
+// ----------------------------------------
+
+image get_image_from_stream_cpp(cap_cv *cap_src)
+{
+ CvCapture *cap = (CvCapture *)cap_src;
+ //IplImage* src = cvQueryFrame(cap);
+ IplImage* src;
+ static int once = 1;
+ if (once) {
+ once = 0;
+ do {
+ src = get_capture_frame(cap);
+ if (!src) return make_empty_image(0, 0, 0);
+ } while (src->width < 1 || src->height < 1 || src->nChannels < 1);
+ printf("Video stream: %d x %d \n", src->width, src->height);
+ }
+ else
+ src = get_capture_frame(cap);
+
+ if (!src) return make_empty_image(0, 0, 0);
+ image im = ipl_to_image(src);
+ rgbgr_image(im);
+ return im;
+}
+// ----------------------------------------
+
+int wait_for_stream(CvCapture *cap, IplImage* src, int dont_close)
+{
+ if (!src) {
+ if (dont_close) src = cvCreateImage(cvSize(416, 416), IPL_DEPTH_8U, 3);
+ else return 0;
+ }
+ if (src->width < 1 || src->height < 1 || src->nChannels < 1) {
+ if (dont_close) {
+ cvReleaseImage(&src);
+ int z = 0;
+ for (z = 0; z < 20; ++z) {
+ get_capture_frame(cap);
+ cvReleaseImage(&src);
+ }
+ src = cvCreateImage(cvSize(416, 416), IPL_DEPTH_8U, 3);
+ }
+ else return 0;
+ }
+ return 1;
+}
+// ----------------------------------------
+
+image get_image_from_stream_resize(cap_cv *cap_src, int w, int h, int c, mat_cv** in_img, int cpp_video_capture, int dont_close)
+{
+ CvCapture *cap = (CvCapture *)cap_src;
+ c = c ? c : 3;
+ IplImage* src;
+ if (cpp_video_capture) {
+ static int once = 1;
+ if (once) {
+ once = 0;
+ do {
+ src = get_capture_frame(cap);
+ if (!src) return make_empty_image(0, 0, 0);
+ } while (src->width < 1 || src->height < 1 || src->nChannels < 1);
+ printf("Video stream: %d x %d \n", src->width, src->height);
+ }
+ else
+ src = get_capture_frame(cap);
+ }
+ else src = cvQueryFrame(cap);
+
+ if (cpp_video_capture)
+ if (!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0);
+ IplImage* new_img = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, c);
+ *in_img = (mat_cv *)cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, c);
+ cvResize(src, *in_img, CV_INTER_LINEAR);
+ cvResize(src, new_img, CV_INTER_LINEAR);
+ image im = ipl_to_image(new_img);
+ cvReleaseImage(&new_img);
+ if (cpp_video_capture) cvReleaseImage(&src);
+ if (c>1)
+ rgbgr_image(im);
+ return im;
+}
+// ----------------------------------------
+
+image get_image_from_stream_letterbox(cap_cv *cap_src, int w, int h, int c, mat_cv** in_img, int cpp_video_capture, int dont_close)
+{
+ CvCapture *cap = (CvCapture *)cap_src;
+ c = c ? c : 3;
+ IplImage* src;
+ if (cpp_video_capture) {
+ static int once = 1;
+ if (once) {
+ once = 0;
+ do {
+ src = get_capture_frame(cap);
+ if (!src) return make_empty_image(0, 0, 0);
+ } while (src->width < 1 || src->height < 1 || src->nChannels < 1);
+ printf("Video stream: %d x %d \n", src->width, src->height);
+ }
+ else
+ src = get_capture_frame(cap);
+ }
+ else src = cvQueryFrame(cap);
+
+ if (cpp_video_capture)
+ if (!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0);
+ *in_img = (mat_cv *)cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, c);
+ cvResize(src, *in_img, CV_INTER_LINEAR);
+ image tmp = ipl_to_image(src);
+ image im = letterbox_image(tmp, w, h);
+ free_image(tmp);
+ if (cpp_video_capture) cvReleaseImage(&src);
+ if (c>1) rgbgr_image(im);
+ return im;
+}
+// ----------------------------------------
+
+int get_stream_fps(cap_cv *cap_src, int cpp_video_capture)
+{
+ CvCapture *cap = (CvCapture *)cap_src;
+ int fps = 25;
+ if (cpp_video_capture) {
+ fps = get_stream_fps_cpp(cap);
+ }
+ else {
+ fps = cvGetCaptureProperty(cap, CV_CAP_PROP_FPS);
+ }
+ return fps;
+}
+// ----------------------------------------
+
+
+// ====================================================================
+// Image Saving
+// ====================================================================
+extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
+extern int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
+
+void save_cv_png(mat_cv *img_src, const char *name)
+{
+ IplImage* img = (IplImage* )img_src;
+ IplImage* img_rgb = cvCreateImage(cvSize(img->width, img->height), 8, 3);
+ cvCvtColor(img, img_rgb, CV_RGB2BGR);
+ stbi_write_png(name, img_rgb->width, img_rgb->height, 3, (char *)img_rgb->imageData, 0);
+ cvRelease((void**)&img_rgb);
+}
+// ----------------------------------------
+
+void save_cv_jpg(mat_cv *img_src, const char *name)
+{
+ IplImage* img = (IplImage*)img_src;
+ IplImage* img_rgb = cvCreateImage(cvSize(img->width, img->height), 8, 3);
+ cvCvtColor(img, img_rgb, CV_RGB2BGR);
+ stbi_write_jpg(name, img_rgb->width, img_rgb->height, 3, (char *)img_rgb->imageData, 80);
+ cvRelease((void**)&img_rgb);
+}
+// ----------------------------------------
+
+// ====================================================================
+// Draw Detection
+// ====================================================================
+void draw_detections_cv_v3(mat_cv* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output)
+{
+ int i, j;
+ if (!show_img) return;
+ static int frame_id = 0;
+ frame_id++;
+
+ for (i = 0; i < num; ++i) {
+ char labelstr[4096] = { 0 };
+ int class_id = -1;
+ for (j = 0; j < classes; ++j) {
+ int show = strncmp(names[j], "dont_show", 9);
+ if (dets[i].prob[j] > thresh && show) {
+ if (class_id < 0) {
+ strcat(labelstr, names[j]);
+ class_id = j;
+ }
+ else {
+ strcat(labelstr, ", ");
+ strcat(labelstr, names[j]);
+ }
+ printf("%s: %.0f%% ", names[j], dets[i].prob[j] * 100);
+ }
+ }
+ if (class_id >= 0) {
+ int width = show_img->height * .006;
+
+ //if(0){
+ //width = pow(prob, 1./2.)*10+1;
+ //alphabet = 0;
+ //}
+
+ //printf("%d %s: %.0f%%\n", i, names[class_id], prob*100);
+ int offset = class_id * 123457 % classes;
+ float red = get_color(2, offset, classes);
+ float green = get_color(1, offset, classes);
+ float blue = get_color(0, offset, classes);
+ float rgb[3];
+
+ //width = prob*20+2;
+
+ rgb[0] = red;
+ rgb[1] = green;
+ rgb[2] = blue;
+ box b = dets[i].bbox;
+ b.w = (b.w < 1) ? b.w : 1;
+ b.h = (b.h < 1) ? b.h : 1;
+ b.x = (b.x < 1) ? b.x : 1;
+ b.y = (b.y < 1) ? b.y : 1;
+ //printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
+
+ int left = (b.x - b.w / 2.)*show_img->width;
+ int right = (b.x + b.w / 2.)*show_img->width;
+ int top = (b.y - b.h / 2.)*show_img->height;
+ int bot = (b.y + b.h / 2.)*show_img->height;
+
+ if (left < 0) left = 0;
+ if (right > show_img->width - 1) right = show_img->width - 1;
+ if (top < 0) top = 0;
+ if (bot > show_img->height - 1) bot = show_img->height - 1;
+
+ //int b_x_center = (left + right) / 2;
+ //int b_y_center = (top + bot) / 2;
+ //int b_width = right - left;
+ //int b_height = bot - top;
+ //sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height);
+
+ float const font_size = show_img->height / 1000.F;
+ CvPoint pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
+ pt1.x = left;
+ pt1.y = top;
+ pt2.x = right;
+ pt2.y = bot;
+ pt_text.x = left;
+ pt_text.y = top - 12;
+ pt_text_bg1.x = left;
+ pt_text_bg1.y = top - (10 + 25 * font_size);
+ pt_text_bg2.x = right;
+ pt_text_bg2.y = top;
+ CvScalar color;
+ color.val[0] = red * 256;
+ color.val[1] = green * 256;
+ color.val[2] = blue * 256;
+
+ // you should create directory: result_img
+ //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);
+ //}
+ //static int img_id = 0;
+ //img_id++;
+ //char image_name[1024];
+ //sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, class_id, names[class_id]);
+ //CvRect rect = cvRect(pt1.x, pt1.y, pt2.x - pt1.x, pt2.y - pt1.y);
+ //cvSetImageROI(copy_img, rect);
+ //cvSaveImage(image_name, copy_img, 0);
+ //cvResetImageROI(copy_img);
+
+ cvRectangle(show_img, pt1, pt2, color, width, 8, 0);
+ if (ext_output)
+ printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
+ (float)left, (float)top, b.w*show_img->width, b.h*show_img->height);
+ else
+ printf("\n");
+
+ cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
+ cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled
+ CvScalar black_color;
+ black_color.val[0] = 0;
+ CvFont font;
+ cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, 0, font_size * 3, 8);
+ cvPutText(show_img, labelstr, pt_text, &font, black_color);
+ }
+ }
+ if (ext_output) {
+ fflush(stdout);
+ }
+}
+// ----------------------------------------
+
+void draw_detections_cv(mat_cv* show_img, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
+{
+ int i;
+
+ for (i = 0; i < num; ++i) {
+ int class_id = max_index(probs[i], classes);
+ float prob = probs[i][class_id];
+ if (prob > thresh) {
+
+ int width = show_img->height * .012;
+
+ if (0) {
+ width = pow(prob, 1. / 2.) * 10 + 1;
+ alphabet = 0;
+ }
+
+ printf("%s: %.0f%%\n", names[class_id], prob * 100);
+ int offset = class_id * 123457 % classes;
+ float red = get_color(2, offset, classes);
+ float green = get_color(1, offset, classes);
+ float blue = get_color(0, offset, classes);
+ float rgb[3];
+
+ //width = prob*20+2;
+
+ rgb[0] = red;
+ rgb[1] = green;
+ rgb[2] = blue;
+ box b = boxes[i];
+
+ int left = (b.x - b.w / 2.)*show_img->width;
+ int right = (b.x + b.w / 2.)*show_img->width;
+ int top = (b.y - b.h / 2.)*show_img->height;
+ int bot = (b.y + b.h / 2.)*show_img->height;
+
+ if (left < 0) left = 0;
+ if (right > show_img->width - 1) right = show_img->width - 1;
+ if (top < 0) top = 0;
+ if (bot > show_img->height - 1) bot = show_img->height - 1;
+
+ float const font_size = show_img->height / 1000.F;
+ CvPoint pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
+ pt1.x = left;
+ pt1.y = top;
+ pt2.x = right;
+ pt2.y = bot;
+ pt_text.x = left;
+ pt_text.y = top - 12;
+ pt_text_bg1.x = left;
+ pt_text_bg1.y = top - (10 + 25 * font_size);
+ pt_text_bg2.x = right;
+ pt_text_bg2.y = top;
+ CvScalar color;
+ color.val[0] = red * 256;
+ color.val[1] = green * 256;
+ color.val[2] = blue * 256;
+
+ cvRectangle(show_img, pt1, pt2, color, width, 8, 0);
+ //printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class_id, names[class_id]);
+ cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
+ cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0); // filled
+ CvScalar black_color;
+ black_color.val[0] = 0;
+ CvFont font;
+ cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, 0, font_size * 3, 8);
+ cvPutText(show_img, names[class_id], pt_text, &font, black_color);
+ }
+ }
+}
+
+// ====================================================================
+// Draw Loss & Accuracy chart
+// ====================================================================
+mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show)
+{
+ int img_offset = 50;
+ int draw_size = img_size - img_offset;
+ IplImage* img = cvCreateImage(cvSize(img_size, img_size), 8, 3);
+ cvSet(img, CV_RGB(255, 255, 255), 0);
+ CvPoint pt1, pt2, pt_text;
+ CvFont font;
+ cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 1, CV_AA);
+ char char_buff[100];
+ int i;
+ // vertical lines
+ pt1.x = img_offset; pt2.x = img_size, pt_text.x = 10;
+ for (i = 1; i <= number_of_lines; ++i) {
+ pt1.y = pt2.y = (float)i * draw_size / number_of_lines;
+ cvLine(img, pt1, pt2, CV_RGB(224, 224, 224), 1, 8, 0);
+ if (i % 10 == 0) {
+ sprintf(char_buff, "%2.1f", max_img_loss*(number_of_lines - i) / number_of_lines);
+ pt_text.y = pt1.y + 5;
+ cvPutText(img, char_buff, pt_text, &font, CV_RGB(0, 0, 0));
+ cvLine(img, pt1, pt2, CV_RGB(128, 128, 128), 1, 8, 0);
+ }
+ }
+ // horizontal lines
+ pt1.y = draw_size; pt2.y = 0, pt_text.y = draw_size + 15;
+ for (i = 0; i <= number_of_lines; ++i) {
+ pt1.x = pt2.x = img_offset + (float)i * draw_size / number_of_lines;
+ cvLine(img, pt1, pt2, CV_RGB(224, 224, 224), 1, 8, 0);
+ if (i % 10 == 0) {
+ sprintf(char_buff, "%d", max_batches * i / number_of_lines);
+ pt_text.x = pt1.x - 20;
+ cvPutText(img, char_buff, pt_text, &font, CV_RGB(0, 0, 0));
+ cvLine(img, pt1, pt2, CV_RGB(128, 128, 128), 1, 8, 0);
+ }
+ }
+
+ cvPutText(img, "Loss", cvPoint(0, 35), &font, CV_RGB(0, 0, 255));
+ cvPutText(img, "Iteration number", cvPoint(draw_size / 2, img_size - 10), &font, CV_RGB(0, 0, 0));
+ char max_batches_buff[100];
+ sprintf(max_batches_buff, "in cfg max_batches=%d", max_batches);
+ cvPutText(img, max_batches_buff, cvPoint(draw_size - 195, img_size - 10), &font, CV_RGB(0, 0, 0));
+ cvPutText(img, "Press 's' to save: chart.png", cvPoint(5, img_size - 10), &font, CV_RGB(0, 0, 0));
+ if (!dont_show) {
+ printf(" If error occurs - run training with flag: -dont_show \n");
+ cvNamedWindow("average loss", CV_WINDOW_NORMAL);
+ cvMoveWindow("average loss", 0, 0);
+ cvResizeWindow("average loss", img_size, img_size);
+ cvShowImage("average loss", img);
+ cvWaitKey(20);
+ }
+ return (mat_cv*)img;
+}
+// ----------------------------------------
+
+void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
+ float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port)
+{
+ IplImage* img = (IplImage*)img_src;
+ int img_offset = 50;
+ int draw_size = img_size - img_offset;
+ CvFont font;
+ cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 1, CV_AA);
+ char char_buff[100];
+ CvPoint pt1, pt2;
+ pt1.x = img_offset + draw_size * (float)current_batch / max_batches;
+ pt1.y = draw_size * (1 - avg_loss / max_img_loss);
+ if (pt1.y < 0) pt1.y = 1;
+ cvCircle(img, pt1, 1, CV_RGB(0, 0, 255), CV_FILLED, 8, 0);
+
+ // precision
+ if (draw_precision) {
+ static float old_precision = 0;
+ static int iteration_old = 0;
+ static int text_iteration_old = 0;
+ if (iteration_old == 0) cvPutText(img, accuracy_name, cvPoint(0, 12), &font, CV_RGB(255, 0, 0));
+
+ cvLine(img,
+ cvPoint(img_offset + draw_size * (float)iteration_old / max_batches, draw_size * (1 - old_precision)),
+ cvPoint(img_offset + draw_size * (float)current_batch / max_batches, draw_size * (1 - precision)),
+ CV_RGB(255, 0, 0), 1, 8, 0);
+
+ if (((int)(old_precision * 10) != (int)(precision * 10)) || (current_batch - text_iteration_old) >= max_batches / 10) {
+ text_iteration_old = current_batch;
+ sprintf(char_buff, "%2.0f%% ", precision * 100);
+ CvFont font3;
+ cvInitFont(&font3, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 5, CV_AA);
+ cvPutText(img, char_buff, cvPoint(pt1.x - 30, draw_size * (1 - precision) + 15), &font3, CV_RGB(255, 255, 255));
+
+ CvFont font2;
+ cvInitFont(&font2, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, 0, 1, CV_AA);
+ cvPutText(img, char_buff, cvPoint(pt1.x - 30, draw_size * (1 - precision) + 15), &font2, CV_RGB(200, 0, 0));
+ }
+ old_precision = precision;
+ iteration_old = current_batch;
+ }
+
+ sprintf(char_buff, "current avg loss = %2.4f iteration = %d", avg_loss, current_batch);
+ pt1.x = 55, pt1.y = 10;
+ pt2.x = pt1.x + 460, pt2.y = pt1.y + 20;
+ cvRectangle(img, pt1, pt2, CV_RGB(255, 255, 255), CV_FILLED, 8, 0);
+ pt1.y += 15;
+ cvPutText(img, char_buff, pt1, &font, CV_RGB(0, 0, 0));
+
+ int k = 0;
+ if (!dont_show) {
+ cvShowImage("average loss", img);
+ k = cvWaitKey(20);
+ }
+ if (k == 's' || current_batch == (max_batches - 1) || current_batch % 100 == 0) {
+ save_cv_png((mat_cv *)img, "chart.png");
+ cvPutText(img, "- Saved", cvPoint(250, img_size - 10), &font, CV_RGB(255, 0, 0));
+ }
+ else
+ cvPutText(img, "- Saved", cvPoint(250, img_size - 10), &font, CV_RGB(255, 255, 255));
+
+ if (mjpeg_port > 0) send_mjpeg((mat_cv *)img, mjpeg_port, 500000, 100);
+}
+// ----------------------------------------
+
+
+// ====================================================================
+// Data augmentation
+// ====================================================================
+image image_data_augmentation(mat_cv* ipl, int w, int h,
+ int pleft, int ptop, int swidth, int sheight, int flip,
+ float jitter, float dhue, float dsat, float dexp)
+{
+ image out;
+ try {
+ cv::Mat img = cv::cvarrToMat(ipl);
+
+ // crop
+ cv::Rect src_rect(pleft, ptop, swidth, sheight);
+ cv::Rect img_rect(cv::Point2i(0, 0), img.size());
+ cv::Rect new_src_rect = src_rect & img_rect;
+
+ cv::Rect dst_rect(cv::Point2i(std::max(0, -pleft), std::max(0, -ptop)), new_src_rect.size());
+
+ cv::Mat cropped(cv::Size(src_rect.width, src_rect.height), img.type());
+ cropped.setTo(cv::Scalar::all(0));
+
+ img(new_src_rect).copyTo(cropped(dst_rect));
+
+ // resize
+ cv::Mat sized;
+ cv::resize(cropped, sized, cv::Size(w, h), 0, 0, INTER_LINEAR);
+
+ // flip
+ if (flip) {
+ cv::flip(sized, cropped, 1); // 0 - x-axis, 1 - y-axis, -1 - both axes (x & y)
+ sized = cropped.clone();
+ }
+
+ // HSV augmentation
+ // CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
+ if (ipl->nChannels >= 3)
+ {
+ cv::Mat hsv_src;
+ cvtColor(sized, hsv_src, CV_BGR2HSV); // also BGR -> RGB
+
+ std::vector hsv;
+ cv::split(hsv_src, hsv);
+
+ hsv[1] *= dsat;
+ hsv[2] *= dexp;
+ hsv[0] += 179 * dhue;
+
+ cv::merge(hsv, hsv_src);
+
+ cvtColor(hsv_src, sized, CV_HSV2RGB); // now RGB instead of BGR
+ }
+ else
+ {
+ sized *= dexp;
+ }
+
+ //std::stringstream window_name;
+ //window_name << "augmentation - " << ipl;
+ //cv::imshow(window_name.str(), sized);
+ //cv::waitKey(0);
+
+ // Mat -> IplImage -> image
+ IplImage src = sized;
+ out = ipl_to_image(&src);
+ }
+ catch (...) {
+ cerr << "OpenCV can't augment image: " << w << " x " << h << " \n";
+ out = ipl_to_image(ipl);
+ }
+ return out;
+}
+
+// ====================================================================
+// Show Anchors
+// ====================================================================
+void show_acnhors(int number_of_boxes, int num_of_clusters, float *rel_width_height_array, model anchors_data, int width, int height)
+{
+ CvMat* labels = cvCreateMat(number_of_boxes, 1, CV_32SC1);
+ CvMat* points = cvCreateMat(number_of_boxes, 2, CV_32FC1);
+ CvMat* centers = cvCreateMat(num_of_clusters, 2, CV_32FC1);
+
+ int i, j;
+ for (i = 0; i < number_of_boxes; ++i) {
+ points->data.fl[i * 2] = rel_width_height_array[i * 2];
+ points->data.fl[i * 2 + 1] = rel_width_height_array[i * 2 + 1];
+ //cvSet1D(points, i * 2, cvScalar(rel_width_height_array[i * 2], 0, 0, 0));
+ //cvSet1D(points, i * 2 + 1, cvScalar(rel_width_height_array[i * 2 + 1], 0, 0, 0));
+ }
+
+ for (i = 0; i < num_of_clusters; ++i) {
+ centers->data.fl[i * 2] = anchors_data.centers.vals[i][0];
+ centers->data.fl[i * 2 + 1] = anchors_data.centers.vals[i][1];
+ }
+
+ for (i = 0; i < number_of_boxes; ++i) {
+ labels->data.i[i] = anchors_data.assignments[i];
+ }
+
+ size_t img_size = 700;
+ IplImage* img = cvCreateImage(cvSize(img_size, img_size), 8, 3);
+ cvZero(img);
+ for (i = 0; i < number_of_boxes; ++i) {
+ CvPoint pt;
+ pt.x = points->data.fl[i * 2] * img_size / width;
+ pt.y = points->data.fl[i * 2 + 1] * img_size / height;
+ int cluster_idx = labels->data.i[i];
+ int red_id = (cluster_idx * (uint64_t)123 + 55) % 255;
+ int green_id = (cluster_idx * (uint64_t)321 + 33) % 255;
+ int blue_id = (cluster_idx * (uint64_t)11 + 99) % 255;
+ cvCircle(img, pt, 1, CV_RGB(red_id, green_id, blue_id), CV_FILLED, 8, 0);
+ //if(pt.x > img_size || pt.y > img_size) printf("\n pt.x = %d, pt.y = %d \n", pt.x, pt.y);
+ }
+
+ for (j = 0; j < num_of_clusters; ++j) {
+ CvPoint pt1, pt2;
+ pt1.x = pt1.y = 0;
+ pt2.x = centers->data.fl[j * 2] * img_size / width;
+ pt2.y = centers->data.fl[j * 2 + 1] * img_size / height;
+ cvRectangle(img, pt1, pt2, CV_RGB(255, 255, 255), 1, 8, 0);
+ }
+ save_cv_png((mat_cv *)img, "cloud.png");
+ cvShowImage("clusters", img);
+ cvWaitKey(0);
+ cvReleaseImage(&img);
+ cvDestroyAllWindows();
+ cvReleaseMat(&labels);
+ cvReleaseMat(&points);
+ cvReleaseMat(¢ers);
+}
+
+} // extern "C"
+
+#endif // OPENCV
diff --git a/src/image_opencv.h b/src/image_opencv.h
new file mode 100644
index 00000000..a1fb4214
--- /dev/null
+++ b/src/image_opencv.h
@@ -0,0 +1,105 @@
+#ifndef IMAGE_OPENCV_H
+#define IMAGE_OPENCV_H
+
+#include "image.h"
+#include "matrix.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef OPENCV
+
+// declaration
+typedef struct mat_cv mat_cv;
+typedef struct cap_cv cap_cv;
+typedef struct write_cv write_cv;
+
+// cv::Mat / IplImage
+image load_image_cv(char *filename, int channels);
+mat_cv *load_image_mat_cv(const char *filename, int flag);
+image load_image_resize(char *filename, int w, int h, int c, image *im);
+int get_width_cv(mat_cv *ipl);
+int get_height_cv(mat_cv *ipl);
+void release_ipl(mat_cv **ipl);
+
+// image-to-ipl, ipl-to-image
+mat_cv *image_to_ipl(image im);
+image ipl_to_image_custom(mat_cv* src);
+// Mat image_to_mat(image im);
+// image mat_to_image(Mat m)
+
+// Window
+void create_window_cv(char const* window_name, int full_screen, int width, int height);
+void destroy_all_windows_cv();
+int wait_key_cv(int delay);
+int wait_until_press_key_cv();
+void make_window(char *name, int w, int h, int fullscreen);
+void show_image_cv(image p, const char *name);
+void show_image_cv_ipl(mat_cv *disp, const char *name);
+
+// Video Writer
+write_cv *create_video_writer(char *out_filename, char c1, char c2, char c3, char c4, int fps, int width, int height, int is_color);
+int write_frame_cv(write_cv *output_video_writer, mat_cv *show_img);
+void release_video_writer(write_cv **output_video_writer);
+
+
+//void *open_video_stream(const char *f, int c, int w, int h, int fps);
+//image get_image_from_stream(void *p);
+//image load_image_cv(char *filename, int channels);
+//int show_image_cv(image im, const char* name, int ms);
+
+// Video Capture
+cap_cv* get_capture_video_stream(const char *path);
+cap_cv* get_capture_webcam(int index);
+void release_capture(cap_cv* cap);
+
+mat_cv* get_capture_frame_cv(cap_cv *cap);
+int get_stream_fps_cpp_cv(cap_cv *cap);
+double get_capture_property_cv(cap_cv *cap, int property_id);
+double get_capture_frame_count_cv(cap_cv *cap);
+int set_capture_property_cv(cap_cv *cap, int property_id, double value);
+int set_capture_position_frame_cv(cap_cv *cap, int index);
+
+// ... Video Capture
+image get_image_from_stream(cap_cv *cap);
+image get_image_from_stream_cpp(cap_cv *cap);
+image get_image_from_stream_resize(cap_cv *cap, int w, int h, int c, mat_cv** in_img, int cpp_video_capture, int dont_close);
+image get_image_from_stream_letterbox(cap_cv *cap, int w, int h, int c, mat_cv** in_img, int cpp_video_capture, int dont_close);
+int get_stream_fps(cap_cv *cap, int cpp_video_capture);
+
+
+// Image Saving
+void save_cv_png(mat_cv *img, const char *name);
+void save_cv_jpg(mat_cv *img, const char *name);
+
+// Draw Detection
+void draw_detections_cv_v3(mat_cv* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output);
+void draw_detections_cv(mat_cv* show_img, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes);
+
+// Draw Loss & Accuracy chart
+mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size, int dont_show);
+void draw_train_loss(mat_cv* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
+ float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port);
+
+// Data augmentation
+image image_data_augmentation(mat_cv* ipl, int w, int h,
+ int pleft, int ptop, int swidth, int sheight, int flip,
+ float jitter, float dhue, float dsat, float dexp);
+
+// Show Anchors
+void show_acnhors(int number_of_boxes, int num_of_clusters, float *rel_width_height_array, model anchors_data, int width, int height);
+
+#else
+
+static int wait_key_cv(int delay) { return 0; }
+static int wait_until_press_key_cv() { return 0; }
+static void destroy_all_windows_cv() {}
+
+#endif // OPENCV
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // IMAGE_OPENCV_H
\ No newline at end of file
diff --git a/src/network.c b/src/network.c
index fd7cddfb..df2dea96 100644
--- a/src/network.c
+++ b/src/network.c
@@ -118,10 +118,10 @@ float get_current_rate(network net)
case SIG:
return net.learning_rate * (1./(1.+exp(net.gamma*(batch_num - net.step))));
case SGDR:
- rate = net.learning_rate_min +
+ rate = net.learning_rate_min +
0.5*(net.learning_rate-net.learning_rate_min)
* (1. + cos( (float) (batch_num % net.batches_per_cycle)*3.14159265 / net.batches_per_cycle));
-
+
return rate;
default:
fprintf(stderr, "Policy is weird!\n");
@@ -322,6 +322,11 @@ float train_network_sgd(network net, data d, int n)
}
float train_network(network net, data d)
+{
+ return train_network_waitkey(net, d, 0);
+}
+
+float train_network_waitkey(network net, data d, int is_main_thread)
{
assert(d.X.rows % net.batch == 0);
int batch = net.batch;
@@ -335,6 +340,7 @@ float train_network(network net, data d)
get_next_batch(d, batch, i*batch, X, y);
float err = train_network_datum(net, X, y);
sum += err;
+ wait_key_cv(5);
}
free(X);
free(y);
diff --git a/src/network.h b/src/network.h
index 764bb7da..222aca39 100644
--- a/src/network.h
+++ b/src/network.h
@@ -116,6 +116,7 @@ void backward_network(network net, network_state state);
void update_network(network net);
float train_network(network net, data d);
+float train_network_waitkey(network net, data d, int is_main_thread);
float train_network_batch(network net, data d, int n);
float train_network_sgd(network net, data d, int n);
float train_network_datum(network net, float *x, float *y);
diff --git a/src/network_kernels.cu b/src/network_kernels.cu
index 7aabc149..b5f0f7db 100644
--- a/src/network_kernels.cu
+++ b/src/network_kernels.cu
@@ -32,9 +32,9 @@
#include "shortcut_layer.h"
#include "blas.h"
-#ifdef OPENCV
-#include
-#endif
+//#ifdef OPENCV
+//#include
+//#endif
#include "http_stream.h"
@@ -160,15 +160,15 @@ void forward_backward_network_gpu(network net, float *x, float *y)
int i;
for (i = 0; i < net.n; ++i) {
layer l = net.layers[i];
- if (l.weights_gpu && l.weights_gpu16 && net.cudnn_half){
+ if (net.cudnn_half){
assert((l.c*l.n*l.size*l.size) > 0);
- if (l.type == CONVOLUTIONAL) {
+ if (l.type == CONVOLUTIONAL && l.weights_gpu && l.weights_gpu16) {
cuda_convert_f32_to_f16(l.weights_gpu, l.c*l.n*l.size*l.size, l.weights_gpu16);
}
- else if (l.type == CRNN) {
- //cuda_convert_f32_to_f16(l.input_layer->weights_gpu, l.input_layer->nweights, l.input_layer->weights_gpu16);
- //cuda_convert_f32_to_f16(l.self_layer->weights_gpu, l.self_layer->nweights, l.self_layer->weights_gpu16);
- //cuda_convert_f32_to_f16(l.output_layer->weights_gpu, l.output_layer->nweights, l.output_layer->weights_gpu16);
+ else if (l.type == CRNN && l.input_layer->weights_gpu && l.input_layer->weights_gpu16) {
+ cuda_convert_f32_to_f16(l.input_layer->weights_gpu, l.input_layer->nweights, l.input_layer->weights_gpu16);
+ cuda_convert_f32_to_f16(l.self_layer->weights_gpu, l.self_layer->nweights, l.self_layer->weights_gpu16);
+ cuda_convert_f32_to_f16(l.output_layer->weights_gpu, l.output_layer->nweights, l.output_layer->weights_gpu16);
}
}
}
diff --git a/src/nightmare.c b/src/nightmare.c
index bd8ccc3b..5c1ca04a 100644
--- a/src/nightmare.c
+++ b/src/nightmare.c
@@ -4,10 +4,6 @@
#include "blas.h"
#include "utils.h"
-#ifdef OPENCV
-#include
-#endif
-
// ./darknet nightmare cfg/extractor.recon.cfg ~/trained/yolo-coco.conv frame6.png -reconstruct -iters 500 -i 3 -lambda .1 -rate .01 -smooth 2
float abs_mean(float *x, int n)
@@ -268,7 +264,7 @@ void run_nightmare(int argc, char **argv)
//if ((n+1)%30 == 0) rate *= .5;
show_image(im, "reconstruction");
#ifdef OPENCV
- cvWaitKey(10);
+ wait_key_cv(10);
#endif
}else{
int layer = max_layer + rand()%range - range/2;
@@ -291,7 +287,7 @@ void run_nightmare(int argc, char **argv)
printf("%d %s\n", e, buff);
save_image(im, buff);
//show_image(im, buff);
- //cvWaitKey(0);
+ //wait_key_cv(0);
if(rotate){
image rot = rotate_image(im, rotate);
diff --git a/src/rnn.c b/src/rnn.c
index 5aee0863..6e1ed721 100644
--- a/src/rnn.c
+++ b/src/rnn.c
@@ -4,10 +4,6 @@
#include "blas.h"
#include "parser.h"
-#ifdef OPENCV
-#include
-#endif
-
typedef struct {
float *x;
float *y;
diff --git a/src/rnn_vid.c b/src/rnn_vid.c
index 7613ab25..ea3d64fc 100644
--- a/src/rnn_vid.c
+++ b/src/rnn_vid.c
@@ -5,14 +5,6 @@
#include "blas.h"
#ifdef OPENCV
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
-image get_image_from_stream(CvCapture *cap);
-image ipl_to_image(IplImage* src);
-
void reconstruct_picture(network net, float *features, image recon, image update, float rate, float momentum, float lambda, int smooth_size, int iters);
@@ -33,8 +25,8 @@ float_pair get_rnn_vid_data(network net, char **files, int n, int batch, int ste
int input_size = net.w*net.h*net.c;
float* input = (float*)calloc(input_size * net.batch, sizeof(float));
char *filename = files[rand()%n];
- CvCapture *cap = cvCaptureFromFile(filename);
- int frames = cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_COUNT);
+ cap_cv *cap = get_capture_video_stream(filename);
+ int frames = get_capture_frame_count_cv(cap);
int index = rand() % (frames - steps - 2);
if (frames < (steps + 4)){
--b;
@@ -43,12 +35,12 @@ float_pair get_rnn_vid_data(network net, char **files, int n, int batch, int ste
}
printf("frames: %d, index: %d\n", frames, index);
- cvSetCaptureProperty(cap, CV_CAP_PROP_POS_FRAMES, index);
+ set_capture_position_frame_cv(cap, index);
int i;
for(i = 0; i < net.batch; ++i){
- IplImage* src = cvQueryFrame(cap);
- image im = ipl_to_image(src);
+ mat_cv *src = get_capture_frame_cv(cap);
+ image im = ipl_to_image_custom(src);
rgbgr_image(im);
image re = resize_image(im, net.w, net.h);
//show_image(re, "loaded");
@@ -65,7 +57,7 @@ float_pair get_rnn_vid_data(network net, char **files, int n, int batch, int ste
memcpy(feats + (b + i*batch)*output_size, output + i*output_size, output_size*sizeof(float));
}
- cvReleaseCapture(&cap);
+ release_capture(cap); //cvReleaseCapture(&cap);
}
//printf("%d %d %d\n", out_im.w, out_im.h, out_im.c);
@@ -164,7 +156,8 @@ void generate_vid_rnn(char *cfgfile, char *weightfile)
set_batch_network(&net, 1);
int i;
- CvCapture* cap = cvCaptureFromFile("extra/vid/ILSVRC2015/Data/VID/snippets/val/ILSVRC2015_val_00007030.mp4");
+ cap_cv *cap = get_capture_video_stream("extra/vid/ILSVRC2015/Data/VID/snippets/val/ILSVRC2015_val_00007030.mp4");
+ //CvCapture* cap = cvCaptureFromFile("extra/vid/ILSVRC2015/Data/VID/snippets/val/ILSVRC2015_val_00007030.mp4");
float *feat;
float *next;
next = NULL;
diff --git a/src/super.c b/src/super.c
index 0f96fddb..35e7f6cf 100644
--- a/src/super.c
+++ b/src/super.c
@@ -3,9 +3,6 @@
#include "utils.h"
#include "parser.h"
-#ifdef OPENCV
-#include
-#endif
void train_super(char *cfgfile, char *weightfile)
{
diff --git a/src/swag.c b/src/swag.c
index d5020007..210f03f4 100644
--- a/src/swag.c
+++ b/src/swag.c
@@ -5,10 +5,6 @@
#include "parser.h"
#include "box.h"
-#ifdef OPENCV
-#include
-#endif
-
void train_swag(char *cfgfile, char *weightfile)
{
char *train_images = "data/voc.0712.trainval";
diff --git a/src/tag.c b/src/tag.c
index 0942d503..ec9327af 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -2,10 +2,6 @@
#include "utils.h"
#include "parser.h"
-#ifdef OPENCV
-#include
-#endif
-
void train_tag(char *cfgfile, char *weightfile, int clear)
{
srand(time(0));
diff --git a/src/voxel.c b/src/voxel.c
index 57fb17b6..ea7442b9 100644
--- a/src/voxel.c
+++ b/src/voxel.c
@@ -3,15 +3,6 @@
#include "utils.h"
#include "parser.h"
-#ifdef OPENCV
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
-image get_image_from_stream(CvCapture *cap);
-#endif
-
void extract_voxel(char *lfile, char *rfile, char *prefix)
{
#ifdef OPENCV
@@ -19,8 +10,8 @@ void extract_voxel(char *lfile, char *rfile, char *prefix)
int h = 1080;
int shift = 0;
int count = 0;
- CvCapture *lcap = cvCaptureFromFile(lfile);
- CvCapture *rcap = cvCaptureFromFile(rfile);
+ cap_cv *lcap = get_capture_video_stream(lfile);
+ cap_cv *rcap = get_capture_video_stream(rfile);
while(1){
image l = get_image_from_stream(lcap);
image r = get_image_from_stream(rcap);
diff --git a/src/writing.c b/src/writing.c
index e0d8019e..29785b7b 100644
--- a/src/writing.c
+++ b/src/writing.c
@@ -2,10 +2,6 @@
#include "utils.h"
#include "parser.h"
-#ifdef OPENCV
-#include
-#endif
-
void train_writing(char *cfgfile, char *weightfile)
{
char* backup_directory = "backup/";
@@ -122,10 +118,9 @@ void test_writing(char *cfgfile, char *weightfile, char *filename)
show_image(pred, "prediction");
show_image(im, "orig");
-#ifdef OPENCV
- cvWaitKey(0);
- cvDestroyAllWindows();
-#endif
+
+ wait_until_press_key_cv();
+ destroy_all_windows_cv();
free_image(upsampled);
free_image(thresh);
diff --git a/src/yolo.c b/src/yolo.c
index fc7d8a6e..07a2092c 100644
--- a/src/yolo.c
+++ b/src/yolo.c
@@ -6,15 +6,6 @@
#include "box.h"
#include "demo.h"
-#ifdef OPENCV
-#include
-#include
-#include
-#ifndef CV_VERSION_EPOCH
-#include
-#endif
-#endif
-
char *voc_names[] = {"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
void train_yolo(char *cfgfile, char *weightfile)
@@ -327,10 +318,10 @@ void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
free_image(im);
free_image(sized);
-#ifdef OPENCV
- cvWaitKey(0);
- cvDestroyAllWindows();
-#endif
+
+ wait_until_press_key_cv();
+ destroy_all_windows_cv();
+
if (filename) break;
}
}
diff --git a/src/yolo_v2_class.cpp b/src/yolo_v2_class.cpp
index ab582bad..6eaa38b7 100644
--- a/src/yolo_v2_class.cpp
+++ b/src/yolo_v2_class.cpp
@@ -384,53 +384,6 @@ LIB_API std::vector Detector::tracking_id(std::vector cur_bbox_v
}
-LIB_API bool Detector::send_json_http(std::vector cur_bbox_vec, std::vector obj_names, int frame_id, std::string filename, int timeout, int port)
-{
- //int timeout = 400000;
- //int port = 8070;
- //send_json(local_dets, local_nboxes, l.classes, demo_names, frame_id, demo_json_port, timeout);
-
- std::string send_str;
-
- char *tmp_buf = (char *)calloc(1024, sizeof(char));
- if (!filename.empty()) {
- sprintf(tmp_buf, "{\n \"frame_id\":%d, \n \"filename\":\"%s\", \n \"objects\": [ \n", frame_id, filename.c_str());
- }
- else {
- sprintf(tmp_buf, "{\n \"frame_id\":%d, \n \"objects\": [ \n", frame_id);
- }
- send_str = tmp_buf;
- free(tmp_buf);
-
- for (auto & i : cur_bbox_vec) {
- char *buf = (char *)calloc(2048, sizeof(char));
-
- sprintf(buf, " {\"class_id\":%d, \"name\":\"%s\", \"absolute_coordinates\":{\"center_x\":%d, \"center_y\":%d, \"width\":%d, \"height\":%d}, \"confidence\":%f",
- i.obj_id, obj_names[i.obj_id].c_str(), i.x, i.y, i.w, i.h, i.prob);
-
- //sprintf(buf, " {\"class_id\":%d, \"name\":\"%s\", \"relative_coordinates\":{\"center_x\":%f, \"center_y\":%f, \"width\":%f, \"height\":%f}, \"confidence\":%f",
- // i.obj_id, obj_names[i.obj_id], i.x, i.y, i.w, i.h, i.prob);
-
- send_str += buf;
-
- if (!std::isnan(i.z_3d)) {
- sprintf(buf, "\n , \"coordinates_in_meters\":{\"x_3d\":%.2f, \"y_3d\":%.2f, \"z_3d\":%.2f}",
- i.x_3d, i.y_3d, i.z_3d);
- send_str += buf;
- }
-
- send_str += "}\n";
-
- free(buf);
- }
-
- //send_str += "\n ] \n}, \n";
- send_str += "\n ] \n}";
-
- send_json_custom(send_str.c_str(), port, timeout);
- return true;
-}
-
void *Detector::get_cuda_context()
{
#ifdef GPU