前言
darknet是一个基于C写出的框架,yolov3速度快,定位准确率比较高,很适合落地到自己的工程中。那么如何调用编译好的darknet.so动态链接库呢?
正文
首先需要去darknet官网下载好darknet-master,然后进行编译,编译的时候最好选择GPU格式的,这样在后面监测的时候速度会非常快。
好啦,直接上代码吧~代码主要包含两个头文件和两个源文件。
imprcoess.h
#ifndef IMPROCESS_H
#define IMPROCESS_H
#include<opencv2/opencv.hpp>
void imgConvert(const cv::Mat& img, float* dst);
void imgResize(float* src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);
void resizeInner(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight);
#endif // IMPROCESS_H
imprcoess.cpp
#include<improcess.h>
void imgConvert(const cv::Mat& img, float* dst){
uchar *data = img.data;
int h = img.rows;
int w = img.cols;
int c = img.channels();
for(int k= 0; k < c; ++k){
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
dst[k*w*h+i*w+j] = data[(i*w + j)*c + k]/255.;
}
}
}
}
void imgResize(float *src, float* dst,int srcWidth,int srcHeight,int dstWidth,int dstHeight){
int new_w = srcWidth;
int new_h = srcHeight;
if (((float)dstWidth/srcWidth) < ((float)dstHeight/srcHeight)) {
new_w = dstWidth;
new_h = (srcHeight * dstWidth)/srcWidth;
} else {
new_h = dstHeight;
new_w = (srcWidth * dstHeight)/srcHeight;
}
float* ImgReInner;
size_t sizeInner=new_w*new_h*3*sizeof(float);
ImgReInner=(float*)malloc(sizeInner);
resizeInner(src,ImgReInner,srcWidth,srcHeight,new_w,new_h);
for(int i=0;i<dstWidth*dstHeight*3;i++){
dst[i]=0.5;
}
for(int k = 0; k < 3; ++k){
for(int y = 0; y < new_h; ++y){
for(int x = 0; x < new_w; ++x){
float val = ImgReInner[k*new_w*new_h+y*new_w+x];
dst[k*dstHeight*dstWidth