From 256ea5050a866dbd3973046428dcaeecc390a148 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Thu, 28 Nov 2019 18:00:19 +0900 Subject: [PATCH] OpenCV4 compilation success --- .gitmodules | 2 +- darknet | 2 +- darknet_ros/CMakeLists.txt | 76 ++++++++++--------- .../darknet_ros/YoloObjectDetector.hpp | 25 +++--- .../include/darknet_ros/image_interface.h | 3 +- darknet_ros/src/YoloObjectDetector.cpp | 27 ++++--- ...{image_interface.c => image_interface.cpp} | 6 +- 7 files changed, 71 insertions(+), 70 deletions(-) rename darknet_ros/src/{image_interface.c => image_interface.cpp} (86%) diff --git a/.gitmodules b/.gitmodules index 000b92b76..02b80612e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "darknet"] path = darknet - url = https://github.com/pjreddie/darknet + url = https://github.com/kunaltyagi/darknet diff --git a/darknet b/darknet index 508381b37..cb82c21fb 160000 --- a/darknet +++ b/darknet @@ -1 +1 @@ -Subproject commit 508381b37fe75e0e1a01bcb2941cb0b31eb0e4c9 +Subproject commit cb82c21fbbaf06177e49ea20660487b1fbe08d54 diff --git a/darknet_ros/CMakeLists.txt b/darknet_ros/CMakeLists.txt index c571a4baa..4bd199246 100644 --- a/darknet_ros/CMakeLists.txt +++ b/darknet_ros/CMakeLists.txt @@ -2,8 +2,7 @@ cmake_minimum_required(VERSION 2.8.12) project(darknet_ros) # Set c++11 cmake flags -set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") -set(CMAKE_C_FLAGS "-Wall -Wno-unused-result -Wno-unknown-pragmas -Wno-unused-variable -Wfatal-errors -fPIC ${CMAKE_C_FLAGS}") +set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wno-unused-result -Wno-unknown-pragmas -Wno-unused-variable -fPIC -fpermissive ${CMAKE_CXX_FLAGS}") # Define path of darknet folder here. find_path(DARKNET_PATH @@ -89,42 +88,43 @@ include_directories( ) set(PROJECT_LIB_FILES - src/YoloObjectDetector.cpp src/image_interface.c + src/YoloObjectDetector.cpp src/image_interface.cpp ) set(DARKNET_CORE_FILES - ${DARKNET_PATH}/src/activation_layer.c ${DARKNET_PATH}/src/im2col.c - ${DARKNET_PATH}/src/activations.c ${DARKNET_PATH}/src/image.c - ${DARKNET_PATH}/src/avgpool_layer.c ${DARKNET_PATH}/src/layer.c - ${DARKNET_PATH}/src/batchnorm_layer.c ${DARKNET_PATH}/src/list.c - ${DARKNET_PATH}/src/blas.c ${DARKNET_PATH}/src/local_layer.c - ${DARKNET_PATH}/src/box.c ${DARKNET_PATH}/src/lstm_layer.c - ${DARKNET_PATH}/src/col2im.c ${DARKNET_PATH}/src/matrix.c - ${DARKNET_PATH}/src/connected_layer.c ${DARKNET_PATH}/src/maxpool_layer.c - ${DARKNET_PATH}/src/convolutional_layer.c ${DARKNET_PATH}/src/network.c - ${DARKNET_PATH}/src/cost_layer.c ${DARKNET_PATH}/src/normalization_layer.c - ${DARKNET_PATH}/src/crnn_layer.c ${DARKNET_PATH}/src/option_list.c - ${DARKNET_PATH}/src/crop_layer.c ${DARKNET_PATH}/src/parser.c - ${DARKNET_PATH}/src/cuda.c ${DARKNET_PATH}/src/region_layer.c - ${DARKNET_PATH}/src/data.c ${DARKNET_PATH}/src/reorg_layer.c - ${DARKNET_PATH}/src/deconvolutional_layer.c ${DARKNET_PATH}/src/rnn_layer.c - ${DARKNET_PATH}/src/demo.c ${DARKNET_PATH}/src/route_layer.c - ${DARKNET_PATH}/src/detection_layer.c ${DARKNET_PATH}/src/shortcut_layer.c - ${DARKNET_PATH}/src/dropout_layer.c ${DARKNET_PATH}/src/softmax_layer.c - ${DARKNET_PATH}/src/gemm.c ${DARKNET_PATH}/src/tree.c - ${DARKNET_PATH}/src/gru_layer.c ${DARKNET_PATH}/src/utils.c - ${DARKNET_PATH}/src/upsample_layer.c ${DARKNET_PATH}/src/logistic_layer.c - ${DARKNET_PATH}/src/l2norm_layer.c ${DARKNET_PATH}/src/yolo_layer.c - - ${DARKNET_PATH}/examples/art.c ${DARKNET_PATH}/examples/lsd.c - ${DARKNET_PATH}/examples/attention.c ${DARKNET_PATH}/examples/nightmare.c - ${DARKNET_PATH}/examples/captcha.c ${DARKNET_PATH}/examples/regressor.c - ${DARKNET_PATH}/examples/cifar.c ${DARKNET_PATH}/examples/rnn.c - ${DARKNET_PATH}/examples/classifier.c ${DARKNET_PATH}/examples/segmenter.c - ${DARKNET_PATH}/examples/coco.c ${DARKNET_PATH}/examples/super.c - ${DARKNET_PATH}/examples/darknet.c ${DARKNET_PATH}/examples/tag.c - ${DARKNET_PATH}/examples/detector.c ${DARKNET_PATH}/examples/yolo.c - ${DARKNET_PATH}/examples/go.c + ${DARKNET_PATH}/src/activation_layer.cpp ${DARKNET_PATH}/src/activations.cpp + ${DARKNET_PATH}/src/avgpool_layer.cpp ${DARKNET_PATH}/src/batchnorm_layer.cpp + ${DARKNET_PATH}/src/blas.cpp ${DARKNET_PATH}/src/box.cpp + ${DARKNET_PATH}/src/col2im.cpp ${DARKNET_PATH}/src/connected_layer.cpp + ${DARKNET_PATH}/src/convolutional_layer.cpp ${DARKNET_PATH}/src/cost_layer.cpp + ${DARKNET_PATH}/src/crnn_layer.cpp ${DARKNET_PATH}/src/crop_layer.cpp + ${DARKNET_PATH}/src/cuda.cpp ${DARKNET_PATH}/src/data.cpp + ${DARKNET_PATH}/src/deconvolutional_layer.cpp ${DARKNET_PATH}/src/demo.cpp + ${DARKNET_PATH}/src/detection_layer.cpp ${DARKNET_PATH}/src/dropout_layer.cpp + ${DARKNET_PATH}/src/gemm.cpp ${DARKNET_PATH}/src/gru_layer.cpp + ${DARKNET_PATH}/src/im2col.cpp ${DARKNET_PATH}/src/image.cpp + ${DARKNET_PATH}/src/image_opencv.cpp ${DARKNET_PATH}/src/iseg_layer.cpp + ${DARKNET_PATH}/src/l2norm_layer.cpp ${DARKNET_PATH}/src/layer.cpp + ${DARKNET_PATH}/src/list.cpp ${DARKNET_PATH}/src/local_layer.cpp + ${DARKNET_PATH}/src/logistic_layer.cpp ${DARKNET_PATH}/src/lstm_layer.cpp + ${DARKNET_PATH}/src/matrix.cpp ${DARKNET_PATH}/src/maxpool_layer.cpp + ${DARKNET_PATH}/src/network.cpp ${DARKNET_PATH}/src/normalization_layer.cpp + ${DARKNET_PATH}/src/option_list.cpp ${DARKNET_PATH}/src/parser.cpp + ${DARKNET_PATH}/src/region_layer.cpp ${DARKNET_PATH}/src/reorg_layer.cpp + ${DARKNET_PATH}/src/rnn_layer.cpp ${DARKNET_PATH}/src/route_layer.cpp + ${DARKNET_PATH}/src/shortcut_layer.cpp ${DARKNET_PATH}/src/softmax_layer.cpp + ${DARKNET_PATH}/src/tree.cpp ${DARKNET_PATH}/src/upsample_layer.cpp + ${DARKNET_PATH}/src/utils.cpp ${DARKNET_PATH}/src/yolo_layer.cpp + + ${DARKNET_PATH}/examples/art.cpp ${DARKNET_PATH}/examples/attention.cpp + ${DARKNET_PATH}/examples/captcha.cpp ${DARKNET_PATH}/examples/cifar.cpp + ${DARKNET_PATH}/examples/classifier.cpp ${DARKNET_PATH}/examples/coco.cpp + ${DARKNET_PATH}/examples/darknet.cpp ${DARKNET_PATH}/examples/detector.cpp + ${DARKNET_PATH}/examples/go.cpp ${DARKNET_PATH}/examples/instance_segmenter.cpp + ${DARKNET_PATH}/examples/lsd.cpp ${DARKNET_PATH}/examples/nightmare.cpp + ${DARKNET_PATH}/examples/regressor.cpp ${DARKNET_PATH}/examples/rnn.cpp + ${DARKNET_PATH}/examples/segmenter.cpp ${DARKNET_PATH}/examples/super.cpp + ${DARKNET_PATH}/examples/tag.cpp ${DARKNET_PATH}/examples/yolo.cpp ) set(DARKNET_CUDA_FILES @@ -169,10 +169,12 @@ else() endif() +set_target_properties(${PROJECT_NAME}_lib PROPERTIES + LINKER_LANGUAGE CXX +) + target_link_libraries(${PROJECT_NAME}_lib m - pthread - stdc++ ${Boost_LIBRARIES} ${OpenCV_LIBRARIES} ${catkin_LIBRARIES} diff --git a/darknet_ros/include/darknet_ros/YoloObjectDetector.hpp b/darknet_ros/include/darknet_ros/YoloObjectDetector.hpp index f1ab50a77..3724c67dc 100644 --- a/darknet_ros/include/darknet_ros/YoloObjectDetector.hpp +++ b/darknet_ros/include/darknet_ros/YoloObjectDetector.hpp @@ -45,21 +45,16 @@ #include "cublas_v2.h" #endif -extern "C" { #include "network.h" #include "detection_layer.h" #include "region_layer.h" #include "cost_layer.h" #include "utils.h" #include "parser.h" +#include "image.h" #include "box.h" #include "darknet_ros/image_interface.h" #include -} - -extern "C" void ipl_into_image(IplImage* src, image im); -extern "C" image ipl_to_image(IplImage* src); -extern "C" void show_image_cv(image p, const char *name, IplImage *disp); namespace darknet_ros { @@ -70,11 +65,15 @@ typedef struct int num, Class; } RosBox_; -typedef struct -{ - IplImage* image; - std_msgs::Header header; -} IplImageWithHeader_; +struct MatWithHeader_ { + cv::Mat image; + std_msgs::Header header; + + MatWithHeader_() = default; + MatWithHeader_(cv::Mat img, std_msgs::Header hdr): + image(img.clone()), header(hdr) {} +}; + class YoloObjectDetector { @@ -177,7 +176,7 @@ class YoloObjectDetector image buffLetter_[3]; int buffId_[3]; int buffIndex_ = 0; - IplImage * ipl_; + cv::Mat ipl_; float fps_ = 0; float demoThresh_ = 0; float demoHier_ = .5; @@ -239,7 +238,7 @@ class YoloObjectDetector void yolo(); - IplImageWithHeader_ getIplImageWithHeader(); + MatWithHeader_ getIplImageWithHeader(); bool getImageStatus(void); diff --git a/darknet_ros/include/darknet_ros/image_interface.h b/darknet_ros/include/darknet_ros/image_interface.h index 460549591..b61df2edd 100644 --- a/darknet_ros/include/darknet_ros/image_interface.h +++ b/darknet_ros/include/darknet_ros/image_interface.h @@ -10,9 +10,10 @@ #define IMAGE_INTERFACE_H #include "image.h" +#include static float get_pixel(image m, int x, int y, int c); image **load_alphabet_with_file(char *datafile); -void generate_image(image p, IplImage *disp); +void generate_image(image p, cv::Mat disp); #endif diff --git a/darknet_ros/src/YoloObjectDetector.cpp b/darknet_ros/src/YoloObjectDetector.cpp index 936a1e4a8..3254ce890 100644 --- a/darknet_ros/src/YoloObjectDetector.cpp +++ b/darknet_ros/src/YoloObjectDetector.cpp @@ -27,9 +27,9 @@ char **detectionNames; YoloObjectDetector::YoloObjectDetector(ros::NodeHandle nh) : nodeHandle_(nh), - imageTransport_(nodeHandle_), numClasses_(0), classLabels_(0), + imageTransport_(nodeHandle_), rosBoxes_(0), rosBoxCounter_(0) { @@ -408,9 +408,9 @@ void *YoloObjectDetector::fetchInThread() { { boost::shared_lock lock(mutexImageCallback_); - IplImageWithHeader_ imageAndHeader = getIplImageWithHeader(); - IplImage* ROS_img = imageAndHeader.image; - ipl_into_image(ROS_img, buff_[buffIndex_]); + MatWithHeader_ imageAndHeader = getIplImageWithHeader(); + cv::Mat ROS_img = imageAndHeader.image; + mat_to_image(ROS_img, buff_ + buffIndex_); headerBuff_[buffIndex_] = imageAndHeader.header; buffId_[buffIndex_] = actionId_; } @@ -421,8 +421,7 @@ void *YoloObjectDetector::fetchInThread() void *YoloObjectDetector::displayInThread(void *ptr) { - show_image_cv(buff_[(buffIndex_ + 1)%3], "YOLO V3", ipl_); - int c = cv::waitKey(waitKeyDelay_); + int c = show_image(buff_[(buffIndex_ + 1)%3], "YOLO V3", waitKeyDelay_); if (c != -1) c = c%256; if (c == 27) { demoDone_ = 1; @@ -504,9 +503,9 @@ void YoloObjectDetector::yolo() { boost::shared_lock lock(mutexImageCallback_); - IplImageWithHeader_ imageAndHeader = getIplImageWithHeader(); - IplImage* ROS_img = imageAndHeader.image; - buff_[0] = ipl_to_image(ROS_img); + MatWithHeader_ imageAndHeader = getIplImageWithHeader(); + cv::Mat ROS_img = imageAndHeader.image; + buff_[0] = mat_to_image(ROS_img); headerBuff_[0] = imageAndHeader.header; } buff_[1] = copy_image(buff_[0]); @@ -516,7 +515,8 @@ void YoloObjectDetector::yolo() buffLetter_[0] = letterbox_image(buff_[0], net_->w, net_->h); buffLetter_[1] = letterbox_image(buff_[0], net_->w, net_->h); buffLetter_[2] = letterbox_image(buff_[0], net_->w, net_->h); - ipl_ = cvCreateImage(cvSize(buff_[0].w, buff_[0].h), IPL_DEPTH_8U, buff_[0].c); + ipl_ = cv::Mat(buff_[0].h, buff_[0].w, + CV_8UC(buff_[0].c)); int count = 0; @@ -560,10 +560,9 @@ void YoloObjectDetector::yolo() } -IplImageWithHeader_ YoloObjectDetector::getIplImageWithHeader() +MatWithHeader_ YoloObjectDetector::getIplImageWithHeader() { - IplImage* ROS_img = new IplImage(camImageCopy_); - IplImageWithHeader_ header = {.image = ROS_img, .header = imageHeader_}; + MatWithHeader_ header {camImageCopy_, imageHeader_}; return header; } @@ -582,7 +581,7 @@ bool YoloObjectDetector::isNodeRunning(void) void *YoloObjectDetector::publishInThread() { // Publish image. - cv::Mat cvImage = cv::cvarrToMat(ipl_); + cv::Mat cvImage = ipl_; if (!publishDetectionImage(cv::Mat(cvImage))) { ROS_DEBUG("Detection image has not been broadcasted."); } diff --git a/darknet_ros/src/image_interface.c b/darknet_ros/src/image_interface.cpp similarity index 86% rename from darknet_ros/src/image_interface.c rename to darknet_ros/src/image_interface.cpp index 7fe9d9d53..84c53fba7 100644 --- a/darknet_ros/src/image_interface.c +++ b/darknet_ros/src/image_interface.cpp @@ -34,17 +34,17 @@ image **load_alphabet_with_file(char *datafile) { } #ifdef OPENCV -void generate_image(image p, IplImage *disp) +void generate_image(image p, cv::Mat disp) { int x,y,k; if(p.c == 3) rgbgr_image(p); //normalize_image(copy); - int step = disp->widthStep; + int step = disp.step[0]; 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(p,x,y,k)*255); + disp.data[y*step + x*p.c + k] = (unsigned char)(get_pixel(p,x,y,k)*255); } } }