Video Analysis底层库拆分,sdk的go封装
chenshijun
2019-10-22 1b388e4b0207003630c326ba1e71af8c8746070f
csrc/buz/yolo/detector.cpp
New file
@@ -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();
    }
}