派生自 libgowrapper/yolo

zhangmeng
2019-12-11 b63c8219736016e3b3952465f41abede37a38fbd
csrc/yolo.cpp
@@ -4,33 +4,16 @@
#include <fstream>
#include <vector>
#include <string>
#include <stdexcept>
#include "struct.h"
namespace cppyolo
{
    sdkyolo::sdkyolo(const char *cfg, const char *weights, const char *name, const int gpu)
    :det_(NULL)
    ,infos_(NULL)
    ,obj_cnt_(0)
    ,image_(NULL)
    ,width_(0)
    ,height_(0)
    ,channel_(0)
    {
        names_.clear();
        if (!init(cfg, weights, name, gpu)){
         throw std::runtime_error("init yolo error");
        }
    }
    static std::vector<std::string> names;
    sdkyolo::~sdkyolo()
    {
        if (det_) delete det_;
    }
    static void objects_names_from_file(std::string filename, std::vector<std::string> &names){
    static void objects_names_from_file(std::string filename){
        std::ifstream file(filename);
        if (!file.is_open()){
@@ -42,44 +25,41 @@
        printf("names count %d\n", names.size());
    }
    const char* sdkyolo::obj_name_by_type(const int typ)const{
        if(names_.empty() || typ < 0 || typ >= names_.size()) return NULL;
        return names_.at(typ).c_str();
    const char* obj_name_by_type(const int typ){
        if(names.empty() || typ < 0 || typ >= names.size()) return NULL;
        return names.at(typ).c_str();
    }
    bool sdkyolo::init(const char *cfg, const char *weights, const char *name, const int gpu){
        if (det_) return true;
    void *init(const char *cfg, const char *weights, const char *name, const int gpu){
        if(!cfg || !weights || !name){
            printf("init Detector error\n");
            return false;
        }
    
        if(names_.empty())
            objects_names_from_file(name, names_);
        if(names.empty())
            objects_names_from_file(name);
        det_ = new Detector(cfg, weights, gpu);
        return true;
        return new Detector(cfg, weights, gpu);
    }
    int sdkyolo::buffer_to_image(const unsigned char *data, const int w, const int h, const int color_channel){
    void release(void *handle){
        if (handle){
            Detector *d = (Detector*)handle;
            delete d;
        }
    }
    static 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;
        if (w != width_ || h != height_ || color_channel != channel_){
            if (image_){
                free(image_->data);
                delete image_;
            }
            image_ = new image_t;
            image_->h = h;
            image_->w = w;
            image_->c = c;
            image_->data = (float*)calloc(h*w*c, sizeof(float));
        }
        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;
    
@@ -87,9 +67,9 @@
        switch(c){
            case 1:{
                for (; count < size; ){
                    image_->data[count] =
                    image_->data[w*h + count] =
                    image_->data[w*h*2 + count] =
                    img->data[count] =
                    img->data[w*h + count] =
                    img->data[w*h*2 + count] =
                    (float)(srcData[count])/255.0;
    
                    ++count;
@@ -97,7 +77,7 @@
                break;
            }
            case 3:{
                float* desData = image_->data;
                float* desData = img->data;
    
                for(int i = 0;i<size;i++){
                    *(desData) = *(srcData + 2) /255.0f;
@@ -115,25 +95,24 @@
                break;
        }
       return 0;
       return img;
    }
    int sdkyolo::detect(const cIMAGE *img, const float thrsh, const bool use_mean, void **objs, int *objCount){
        if (!det_) return -1;
    int detect(void *handle, const cIMAGE *img, const float thrsh, const bool use_mean, void **objs, int *objCount){
        if (!handle || !img || img->width <= 0 || img->height <= 0 || !img->data) return -1;
        Detector *d = (Detector*)handle;
        const int color_channel = img->channel;
        buffer_to_image(img->data, img->width, img->height, color_channel);
        image_t *image = buffer_to_image(img->data, img->width, img->height, color_channel);
        
        std::vector<bbox_t> result_vec = det_->detect(*image_, thrsh, use_mean);
      // det->free_image(*im);
        // delete im;
        std::vector<bbox_t> result_vec = d->detect(*image, thrsh, use_mean);
      d->free_image(*image);
        delete image;
        if (obj_cnt_ < result_vec.size()){
            free(infos_);
            obj_cnt_ = result_vec.size();
            infos_ = (cObjInfo*)malloc(obj_cnt_ * sizeof(cObjInfo));
        cObjInfo *infos = NULL;
        if(!result_vec.empty()){
            infos = (cObjInfo*)malloc(result_vec.size() * sizeof(cObjInfo));
        }
        int count = 0;
@@ -147,11 +126,11 @@
         info.rcObj.right = i.x+i.w;
            info.rcObj.bottom = i.y+i.h;
            
            infos_[count++] = info;
            infos[count++] = info;
        }
        
        *objCount = count;
        *objs = infos_;
        *objs = infos;
        return count;
    }