1 #include "darknet.h"
2 #include "yolo_v2_class.hpp"
3 
4 #include "network.h"
5 
6 extern "C" {
7 #include "detection_layer.h"
8 #include "region_layer.h"
9 #include "cost_layer.h"
10 #include "utils.h"
11 #include "parser.h"
12 #include "box.h"
13 #include "image.h"
14 #include "demo.h"
15 #include "option_list.h"
16 #include "stb_image.h"
17 }
18 //#include <sys/time.h>
19 
20 #include <vector>
21 #include <iostream>
22 #include <algorithm>
23 #include <cmath>
24 
25 #define NFRAMES 3
26 
27 //static Detector* detector = NULL;
28 static std::unique_ptr<Detector> detector;
29 
init(const char * configurationFilename,const char * weightsFilename,int gpu)30 int init(const char *configurationFilename, const char *weightsFilename, int gpu)
31 {
32     detector.reset(new Detector(configurationFilename, weightsFilename, gpu));
33     return 1;
34 }
35 
detect_image(const char * filename,bbox_t_container & container)36 int detect_image(const char *filename, bbox_t_container &container)
37 {
38     std::vector<bbox_t> detection = detector->detect(filename);
39     for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i)
40         container.candidates[i] = detection[i];
41     return detection.size();
42 }
43 
detect_mat(const uint8_t * data,const size_t data_length,bbox_t_container & container)44 int detect_mat(const uint8_t* data, const size_t data_length, bbox_t_container &container) {
45 #ifdef OPENCV
46     std::vector<char> vdata(data, data + data_length);
47     cv::Mat image = imdecode(cv::Mat(vdata), 1);
48 
49     std::vector<bbox_t> detection = detector->detect(image);
50     for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i)
51         container.candidates[i] = detection[i];
52     return detection.size();
53 #else
54     return -1;
55 #endif    // OPENCV
56 }
57 
dispose()58 int dispose() {
59     //if (detector != NULL) delete detector;
60     //detector = NULL;
61     detector.reset();
62     return 1;
63 }
64 
get_device_count()65 int get_device_count() {
66 #ifdef GPU
67     int count = 0;
68     cudaGetDeviceCount(&count);
69     return count;
70 #else
71     return -1;
72 #endif	// GPU
73 }
74 
built_with_cuda()75 bool built_with_cuda(){
76 #ifdef GPU
77     return true;
78 #else
79     return false;
80 #endif
81 }
82 
built_with_cudnn()83 bool built_with_cudnn(){
84 #ifdef CUDNN
85     return true;
86 #else
87     return false;
88 #endif
89 }
90 
built_with_opencv()91 bool built_with_opencv(){
92 #ifdef OPENCV
93     return true;
94 #else
95     return false;
96 #endif
97 }
98 
99 
get_device_name(int gpu,char * deviceName)100 int get_device_name(int gpu, char* deviceName) {
101 #ifdef GPU
102     cudaDeviceProp prop;
103     cudaGetDeviceProperties(&prop, gpu);
104     std::string result = prop.name;
105     std::copy(result.begin(), result.end(), deviceName);
106     return 1;
107 #else
108     return -1;
109 #endif	// GPU
110 }
111 
112 #ifdef GPU
check_cuda(cudaError_t status)113 void check_cuda(cudaError_t status) {
114     if (status != cudaSuccess) {
115         const char *s = cudaGetErrorString(status);
116         printf("CUDA Error Prev: %s\n", s);
117     }
118 }
119 #endif
120 
121 struct detector_gpu_t {
122     network net;
123     image images[NFRAMES];
124     float *avg;
125     float* predictions[NFRAMES];
126     int demo_index;
127     unsigned int *track_id;
128 };
129 
Detector(std::string cfg_filename,std::string weight_filename,int gpu_id)130 LIB_API Detector::Detector(std::string cfg_filename, std::string weight_filename, int gpu_id) : cur_gpu_id(gpu_id)
131 {
132     wait_stream = 0;
133 #ifdef GPU
134     int old_gpu_index;
135     check_cuda( cudaGetDevice(&old_gpu_index) );
136 #endif
137 
138     detector_gpu_ptr = std::make_shared<detector_gpu_t>();
139     detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
140 
141 #ifdef GPU
142     //check_cuda( cudaSetDevice(cur_gpu_id) );
143     cuda_set_device(cur_gpu_id);
144     printf(" Used GPU %d \n", cur_gpu_id);
145 #endif
146     network &net = detector_gpu.net;
147     net.gpu_index = cur_gpu_id;
148     //gpu_index = i;
149 
150     _cfg_filename = cfg_filename;
151     _weight_filename = weight_filename;
152 
153     char *cfgfile = const_cast<char *>(_cfg_filename.c_str());
154     char *weightfile = const_cast<char *>(_weight_filename.c_str());
155 
156     net = parse_network_cfg_custom(cfgfile, 1, 1);
157     if (weightfile) {
158         load_weights(&net, weightfile);
159     }
160     set_batch_network(&net, 1);
161     net.gpu_index = cur_gpu_id;
162     fuse_conv_batchnorm(net);
163 
164     layer l = net.layers[net.n - 1];
165     int j;
166 
167     detector_gpu.avg = (float *)calloc(l.outputs, sizeof(float));
168     for (j = 0; j < NFRAMES; ++j) detector_gpu.predictions[j] = (float*)calloc(l.outputs, sizeof(float));
169     for (j = 0; j < NFRAMES; ++j) detector_gpu.images[j] = make_image(1, 1, 3);
170 
171     detector_gpu.track_id = (unsigned int *)calloc(l.classes, sizeof(unsigned int));
172     for (j = 0; j < l.classes; ++j) detector_gpu.track_id[j] = 1;
173 
174 #ifdef GPU
175     check_cuda( cudaSetDevice(old_gpu_index) );
176 #endif
177 }
178 
179 
~Detector()180 LIB_API Detector::~Detector()
181 {
182     detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
183     //layer l = detector_gpu.net.layers[detector_gpu.net.n - 1];
184 
185     free(detector_gpu.track_id);
186 
187     free(detector_gpu.avg);
188     for (int j = 0; j < NFRAMES; ++j) free(detector_gpu.predictions[j]);
189     for (int j = 0; j < NFRAMES; ++j) if (detector_gpu.images[j].data) free(detector_gpu.images[j].data);
190 
191 #ifdef GPU
192     int old_gpu_index;
193     cudaGetDevice(&old_gpu_index);
194     cuda_set_device(detector_gpu.net.gpu_index);
195 #endif
196 
197     free_network(detector_gpu.net);
198 
199 #ifdef GPU
200     cudaSetDevice(old_gpu_index);
201 #endif
202 }
203 
get_net_width() const204 LIB_API int Detector::get_net_width() const {
205     detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
206     return detector_gpu.net.w;
207 }
get_net_height() const208 LIB_API int Detector::get_net_height() const {
209     detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
210     return detector_gpu.net.h;
211 }
get_net_color_depth() const212 LIB_API int Detector::get_net_color_depth() const {
213     detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
214     return detector_gpu.net.c;
215 }
216 
217 
detect(std::string image_filename,float thresh,bool use_mean)218 LIB_API std::vector<bbox_t> Detector::detect(std::string image_filename, float thresh, bool use_mean)
219 {
220     std::shared_ptr<image_t> image_ptr(new image_t, [](image_t *img) { if (img->data) free(img->data); delete img; });
221     *image_ptr = load_image(image_filename);
222     return detect(*image_ptr, thresh, use_mean);
223 }
224 
load_image_stb(char * filename,int channels)225 static image load_image_stb(char *filename, int channels)
226 {
227     int w, h, c;
228     unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
229     if (!data)
230         throw std::runtime_error("file not found");
231     if (channels) c = channels;
232     int i, j, k;
233     image im = make_image(w, h, c);
234     for (k = 0; k < c; ++k) {
235         for (j = 0; j < h; ++j) {
236             for (i = 0; i < w; ++i) {
237                 int dst_index = i + w*j + w*h*k;
238                 int src_index = k + c*i + c*w*j;
239                 im.data[dst_index] = (float)data[src_index] / 255.;
240             }
241         }
242     }
243     free(data);
244     return im;
245 }
246 
load_image(std::string image_filename)247 LIB_API image_t Detector::load_image(std::string image_filename)
248 {
249     char *input = const_cast<char *>(image_filename.c_str());
250     image im = load_image_stb(input, 3);
251 
252     image_t img;
253     img.c = im.c;
254     img.data = im.data;
255     img.h = im.h;
256     img.w = im.w;
257 
258     return img;
259 }
260 
261 
free_image(image_t m)262 LIB_API void Detector::free_image(image_t m)
263 {
264     if (m.data) {
265         free(m.data);
266     }
267 }
268 
detect(image_t img,float thresh,bool use_mean)269 LIB_API std::vector<bbox_t> Detector::detect(image_t img, float thresh, bool use_mean)
270 {
271     detector_gpu_t &detector_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
272     network &net = detector_gpu.net;
273 #ifdef GPU
274     int old_gpu_index;
275     cudaGetDevice(&old_gpu_index);
276     if(cur_gpu_id != old_gpu_index)
277         cudaSetDevice(net.gpu_index);
278 
279     net.wait_stream = wait_stream;    // 1 - wait CUDA-stream, 0 - not to wait
280 #endif
281     //std::cout << "net.gpu_index = " << net.gpu_index << std::endl;
282 
283     image im;
284     im.c = img.c;
285     im.data = img.data;
286     im.h = img.h;
287     im.w = img.w;
288 
289     image sized;
290 
291     if (net.w == im.w && net.h == im.h) {
292         sized = make_image(im.w, im.h, im.c);
293         memcpy(sized.data, im.data, im.w*im.h*im.c * sizeof(float));
294     }
295     else
296         sized = resize_image(im, net.w, net.h);
297 
298     layer l = net.layers[net.n - 1];
299 
300     float *X = sized.data;
301 
302     float *prediction = network_predict(net, X);
303 
304     if (use_mean) {
305         memcpy(detector_gpu.predictions[detector_gpu.demo_index], prediction, l.outputs * sizeof(float));
306         mean_arrays(detector_gpu.predictions, NFRAMES, l.outputs, detector_gpu.avg);
307         l.output = detector_gpu.avg;
308         detector_gpu.demo_index = (detector_gpu.demo_index + 1) % NFRAMES;
309     }
310     //get_region_boxes(l, 1, 1, thresh, detector_gpu.probs, detector_gpu.boxes, 0, 0);
311     //if (nms) do_nms_sort(detector_gpu.boxes, detector_gpu.probs, l.w*l.h*l.n, l.classes, nms);
312 
313     int nboxes = 0;
314     int letterbox = 0;
315     float hier_thresh = 0.5;
316     detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);
317     if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
318 
319     std::vector<bbox_t> bbox_vec;
320 
321     for (int i = 0; i < nboxes; ++i) {
322         box b = dets[i].bbox;
323         int const obj_id = max_index(dets[i].prob, l.classes);
324         float const prob = dets[i].prob[obj_id];
325 
326         if (prob > thresh)
327         {
328             bbox_t bbox;
329             bbox.x = std::max((double)0, (b.x - b.w / 2.)*im.w);
330             bbox.y = std::max((double)0, (b.y - b.h / 2.)*im.h);
331             bbox.w = b.w*im.w;
332             bbox.h = b.h*im.h;
333             bbox.obj_id = obj_id;
334             bbox.prob = prob;
335             bbox.track_id = 0;
336             bbox.frames_counter = 0;
337             bbox.x_3d = NAN;
338             bbox.y_3d = NAN;
339             bbox.z_3d = NAN;
340 
341             bbox_vec.push_back(bbox);
342         }
343     }
344 
345     free_detections(dets, nboxes);
346     if(sized.data)
347         free(sized.data);
348 
349 #ifdef GPU
350     if (cur_gpu_id != old_gpu_index)
351         cudaSetDevice(old_gpu_index);
352 #endif
353 
354     return bbox_vec;
355 }
356 
tracking_id(std::vector<bbox_t> cur_bbox_vec,bool const change_history,int const frames_story,int const max_dist)357 LIB_API std::vector<bbox_t> Detector::tracking_id(std::vector<bbox_t> cur_bbox_vec, bool const change_history,
358     int const frames_story, int const max_dist)
359 {
360     detector_gpu_t &det_gpu = *static_cast<detector_gpu_t *>(detector_gpu_ptr.get());
361 
362     bool prev_track_id_present = false;
363     for (auto &i : prev_bbox_vec_deque)
364         if (i.size() > 0) prev_track_id_present = true;
365 
366     if (!prev_track_id_present) {
367         for (size_t i = 0; i < cur_bbox_vec.size(); ++i)
368             cur_bbox_vec[i].track_id = det_gpu.track_id[cur_bbox_vec[i].obj_id]++;
369         prev_bbox_vec_deque.push_front(cur_bbox_vec);
370         if (prev_bbox_vec_deque.size() > frames_story) prev_bbox_vec_deque.pop_back();
371         return cur_bbox_vec;
372     }
373 
374     std::vector<unsigned int> dist_vec(cur_bbox_vec.size(), std::numeric_limits<unsigned int>::max());
375 
376     for (auto &prev_bbox_vec : prev_bbox_vec_deque) {
377         for (auto &i : prev_bbox_vec) {
378             int cur_index = -1;
379             for (size_t m = 0; m < cur_bbox_vec.size(); ++m) {
380                 bbox_t const& k = cur_bbox_vec[m];
381                 if (i.obj_id == k.obj_id) {
382                     float center_x_diff = (float)(i.x + i.w/2) - (float)(k.x + k.w/2);
383                     float center_y_diff = (float)(i.y + i.h/2) - (float)(k.y + k.h/2);
384                     unsigned int cur_dist = sqrt(center_x_diff*center_x_diff + center_y_diff*center_y_diff);
385                     if (cur_dist < max_dist && (k.track_id == 0 || dist_vec[m] > cur_dist)) {
386                         dist_vec[m] = cur_dist;
387                         cur_index = m;
388                     }
389                 }
390             }
391 
392             bool track_id_absent = !std::any_of(cur_bbox_vec.begin(), cur_bbox_vec.end(),
393                 [&i](bbox_t const& b) { return b.track_id == i.track_id && b.obj_id == i.obj_id; });
394 
395             if (cur_index >= 0 && track_id_absent){
396                 cur_bbox_vec[cur_index].track_id = i.track_id;
397                 cur_bbox_vec[cur_index].w = (cur_bbox_vec[cur_index].w + i.w) / 2;
398                 cur_bbox_vec[cur_index].h = (cur_bbox_vec[cur_index].h + i.h) / 2;
399             }
400         }
401     }
402 
403     for (size_t i = 0; i < cur_bbox_vec.size(); ++i)
404         if (cur_bbox_vec[i].track_id == 0)
405             cur_bbox_vec[i].track_id = det_gpu.track_id[cur_bbox_vec[i].obj_id]++;
406 
407     if (change_history) {
408         prev_bbox_vec_deque.push_front(cur_bbox_vec);
409         if (prev_bbox_vec_deque.size() > frames_story) prev_bbox_vec_deque.pop_back();
410     }
411 
412     return cur_bbox_vec;
413 }
414 
415 
get_cuda_context()416 void *Detector::get_cuda_context()
417 {
418 #ifdef GPU
419     int old_gpu_index;
420     cudaGetDevice(&old_gpu_index);
421     if (cur_gpu_id != old_gpu_index)
422         cudaSetDevice(cur_gpu_id);
423 
424     void *cuda_context = cuda_get_context();
425 
426     if (cur_gpu_id != old_gpu_index)
427         cudaSetDevice(old_gpu_index);
428 
429     return cuda_context;
430 #else   // GPU
431     return NULL;
432 #endif  // GPU
433 }