Skip to content
This repository was archived by the owner on Jul 22, 2022. It is now read-only.

Commit 9658676

Browse files
authored
Merge pull request #14 from JdeRobot/issue13
I upload the latest version of darknet adapted
2 parents 2a8c0d2 + 82684cc commit 9658676

File tree

162 files changed

+44307
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+44307
-0
lines changed

darknet/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
*.o
2+
*.dSYM
3+
*.csv
4+
*.out
5+
*.png
6+
*.jpg
7+
*.pyc
8+
old/
9+
mnist/
10+
data/
11+
caffe/
12+
grasp/
13+
images/
14+
opencv/
15+
convnet/
16+
decaf/
17+
submission/
18+
cfg/
19+
darknet
20+
.fuse*
21+
22+
# OS Generated #
23+
.DS_Store*
24+
ehthumbs.db
25+
Icon?
26+
Thumbs.db
27+
*.swp

darknet/CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
3+
PROJECT(DARKNET C CXX)
4+
5+
####################################################################
6+
####################################################################
7+
###### DEFINE DARKNET CMAKE OPTIONS ######
8+
####################################################################
9+
####################################################################
10+
OPTION(BUILD_python "Build Python wrapper" ON)
11+
OPTION(USE_GPU "Compile with GPU support" ON)
12+
13+
SET(DARKNET_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/src ${CMAKE_CURRENT_LIST_DIR}/examples ${CMAKE_CURRENT_LIST_DIR}/)
14+
15+
IF (USE_GPU)
16+
add_definitions(-DGPU)
17+
ENDIF()
18+
19+
####################################################################
20+
####################################################################
21+
###### DEFINE LOCAL CMAKE REPO AND TOOLS ######
22+
####################################################################
23+
####################################################################
24+
25+
26+
IF (NOT DEFINED CMAKE_BUILD_TYPE)
27+
SET(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
28+
ELSEIF (CMAKE_BUILD_TYPE STREQUAL "" )
29+
SET(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
30+
ENDIF()
31+
32+
33+
####################################################################
34+
####################################################################
35+
36+
37+
38+
####################################################################
39+
###### DEPENDENCIES ######
40+
####################################################################
41+
# ---[ Python
42+
43+
44+
#Cuda
45+
FIND_PACKAGE(OpenCV REQUIRED)
46+
FIND_PACKAGE(Boost REQUIRED)
47+
48+
49+
IF(USE_GPU)
50+
FIND_PACKAGE(CUDA REQUIRED)
51+
ENDIF(USE_GPU)
52+
53+
54+
####################################################################
55+
####################################################################
56+
###### SET COMPILLER OPTIONS ######
57+
####################################################################
58+
####################################################################
59+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
60+
add_definitions(-fPIC)
61+
####################################################################
62+
####################################################################
63+
64+
set(LIB_DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" CACHE STRING "Library directory name" FORCE)
65+
set(HEADER_DESTINATION "${CMAKE_INSTALL_PREFIX}/include" CACHE STRING "include directory name" FORCE)
66+
67+
68+
add_subdirectory(src)
69+
add_subdirectory(examples)
70+
add_subdirectory(DarknetAPI)
71+
add_subdirectory(test)
72+
73+
add_custom_target(uninstall
74+
"${CMAKE_COMMAND}" -P "${CMAKE_SOURCE_DIR}/uninstall.cmake"
75+
)

darknet/DarknetAPI/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
if(UNIX)
3+
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; -Xcompiler -fPIC; -O3; -ccbin ${CMAKE_CXX_COMPILER})
4+
else()
5+
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}; -ccbin "\"${CMAKE_CXX_COMPILER}\"")
6+
endif()
7+
8+
9+
set (darknetAPI_SOURCES
10+
DarknetAPI
11+
DarknetAPIConversions
12+
DarknetDetection
13+
)
14+
15+
16+
17+
include_directories(${CMAKE_CURRENT_LIST_DIR}/../ ${CMAKE_CURRENT_LIST_DIR}/../include ${CMAKE_CURRENT_LIST_DIR}/../examples ${CMAKE_CURRENT_LIST_DIR}/../src ${CMAKE_CURRENT_LIST_DIR} ${OPENCV_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS} ${RAPIDJSON_INCLUDE_DIR})
18+
if(USE_GPU)
19+
cuda_add_library (darknetAPILib ${darknetAPI_SOURCES} ${gpu_SRCS} SHARED)
20+
else()
21+
add_library (darknetAPILib SHARED ${darknetAPI_SOURCES})
22+
endif(USE_GPU)
23+
24+
target_link_libraries(darknetAPILib darknetLib ${OpenCV_LIBRARIES})
25+
26+
install(TARGETS darknetAPILib LIBRARY DESTINATION ${LIB_DESTINATION} ARCHIVE DESTINATION ${LIB_DESTINATION} RUNTIME DESTINATION ${LIB_DESTINATION})
27+
28+
file(GLOB filesB *.h)
29+
file(GLOB filesC ../include/*.h)
30+
31+
32+
install(FILES ${filesB} DESTINATION ${HEADER_DESTINATION}/DarknetAPI)
33+
install(FILES ${filesC} DESTINATION ${HEADER_DESTINATION})

darknet/DarknetAPI/DarknetAPI.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//
2+
// Created by frivas on 25/04/16.
3+
//
4+
5+
#include "DarknetAPI.h"
6+
#include <iostream>
7+
#include "DarknetAPIConversions.h"
8+
9+
//char *voc_names2[] = {"gable_main", "hip_main", "hip_ext", "hipgable_main", "hipgable_ext",
10+
// "nested_turret"};
11+
//image voc_labels[6];
12+
13+
14+
char *voc_names2[] = {"None","motorcycle","car","van","bus","truck","small-truck","tank-truck"};
15+
image voc_labels[8];
16+
17+
18+
19+
20+
void addDetection(image& im, int num, float thresh, detection *dets, char **names, image *labels, int classes, DarknetDetections& detections){
21+
22+
int i,j;
23+
int classid;
24+
float prob;
25+
for(i = 0; i < num; ++i){
26+
char labelstr[4096] = {0};
27+
classid = -1;
28+
for(j = 0; j < classes; ++j){
29+
if (dets[i].prob[j] > thresh){
30+
if (classid < 0) {
31+
strcat(labelstr, names[j]);
32+
classid = j;
33+
prob = dets[i].prob[j];
34+
} else {
35+
strcat(labelstr, ", ");
36+
strcat(labelstr, names[j]);
37+
}
38+
printf("%s: %.0f%%\n", names[j], dets[i].prob[j]*100);
39+
}
40+
}
41+
if(classid >= 0){
42+
/*int width = pow(prob, 1./2.)*10+1;
43+
int offset = classid*17 % classes;*/
44+
box b = dets[i].bbox;
45+
46+
int left = (b.x-b.w/2.)*im.w;
47+
int right = (b.x+b.w/2.)*im.w;
48+
int top = (b.y-b.h/2.)*im.h;
49+
int bot = (b.y+b.h/2.)*im.h;
50+
51+
if(left < 0) left = 0;
52+
if(right > im.w-1) right = im.w-1;
53+
if(top < 0) top = 0;
54+
if(bot > im.h-1) bot = im.h-1;
55+
56+
box detectedbox;
57+
58+
detectedbox.x = left;
59+
detectedbox.y = top;
60+
detectedbox.h = bot - top;
61+
detectedbox.w = right - left;
62+
63+
// std::cout<<"---------------------:" << detectedbox.x<<" "<<detectedbox.y<<" "<<detectedbox.w<<" "<<detectedbox.h<<std::endl;
64+
DarknetDetection detection(detectedbox,classid,prob);
65+
66+
detections.push_back(detection);
67+
68+
69+
}
70+
}
71+
}
72+
73+
74+
DarknetDetections processImageDetection(network *net,image& im, float thresh ){
75+
76+
float hier_thresh=0.5;
77+
78+
79+
DarknetDetections detections;
80+
81+
c_set_batch_network(net, 1);
82+
srand(2222222);
83+
clock_t time;
84+
char buff[256];
85+
char *input = buff;
86+
int j;
87+
float nms = .3;
88+
89+
image sized = c_letterbox_image(im, net->w, net->h);
90+
layer l = net->layers[net->n-1];
91+
92+
93+
float *X = sized.data;
94+
time=clock();
95+
c_network_predict(net, X);
96+
printf("%s: Predicted in %f seconds.\n", input, c_sec(clock()-time));
97+
int nboxes = 0;
98+
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
99+
/*c_get_region_boxes(l, im.w, im.h, net->w, net->h, thresh, probs, boxes, masks, 0, 0, hier_thresh, 1);*/
100+
if (l.softmax_tree && nms)
101+
c_do_nms_obj(dets, nboxes, l.classes, nms);
102+
else if (nms)
103+
c_do_nms_sort(dets, nboxes, l.classes, nms);
104+
105+
106+
107+
addDetection( im, nboxes, thresh, dets, voc_names2, 0, l.classes, detections );
108+
c_free_image(sized);
109+
110+
return detections;
111+
112+
}
113+
114+
115+
DarknetDetections processImageDetection(network *net,const cv::Mat & im, float thresh){
116+
image imDark= cv_to_image(im);
117+
auto detection= processImageDetection(net,imDark,thresh);
118+
c_free_image(imDark);
119+
return detection;
120+
121+
}
122+
123+
124+
125+
DarknetAPI::DarknetAPI(char *cfgfile, char *weightfile) {
126+
net = c_parse_network_cfg(cfgfile);
127+
if (weightfile != nullptr) {
128+
c_load_weights(net, weightfile);
129+
}
130+
}
131+
DarknetAPI::~DarknetAPI(){
132+
c_free_network(net);
133+
}
134+
135+
136+
DarknetDetections DarknetAPI::process(image& im, float thresh ){
137+
return processImageDetection(this->net,im,thresh);
138+
}
139+
140+
DarknetDetections DarknetAPI::process(const cv::Mat &im, float thresh) {
141+
image imDark= cv_to_image(im);
142+
return processImageDetection(this->net,imDark,thresh);
143+
}
144+
145+
std::string DarknetAPI::processToJson(const cv::Mat &im, float thresh) {
146+
return process(im,thresh).serialize();
147+
}

darknet/DarknetAPI/DarknetAPI.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Created by frivas on 25/04/16.
3+
//
4+
5+
#ifndef DARKNET_DARKNETAPI_H
6+
#define DARKNET_DARKNETAPI_H
7+
8+
9+
#include "parser.h"
10+
#include "detection_layer.h"
11+
#include "utils.h"
12+
#include <DarknetAPI/DarknetDetection.h>
13+
14+
#include <vector>
15+
#include <iostream>
16+
17+
extern "C" void c_test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh);
18+
extern "C" network *c_parse_network_cfg(char *filename);
19+
extern "C" void c_load_weights(network *net, char *filename);
20+
extern "C" void c_set_batch_network(network *net, int b);
21+
extern "C" image c_load_image_color(char *filename, int w, int h);
22+
extern "C" image c_resize_image(image im, int w, int h);
23+
extern "C" float *c_network_predict(network *net, float *input);
24+
extern "C" float c_sec(clock_t clocks);
25+
extern "C" void c_get_detection_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness);
26+
extern "C" void c_do_nms_sort(detection *dets, int total, int classes, float thresh);
27+
extern "C" void c_draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image *labels, int classes);
28+
extern "C" void c_show_image(image p, const char *name);
29+
extern "C" void c_save_image(image im, const char *name);
30+
extern "C" void c_free_image(image m);
31+
extern "C" float c_get_pixel(image m, int x, int y, int c);
32+
extern "C" image c_make_image(int w, int h, int c);
33+
extern "C" image c_copy_image(image p);
34+
extern "C" void c_rgbgr_image(image im);
35+
extern "C" void c_free_network(network *net);
36+
extern "C" int c_max_index(float *a, int n);
37+
extern "C" void c_do_nms_obj(detection *dets, int total, int classes, float thresh);
38+
//extern "C" void c_get_region_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness, int *map, float tree_thresh);
39+
extern "C" void c_get_region_boxes(layer l, int w, int h, int netw, int neth, float thresh, float **probs, box *boxes, float **masks, int only_objectness, int *map, float tree_thresh, int relative);
40+
extern "C" image c_letterbox_image(image im, int w, int h);
41+
42+
43+
44+
45+
46+
void addDetection(int num, float thresh, box *boxes, float **probs, char **names, image *labels, int classes, DarknetDetections& detections);
47+
DarknetDetections processImageDetection(network *net, image& im, float thresh = 0.24);
48+
DarknetDetections processImageDetection(network *net,const cv::Mat & im, float thresh = 0.24);
49+
50+
51+
52+
class DarknetAPI {
53+
54+
network *net;
55+
56+
57+
public:
58+
59+
DarknetAPI(char *cfgfile, char *weightfile);
60+
61+
~DarknetAPI();
62+
63+
DarknetDetections process(image& im, float thresh = 0.24);
64+
DarknetDetections process(const cv::Mat& im, float thresh = 0.24);
65+
std::string processToJson(const cv::Mat& im, float thresh = 0.24);
66+
};
67+
68+
69+
70+
#endif //DARKNET_DARKNETAPI_H

0 commit comments

Comments
 (0)