From 1b388e4b0207003630c326ba1e71af8c8746070f Mon Sep 17 00:00:00 2001 From: chenshijun <csj_sky@126.com> Date: 星期二, 22 十月 2019 15:14:23 +0800 Subject: [PATCH] Merge branch 'master' of ssh://192.168.5.5:29418/valib/gosdk --- csrc/buz/yolo/detector.cpp | 139 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 139 insertions(+), 0 deletions(-) diff --git a/csrc/buz/yolo/detector.cpp b/csrc/buz/yolo/detector.cpp new file mode 100644 index 0000000..1ca2bfe --- /dev/null +++ b/csrc/buz/yolo/detector.cpp @@ -0,0 +1,139 @@ +#include "detector.h" + +#include <stdlib.h> +#include <fstream> + +#include <sys/time.h> + +#include "csdk_struct.h" + +#include "yolo.hpp" + +namespace csdk_wrap{ + + static std::vector<std::string> names; + + static void objects_names_from_file(std::string filename) { + std::ifstream file(filename); + + if (!file.is_open()){ + printf("open %s file error\n", filename.c_str()); + return; + } + for(std::string line; getline(file, line);) names.push_back(line); + + printf("names count %d\n", names.size()); + } + + void* init_yolo_detector(const char *cfg, const char *weights, const char *name, + const int gpu_index, VecFunc &vec){ + + if(!cfg || !weights || !name){ + printf("init Detector error\n"); + return NULL; + } + + if(names.empty()) + objects_names_from_file(name); + + auto det = new Detector(cfg, weights, gpu_index); + vec.emplace_back([det]{delete det;}); + + return det; + } + + image_t* buffer_to_image(const unsigned char *data, const int w, const int h, const int color_channel){ + int size = w*h; + int size2 = size*2; + + int c = color_channel; + image_t *img = new image_t; + img->h = h; + img->w = w; + img->c = color_channel; + img->data = (float*)calloc(h*w*c, sizeof(float)); + // image im = make_image(w, h, c); + const unsigned char *srcData = data; + + int count = 0; + switch(c){ + case 1:{ + for (; count < size; ){ + img->data[count] = + img->data[w*h + count] = + img->data[w*h*2 + count] = + (float)(srcData[count])/255.0; + + ++count; + } + break; + } + case 3:{ + float* desData = img->data; + + for(int i = 0;i<size;i++){ + *(desData) = *(srcData + 2) /255.0f; + *(desData+size) = *(srcData + 1) /255.0f; + *(desData+size2) = *(srcData) /255.0f; + + desData++; + srcData+=3; + } + break; + } + + default: + printf("Channel number not supported.\n"); + break; + } + + return img; + } + + cObjInfo* yolo_detect(void *handle,int *objCount, const cIMAGE *img, const float thrsh, const bool use_mean){ + Detector *det = (Detector*)handle; + + const int color_channel = img->channel; + image_t* im = buffer_to_image(img->data, img->width, img->height, color_channel); + + // struct timeval b,e; + // gettimeofday(&b, NULL); + + std::vector<bbox_t> result_vec = det->detect(*im, thrsh, use_mean); + det->free_image(*im); + delete im; + + // gettimeofday(&e,NULL); + // double t = e.tv_sec*1000.0 + e.tv_usec/1000.0 - b.tv_sec*1000.0-b.tv_usec/1000.0; + // printf("lib yolo detect use time %f ms\n", t); + + cObjInfo *infos = NULL; + if(!result_vec.empty()){ + infos = (cObjInfo*)malloc(result_vec.size() * sizeof(cObjInfo)); + } + + int count = 0; + for(auto &i : result_vec){ + + cObjInfo info; + info.typ = i.obj_id; + info.prob = i.prob; + info.rcObj.left = i.x; + info.rcObj.top = i.y; + info.rcObj.right = i.x+i.w; + info.rcObj.bottom = i.y+i.h; + + infos[count++] = info; + } + + *objCount = count; + + return infos; + } + + const char* yolo_obj_name_by_type(const int typ){ + if(names.empty() || typ < 0 || typ >= names.size()) return NULL; + return names.at(typ).c_str(); + } +} + -- Gitblit v1.8.0