Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opencv4, now working #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "darknet"]
path = darknet
url = https://github.com/pjreddie/darknet
url = https://github.com/kunaltyagi/darknet
2 changes: 1 addition & 1 deletion darknet
Submodule darknet updated 120 files
76 changes: 39 additions & 37 deletions darknet_ros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
25 changes: 12 additions & 13 deletions darknet_ros/include/darknet_ros/YoloObjectDetector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/time.h>
}

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 {

Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -239,7 +238,7 @@ class YoloObjectDetector

void yolo();

IplImageWithHeader_ getIplImageWithHeader();
MatWithHeader_ getIplImageWithHeader();

bool getImageStatus(void);

Expand Down
3 changes: 2 additions & 1 deletion darknet_ros/include/darknet_ros/image_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#define IMAGE_INTERFACE_H

#include "image.h"
#include <opencv2/core.hpp>

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
27 changes: 13 additions & 14 deletions darknet_ros/src/YoloObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -408,9 +408,9 @@ void *YoloObjectDetector::fetchInThread()
{
{
boost::shared_lock<boost::shared_mutex> 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_;
}
Expand All @@ -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;
Expand Down Expand Up @@ -504,9 +503,9 @@ void YoloObjectDetector::yolo()

{
boost::shared_lock<boost::shared_mutex> 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]);
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down