#include "yolo.h"
|
|
#include "yolo.hpp"
|
|
#include <fstream>
|
#include <vector>
|
#include <string>
|
#include <stdexcept>
|
|
#include "struct.h"
|
|
namespace cppyolo
|
{
|
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());
|
}
|
|
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();
|
}
|
|
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);
|
|
return new Detector(cfg, weights, gpu);
|
}
|
|
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;
|
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;
|
}
|
|
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;
|
image_t *image = buffer_to_image(img->data, img->width, img->height, color_channel);
|
|
std::vector<bbox_t> result_vec = d->detect(*image, thrsh, use_mean);
|
d->free_image(*image);
|
delete image;
|
|
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;
|
*objs = infos;
|
|
return count;
|
}
|
|
} // namespace cppyolo
|