赞
踩
darknet是一个较为轻型的完全基于C与CUDA的开源深度学习框架,其主要特点就是容易安装,没有任何依赖项(OpenCV都可以不用),移植性非常好,支持CPU与GPU两种计算方式。
相比于TensorFlow来说,darknet并没有那么强大,但这也成了darknet的优势:
darknet完全由C语言实现,没有任何依赖项,当然可以使用OpenCV,但只是用其来显示图片、为了更好的可视化;
darknet支持CPU(所以没有GPU也不用紧的)与GPU(CUDA/cuDNN,使用GPU当然更块更好了);
正是因为其较为轻型,没有像TensorFlow那般强大的API,所以给我的感觉就是有另一种味道的灵活性,适合用来研究底层,可以更为方便的从底层对其进行改进与扩展;
darknet的实现与caffe的实现存在相似的地方,熟悉了darknet,相信对上手caffe有帮助
官方darknet源码下载网址:https://github.com/pjreddie/darknet
官方权重下载地址:https://pjreddie.com/media/files/yolov3_tiny.weights
csdn下载地址:yolov3-tiny 权重文件
主要参考:详细步骤!使用yolov3-tiny训练,测试、验证VOC数据集
https://blog.csdn.net/xiao_lxl/article/details/85047103
下载后,首先查看makefile
可以根据自己实际情况进行更改,用下面命令进行编译
make clean;make all
编译完成后生成darknet 可执行文件及动态库。
如下:
文件夹说明
1.cfg文件夹内是一些模型的架构,每个cfg文件类似与caffe的prototxt文件,通过该文件定义的整个模型的架构
2.data文件夹内放置了一些label文件,如coco9k的类别名等,和一些样例图(该文件夹主要为演示用,或者是直接训练coco等对应数据集时有用,如果要用自己的数据自行训练,该文件夹内的东西都不是我们需要的)
3.src文件夹内全是最底层的框架定义文件,所有层的定义等最基本的函数全部在该文件夹内,可以理解为该文件夹就是框架的源码;
4.examples文件夹是更为高层的一些函数,如检测函数,识别函数等,这些函数直接调用了底层的函数,我们经常使用的就是example中的函数;
5.include文件夹,顾名思义,存放头文件的地方;
6.python文件夹里是使用python对模型的调用方法,基本都在darknet.py中。当然,要实现python的调用,还需要用到darknet的动态库libdarknet.so,这个动态库稍后再介绍;
7.scripts文件夹中是一些脚本,如下载coco数据集,将voc格式的数据集转换为训练所需格式的脚本等。
8.除了license文件,剩下的就是Makefile文件,如下图,在问价开头有一些选项,把你需要使用的选项设为1即可。
./darknet detector test cfg/coco.data cfg/yolov3-tiny.cfg weights/yolov3-tiny.weights data/dog.jpg
出现上述的结果,说明运行正常。
待续。。。
参考:
https://blog.csdn.net/qq_28663849/article/details/107362445
CPU版本
首先打开makefile中,特别是OPENMP和AVX,速度快
OPENCV=1
AVX=1
OPENMP=1
参考:
原文链接:https://blog.csdn.net/stjuliet/article/details/87884976
#include <iostream> #define OPENCV //#define GPU #include "yolo_v2_class.hpp" //引用动态链接库中的头文件 #include <opencv2/opencv.hpp> #include "opencv2/highgui/highgui.hpp" //#pragma comment(lib, "opencv_world340d.lib") //引入OpenCV链接库 //#pragma comment(lib, "yolo_cpp_dll_no_gpu.lib") //引入YOLO动态链接库 #pragma comment(lib, "yolo_cpp_dll.lib") //引入YOLO动态链接库 //以下两段代码来自yolo_console_dll.sln void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names, int current_det_fps , int current_cap_fps ) { int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } }; for (auto &i : result_vec) { cv::Scalar color = obj_id_to_color(i.obj_id); cv::rectangle(mat_img, cv::Rect(i.x, i.y, i.w, i.h), color, 2); if (obj_names.size() > i.obj_id) { std::string obj_name = obj_names[i.obj_id]; if (i.track_id > 0) obj_name += " - " + std::to_string(i.track_id); cv::Size const text_size = getTextSize(obj_name, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0); int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2); cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 30, 0)), cv::Point2f(std::min((int)i.x + max_width, mat_img.cols - 1), std::min((int)i.y, mat_img.rows - 1)), color, CV_FILLED, 8, 0); putText(mat_img, obj_name, cv::Point2f(i.x, i.y - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(0, 0, 0), 2); } } if (current_det_fps >= 0) { std::string fps_str = "FPS detection: " + std::to_string(current_det_fps) ; putText(mat_img, fps_str, cv::Point2f(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2); } if (current_cap_fps >= 0) { std::string fps_str = "FPS capture: " + std::to_string(current_cap_fps); putText(mat_img, fps_str, cv::Point2f(10, 50), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2); } } std::vector<std::string> objects_names_from_file(std::string const filename) { std::ifstream file(filename); std::vector<std::string> file_lines; if (!file.is_open()) return file_lines; for (std::string line; getline(file, line);) file_lines.push_back(line); std::cout << "object names loaded \n"; return file_lines; } int main() { std::string names_file = "coco.names"; std::string cfg_file = "yolov3-tiny.cfg"; std::string weights_file = "yolov3-tiny.weights"; Detector detector(cfg_file, weights_file, 0); //初始化检测器 //std::vector<std::string> obj_names = objects_names_from_file(names_file); //调用获得分类对象名称 //或者使用以下四行代码也可实现读入分类对象文件 std::vector<std::string> obj_names; std::ifstream ifs(names_file.c_str()); std::string line; while (getline(ifs, line)) obj_names.push_back(line); //测试是否成功读入分类对象文件 for (size_t i = 0; i < obj_names.size(); i++) { std::cout << obj_names[i] << std::endl; } cv::VideoCapture capture; capture.open("bsd.mp4"); if (!capture.isOpened()) { printf("文件打开失败"); } cv::Mat frame; float fps = 0; while (true) { int begin, duration; begin = clock(); capture >> frame; //if (frame.empty() == NULL) //{ // printf("Image is empty"); // return 0; //} duration = clock() - begin; //printf("读图用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); //cv::resize(frame, frame, cv::Size(640,360)); std::vector<bbox_t> result_vec = detector.detect(frame); duration = clock() - begin; printf("识别用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); fps =(float) 1000.0 / duration; draw_boxes(frame, result_vec, obj_names,fps, fps); //duration = clock() - begin; //printf("画框用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); //cv::namedWindow("bsd", 0); //cv::imshow("bsd", frame); //cv::waitKey(1); //duration = clock() - begin; //printf("显示用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); printf("\n\n\n"); } return 0; }
/***************************************************************************************** 1、盲区检测 2021.3.17: *****************************************************************************************/ #define OPENCV/*必须放置在链接的头文件前面*/ //#define GPU #include <iostream> #include <stdlib.h> //srand()和rand()函数 #include<windows.h> #include <fstream> #include "yolo_v2_class.hpp" //引用动态链接库中的头文件 /*opencv相关*/ #include <opencv2/core/core.hpp> #include <opencv2/opencv.hpp> #include "opencv2/highgui/highgui.hpp" #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/objdetect/objdetect.hpp> #include <opencv2/ml/ml.hpp> #include <opencv2\imgproc\types_c.h> using namespace cv; using namespace std; /**************** 打印相关组件 start ! ********************************************************************/ #define debug_show_picture 1 //是否显示部分调试图片,方便调试 /*打印等级,修改后面的宏定义可以改变函数输出打印等级*/ #define ALG_PRTINT_LEVER PRINT_LEVEL_UNLIMIT #define ALG_PRTINT(...) SAL_printf(__VA_ARGS__) #define ALG_PRT(...) ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_UNLIMIT, __VA_ARGS__) #define ALG_DBG(...) ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_DBG, __VA_ARGS__) #define ALG_WAR(...) ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_WRN, __VA_ARGS__) #define ALG_ERR(...) ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_ERR, __VA_ARGS__) /*********************************************************************************************** * @enum HAT_SAL_PRT_LEVEL_E * @brief 打印输出的等级 ***************************************************************************************************/ typedef enum _PRT_LEVEL_E_ { PRINT_LEVEL_ERR = 0, /*错误打印信息*/ PRINT_LEVEL_WRN = 1, /*警告打印信息*/ PRINT_LEVEL_DBG = 2, /*调试打印信息*/ PRINT_LEVEL_UNLIMIT = 3, /*无限制打印信息*/ PRINT_LEVEL_NOPRT = 4, /*没有打印信息*/ } PRT_LEVEL_E; /******************************************************************************* Function: SAL_printf Description: 该函数能够通过设置的打印等级ALG_PRTINT_LEVER,来控制是否输出相关语句 Input: Output: Return: 0: Successful ohters: Failed *******************************************************************************/ void SAL_printf(const char *pFun, UINT line, PRT_LEVEL_E levelParam, const char *fmt, ...) { static INT8 g_printfInfo[4][16] = { "ERR", "WAR", "DBG", "INF" }; va_list p; if (ALG_PRTINT_LEVER == PRINT_LEVEL_NOPRT || levelParam == PRINT_LEVEL_NOPRT) { return; } if (levelParam <= ALG_PRTINT_LEVER) { va_start(p, fmt); printf("[DIYUN_ALG][%s][%s][%4d] ", g_printfInfo[levelParam], pFun, line); vprintf(fmt, p); va_end(p); } } /****************************** 打印相关组件 end *************************************************************************/ //#pragma comment(lib, "opencv_world340d.lib") //引入OpenCV链接库 //#pragma comment(lib, "yolo_cpp_dll_no_gpu.lib") //引入YOLO动态链接库 #pragma comment(lib, "yolo_cpp_dll.lib") //引入YOLO动态链接库 #ifdef max #undef max #endif #ifdef min #undef min #endif //以下两段代码来自yolo_console_dll.sln void draw_boxes(Mat mat_img, vector<bbox_t> result_vec, vector<string> obj_names, int current_det_fps , int current_cap_fps ) { int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } }; for (auto &i : result_vec) { Scalar color = obj_id_to_color(i.obj_id); rectangle(mat_img, Rect(i.x, i.y, i.w, i.h), color, 2); if (obj_names.size() > i.obj_id) { string obj_name = obj_names[i.obj_id]; if (i.track_id > 0) obj_name += " - " + to_string(i.track_id); Size const text_size = getTextSize(obj_name, FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0); int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2); rectangle(mat_img, Point2f(max((int)i.x - 1, 0), max((int)i.y - 30, 0)), Point2f(min((int)i.x + max_width, mat_img.cols - 1), min((int)i.y, mat_img.rows - 1)), color, CV_FILLED, 8, 0); putText(mat_img, obj_name, Point2f(i.x, i.y - 10), FONT_HERSHEY_COMPLEX_SMALL, 1.2, Scalar(0, 0, 0), 2); } } if (current_det_fps >= 0) { string fps_str = "FPS detection: " + to_string(current_det_fps) ; putText(mat_img, fps_str, Point2f(10, 20), FONT_HERSHEY_COMPLEX_SMALL, 1.2, Scalar(50, 255, 0), 2); } if (current_cap_fps >= 0) { string fps_str = "FPS capture: " + to_string(current_cap_fps); putText(mat_img, fps_str, Point2f(10, 50), FONT_HERSHEY_COMPLEX_SMALL, 1.2, Scalar(50, 255, 0), 2); } } vector<string> objects_names_from_file(string const filename) { ifstream file(filename); vector<string> file_lines; if (!file.is_open()) return file_lines; for (string line; getline(file, line);) file_lines.push_back(line); cout << "object names loaded \n"; return file_lines; } //点(浮点型) typedef struct _POINT_F_ { float x; float y; }POINT_F; //多边形(浮点型) typedef struct _VCA_POLYGON_F_ { unsigned int vertex_num; //顶点数 POINT_F point[5]; //顶点 }VCA_POLYGON_F; //前后视图片切换索引 typedef enum _BSD_WARNNING_TYPE_ { BSD_WARNNING_INVALID = 0, // 无效值 BSD_WARNNING_WARN = 1, // BSD提醒(超车) BSD_WARNNING_ALARM = 2, // BSD预警(无超车要求) BSD_WARNNING_ICW = 3, // 摄像头失效报警 BSD_EVENT_MOD = 4, // 移动障碍物检测 BSD_EVENT_MOD_ALL = 5, // 移动障碍物检测(全景) BSD_EVENT_DOW = 6, // 开门预警检测 BSD_WARNNING_END } BSD_WARNNING_TYPE; // BSDC报警区域枚举 typedef enum _VCA_REAR_VIEW_STATE_ { VCA_MOD_INVALID = 0, // 无障碍物(无效) VCA_MOD_OBS = 1, // 近处有障碍物 VCA_MOD_FAR = 2, // 远处有障碍物 VCA_MOD_LEFT = 3, // 左侧有障碍物 VCA_MOD_RIGHT = 4, // 右侧有障碍物 VCA_MOD_LEFT_LEVEL_TWO = 5, // 左侧有障碍物二级报警 VCA_MOD_RIGHT_LEVEL_TWO = 6, // 右侧有障碍物二级报警 VCA_MOD_LEVEL1 = 11, // 一级区域——最危险 VCA_MOD_LEVEL2 = 12, // 二级区域——中等危险 VCA_MOD_LEVEL3 = 13, // 三级区域——较安全 VCA_MOD_END } VCA_REAR_VIEW_STATE; /* VCA_BSD配置参数 */ typedef struct _BSD_REGION_PARAM_SINGLE_ { BSD_WARNNING_TYPE event_type;/*报警类型*/ VCA_REAR_VIEW_STATE view_state; /* 报警区域级别 */ VCA_POLYGON_F poly; /*报警框顶点*/ short sensity; char reserved[2]; } BSD_REGION_PARAM_SINGLE; /* BSD配置参数 */ typedef struct _BSD_PARAM_ALL_ { int region_num; BSD_REGION_PARAM_SINGLE region[4]; int time_type;/*0 1 2*/ char mirror_flag; /* 图像和结果是否需要镜像,1为镜像,0不镜像 */ } VCA_BSD_PARAM_ALL; // 侧视、车尾的报警区域配置参数 typedef struct _VCA_ADAS_BSDC_MOD_INPUT_PARAM_ { int whole_w; int whole_h; int poly_valid[3]; // 报警区域是否有效标志位 VCA_POLYGON_F bsd_poly[3]; // BSD报警区域坐标 VCA_REAR_VIEW_STATE view_state[3]; // 报警区域级别 } VCA_ADAS_BSDC_MOD_INPUT_PARAM; /*全局变量,用于bsd参数配置传递等*/ VCA_ADAS_BSDC_MOD_INPUT_PARAM gBsd_init_para = { 0 }; /******************************************************************************* 函数名称: BsdInitParam 描 述: 该函数初始化BSD报警参数配置,主要是报警框的设置 输入:输入宽高 输出: 返回值: 0: Successful ohters: Failed *******************************************************************************/ int BsdInitParam(int width,int height) { bool OK = 0;/*返回值初始化*/ int ori_w = width; int ori_h = height; VCA_ADAS_BSDC_MOD_INPUT_PARAM *pBsd_init_para = NULL; pBsd_init_para = &gBsd_init_para; #if 0 /*左视*/ //一级区域(一般为最小、最靠近本车的区域) pBsd_init_para->poly_valid[0] = 1; //该区域有效 pBsd_init_para->view_state[0] = VCA_MOD_LEVEL3; //一级区域 pBsd_init_para->bsd_poly[0].vertex_num = 5; pBsd_init_para->bsd_poly[0].point[0].x = (float)1100 / ori_w; pBsd_init_para->bsd_poly[0].point[1].x = (float)1255 / ori_w; pBsd_init_para->bsd_poly[0].point[2].x = (float)1083 / ori_w; pBsd_init_para->bsd_poly[0].point[3].x = (float)643 / ori_w; pBsd_init_para->bsd_poly[0].point[4].x = (float)643 / ori_w; pBsd_init_para->bsd_poly[0].point[0].y = (float)238 / ori_h; pBsd_init_para->bsd_poly[0].point[1].y = (float)218 / ori_h; pBsd_init_para->bsd_poly[0].point[2].y = (float)713 / ori_h; pBsd_init_para->bsd_poly[0].point[3].y = (float)716 / ori_h; pBsd_init_para->bsd_poly[0].point[4].y = (float)626 / ori_h; //二级区域 pBsd_init_para->poly_valid[1] = 1; //该区域有效 pBsd_init_para->view_state[1] = VCA_MOD_LEVEL2; //二级区域 pBsd_init_para->bsd_poly[0].vertex_num = 5; pBsd_init_para->bsd_poly[0].point[0].x = (float)1040 / ori_w; pBsd_init_para->bsd_poly[0].point[1].x = (float)1265 / ori_w; pBsd_init_para->bsd_poly[0].point[2].x = (float)1083 / ori_w; pBsd_init_para->bsd_poly[0].point[3].x = (float)413 / ori_w; pBsd_init_para->bsd_poly[0].point[4].x = (float)413 / ori_w; pBsd_init_para->bsd_poly[0].point[0].y = (float)218 / ori_h; pBsd_init_para->bsd_poly[0].point[1].y = (float)198 / ori_h; pBsd_init_para->bsd_poly[0].point[2].y = (float)716 / ori_h; pBsd_init_para->bsd_poly[0].point[3].y = (float)716 / ori_h; pBsd_init_para->bsd_poly[0].point[4].y = (float)616 / ori_h; //三级区域 pBsd_init_para->poly_valid[2] = 1; //该区域有效 pBsd_init_para->view_state[2] = VCA_MOD_LEVEL1; //三级区域 pBsd_init_para->bsd_poly[2].vertex_num = 5; pBsd_init_para->bsd_poly[2].point[0].x = (float)960 / ori_w; // 925 pBsd_init_para->bsd_poly[2].point[1].x = (float)1265 / ori_w; // pBsd_init_para->bsd_poly[2].point[2].x = (float)1083 / ori_w; pBsd_init_para->bsd_poly[2].point[3].x = (float)183 / ori_w; pBsd_init_para->bsd_poly[2].point[4].x = (float)183 / ori_w; pBsd_init_para->bsd_poly[2].point[0].y = (float)198 / ori_h; pBsd_init_para->bsd_poly[2].point[1].y = (float)178 / ori_h; pBsd_init_para->bsd_poly[2].point[2].y = (float)716 / ori_h; pBsd_init_para->bsd_poly[2].point[3].y = (float)716 / ori_h; pBsd_init_para->bsd_poly[2].point[4].y = (float)616 / ori_h; #endif # if 1 // //右视 pBsd_init_para->poly_valid[0] = 1; //该区域有效 pBsd_init_para->view_state[0] = VCA_MOD_LEVEL3; //一级区域,最大框 pBsd_init_para->bsd_poly[0].vertex_num = 5; pBsd_init_para->bsd_poly[0].point[0].x = (float)431/ori_w; pBsd_init_para->bsd_poly[0].point[0].y = (float)182/ori_h; pBsd_init_para->bsd_poly[0].point[1].x = (float)52/ori_w; pBsd_init_para->bsd_poly[0].point[1].y = (float)163/ori_h; pBsd_init_para->bsd_poly[0].point[2].x = (float)53/ori_w; pBsd_init_para->bsd_poly[0].point[2].y =(float) 720/ori_h; pBsd_init_para->bsd_poly[0].point[3].x =(float) 704/ori_w; pBsd_init_para->bsd_poly[0].point[3].y = (float)712/ori_h; pBsd_init_para->bsd_poly[0].point[4].x = (float)579/ori_w; pBsd_init_para->bsd_poly[0].point[4].y = (float)480/ori_h; //二级区域 pBsd_init_para->poly_valid[1] = 1; //该区域有效 pBsd_init_para->view_state[1] = VCA_MOD_LEVEL2; //二级区域 pBsd_init_para->bsd_poly[0].vertex_num = 5; pBsd_init_para->bsd_poly[1].point[0].x = (float)298/ori_w; pBsd_init_para->bsd_poly[1].point[0].y = (float)187/ori_h; pBsd_init_para->bsd_poly[1].point[1].x = (float)60/ori_w; pBsd_init_para->bsd_poly[1].point[1].y = (float)172/ori_h; pBsd_init_para->bsd_poly[1].point[2].x = (float)66/ori_w; pBsd_init_para->bsd_poly[1].point[2].y = (float)717/ori_h; pBsd_init_para->bsd_poly[1].point[3].x = (float)481/ori_w; pBsd_init_para->bsd_poly[1].point[3].y = (float)709/ori_h; pBsd_init_para->bsd_poly[1].point[4].x = (float)400/ori_w; pBsd_init_para->bsd_poly[1].point[4].y = (float)495/ori_h; //三级区域 pBsd_init_para->poly_valid[2] = 1; //该区域有效 pBsd_init_para->view_state[2] = VCA_MOD_LEVEL1; //三级区域 pBsd_init_para->bsd_poly[2].vertex_num = 5; pBsd_init_para->bsd_poly[2].point[0].x = (float)183/ori_w; pBsd_init_para->bsd_poly[2].point[0].y = (float)172/ori_h; pBsd_init_para->bsd_poly[2].point[1].x = (float)75/ori_w; pBsd_init_para->bsd_poly[2].point[1].y = (float)171/ori_h; pBsd_init_para->bsd_poly[2].point[2].x = (float)46/ori_w; pBsd_init_para->bsd_poly[2].point[2].y = (float)705/ori_h; pBsd_init_para->bsd_poly[2].point[3].x = (float)234/ori_w; pBsd_init_para->bsd_poly[2].point[3].y = (float)706/ori_h; pBsd_init_para->bsd_poly[2].point[4].x = (float)218/ori_w; pBsd_init_para->bsd_poly[2].point[4].y = (float)558/ori_h; #endif return OK; } /******************************************************************************* 函数名称: Bsd_draw_jeiguo 描 述: 该函数画图 输入:输入宽高,以及报警框个数 输出: 返回值: 图像 0: Successful ohters: Failed *******************************************************************************/ Mat Bsd_draw_jeiguo(Mat src, int width, int height,int lever) { bool OK = 0;/*返回值初始化*/ int ori_w = width; int ori_h = height; Mat dst; VCA_ADAS_BSDC_MOD_INPUT_PARAM *pBsd_init_para = NULL; pBsd_init_para = &gBsd_init_para; Point p1,p2; /*绘制报警框*/ for (int i = 0; i < lever; i++)/*三个等级*/ { for (int j = 0; j < 5; j++)/*每个报警框由5条线段组成*/ { p1.x = (int )( pBsd_init_para->bsd_poly[i].point[j].x*ori_w ); p1.y = (int )( pBsd_init_para->bsd_poly[i].point[j].y*ori_h ); if (j == 4) { p2.x = (int )(pBsd_init_para->bsd_poly[i].point[0].x*ori_w); p2.y = (int)( pBsd_init_para->bsd_poly[i].point[0].y*ori_h); } else { p2.x = (int)( pBsd_init_para->bsd_poly[i].point[j + 1].x*ori_w); p2.y = (int)( pBsd_init_para->bsd_poly[i].point[j + 1].y*ori_h); } /*绘制直线*/ line(src, p1, p2, Scalar(0, 0, 255), 2); } } return src; } /******************************************************************************* 函数名称: Bsd_AlarmInfo 描 述: 判断报警等级 输入:输入报警框, 输出:报警结果 返回值: 图像 0: Successful ohters: Failed *******************************************************************************/ Mat Bsd_AlarmInfo(Mat src, int width, int height, int lever) { bool OK = 0;/*返回值初始化*/ int ori_w = width; int ori_h = height; Mat dst; VCA_ADAS_BSDC_MOD_INPUT_PARAM *pBsd_init_para = NULL; pBsd_init_para = &gBsd_init_para; Point p1, p2; /*绘制报警框*/ for (int i = 0; i < lever; i++)/*三个等级*/ { for (int j = 0; j < 5; j++)/*每个报警框由5条线段组成*/ { p1.x = (int)(pBsd_init_para->bsd_poly[i].point[j].x*ori_w); p1.y = (int)(pBsd_init_para->bsd_poly[i].point[j].y*ori_h); if (j == 4) { p2.x = (int)(pBsd_init_para->bsd_poly[i].point[0].x*ori_w); p2.y = (int)(pBsd_init_para->bsd_poly[i].point[0].y*ori_h); } else { p2.x = (int)(pBsd_init_para->bsd_poly[i].point[j + 1].x*ori_w); p2.y = (int)(pBsd_init_para->bsd_poly[i].point[j + 1].y*ori_h); } /*绘制直线*/ line(src, p1, p2, Scalar(0, 0, 255), 2); } } return src; } int main() { bool OK = 0;/*返回值初始化*/ int ret = TRUE; int BSD_IMAGE_W = 1280;/*宽度*/ int BSD_IMAGE_H = 1280;/*高度*/ int lever = 1;/*只显示一个报警框*/ //string names_file = "coco.names"; //string cfg_file = "yolov3-tiny.cfg"; //string weights_file = "yolov3-tiny.weights"; /*test*/ string names_file = "coco.names"; string cfg_file = "yolov3-tiny.cfg"; string weights_file = "yolov3-tiny.weights"; //string names_file = "coco.names"; //string cfg_file = "yolov4-tiny.cfg"; //string weights_file = "yolov4-tiny.weights"; Detector detector(cfg_file, weights_file, 0); //初始化检测器 vector<string> obj_names; ifstream ifs(names_file.c_str()); string line; while (getline(ifs, line)) obj_names.push_back(line); //测试是否成功读入分类对象文件 for (size_t i = 0; i < obj_names.size(); i++) { cout << obj_names[i] << endl; } /*初始化BSD参数*/ ret = BsdInitParam(BSD_IMAGE_W, BSD_IMAGE_H); if (ret != OK) { ALG_ERR("配置BSD参数有问题\n"); } VideoCapture capture; //capture.open("yejianbsd.mp4"); capture.open("bsd.mp4"); if (!capture.isOpened()) { printf("文件打开失败"); } Mat frame; float fps = 0; while (true) { int begin, duration; begin = clock(); capture >> frame; if (frame.data == NULL) { printf("Image is empty"); return 0; } duration = clock() - begin; //printf("读图用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); //resize(frame, frame, Size(640,360)); vector<bbox_t> result_vec = detector.detect(frame); duration = clock() - begin; printf("识别用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); fps =(float) 1000.0 / duration; draw_boxes(frame, result_vec, obj_names,fps, fps); //duration = clock() - begin; //printf("画框用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); frame = Bsd_draw_jeiguo(frame, BSD_IMAGE_W, BSD_IMAGE_H, lever); namedWindow("bsd", 0); imshow("bsd", frame); waitKey(1); //duration = clock() - begin; //printf("显示用时约: %d毫秒 \n", duration * 1000 / CLOCKS_PER_SEC); printf("\n\n\n"); } return 0; }
纯cpu效果图:
如果只跑cpu,需要把darknet.vcxproj文件中cuda和cudnn路径去掉,不然打不开,
如下:
<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Debug|x64"> <Configuration>Debug</Configuration> <Platform>x64</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|Win32"> <Configuration>Release</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|x64"> <Configuration>Release</Configuration> <Platform>x64</Platform> </ProjectConfiguration> </ItemGroup> <PropertyGroup Label="Globals"> <ProjectGuid>{4CF5694F-12A5-4012-8D94-9A0915E9FEB5}</ProjectGuid> <RootNamespace>darknet</RootNamespace> <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> <PlatformToolset>v140</PlatformToolset> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> <PlatformToolset>v140</PlatformToolset> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> <PlatformToolset>v140</PlatformToolset> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> <PlatformToolset>v140</PlatformToolset> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings" /> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <PropertyGroup Label="UserMacros" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <OutDir>$(SolutionDir)$(Platform)\</OutDir> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <OutDir>$(SolutionDir)$(Platform)\</OutDir> </PropertyGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ClCompile> <WarningLevel>Level3</WarningLevel> <Optimization>Disabled</Optimization> <SDLCheck>true</SDLCheck> </ClCompile> <Link> <GenerateDebugInformation>true</GenerateDebugInformation> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> <WarningLevel>Level3</WarningLevel> <Optimization>Disabled</Optimization> <SDLCheck>true</SDLCheck> <AdditionalIncludeDirectories>$(OPENCV_DIR)\include;C:\opencv_3.0\opencv\build\include;..\..\include;..\..\3rdparty\stb\include;..\..\3rdparty\pthreads\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>_MBCS;LIB_EXPORTS;_TIMESPEC_DEFINED;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <UndefinePreprocessorDefinitions>CUDNN</UndefinePreprocessorDefinitions> <MultiProcessorCompilation>true</MultiProcessorCompilation> <ForcedIncludeFiles>stdlib.h;crtdbg.h;%(ForcedIncludeFiles)</ForcedIncludeFiles> </ClCompile> <Link> <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>$(OPENCV_DIR)\x64\vc15\lib;$(OPENCV_DIR)\x64\vc14\lib;C:\opencv_3.0\opencv\build\x64\vc14\lib;..\..\3rdparty\pthreads\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <OutputFile>$(OutDir)\$(TargetName)$(TargetExt)</OutputFile> <AdditionalDependencies>pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies> <AssemblyDebug>true</AssemblyDebug> </Link> <CudaCompile> <CodeGeneration>compute_30,sm_30;compute_75,sm_75</CodeGeneration> <TargetMachinePlatform>64</TargetMachinePlatform> </CudaCompile> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ClCompile> <WarningLevel>Level3</WarningLevel> <Optimization>MaxSpeed</Optimization> <FunctionLevelLinking>true</FunctionLevelLinking> <IntrinsicFunctions>true</IntrinsicFunctions> <SDLCheck>true</SDLCheck> <AdditionalIncludeDirectories>C:\opencv_2.4.9\opencv\build\include;..\..\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include</AdditionalIncludeDirectories> <PreprocessorDefinitions>LIB_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <MultiProcessorCompilation>true</MultiProcessorCompilation> </ClCompile> <Link> <GenerateDebugInformation>true</GenerateDebugInformation> <EnableCOMDATFolding>true</EnableCOMDATFolding> <OptimizeReferences>true</OptimizeReferences> <AdditionalLibraryDirectories>C:\opencv_2.4.9\opencv\build\x86\vc14\lib;C:\opencv_2.4.9\opencv\build\x86\vc12\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalDependencies>..\..\3rdparty\lib\x86\pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ClCompile> <WarningLevel>Level3</WarningLevel> <Optimization>MaxSpeed</Optimization> <FunctionLevelLinking>true</FunctionLevelLinking> <IntrinsicFunctions>true</IntrinsicFunctions> <SDLCheck>true</SDLCheck> <AdditionalIncludeDirectories>$(OPENCV_DIR)\include;C:\opencv_3.0\opencv\build\include;..\..\include;..\..\3rdparty\stb\include;..\..\3rdparty\pthreads\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>LIB_EXPORTS;_TIMESPEC_DEFINED;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <CLanguageStandard>c11</CLanguageStandard> <CppLanguageStandard>c++1y</CppLanguageStandard> <PrecompiledHeaderCompileAs>CompileAsCpp</PrecompiledHeaderCompileAs> <CompileAs>Default</CompileAs> <UndefinePreprocessorDefinitions>NDEBUG</UndefinePreprocessorDefinitions> <MultiProcessorCompilation>true</MultiProcessorCompilation> <AdditionalUsingDirectories> </AdditionalUsingDirectories> </ClCompile> <Link> <GenerateDebugInformation>true</GenerateDebugInformation> <EnableCOMDATFolding>true</EnableCOMDATFolding> <OptimizeReferences>true</OptimizeReferences> <AdditionalLibraryDirectories>$(OPENCV_DIR)\x64\vc15\lib;$(OPENCV_DIR)\x64\vc14\lib;C:\opencv_3.0\opencv\build\x64\vc14\lib;..\..\3rdparty\pthreads\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalDependencies>pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies> <OutputFile>$(OutDir)\$(TargetName)$(TargetExt)</OutputFile> </Link> <CudaCompile> <TargetMachinePlatform>64</TargetMachinePlatform> <CodeGeneration>compute_35,sm_35;compute_86,sm_86</CodeGeneration> </CudaCompile> </ItemDefinitionGroup> <ItemGroup> <CudaCompile Include="..\..\src\activation_kernels.cu" /> <CudaCompile Include="..\..\src\avgpool_layer_kernels.cu" /> <CudaCompile Include="..\..\src\blas_kernels.cu" /> <CudaCompile Include="..\..\src\col2im_kernels.cu" /> <CudaCompile Include="..\..\src\convolutional_kernels.cu" /> <CudaCompile Include="..\..\src\crop_layer_kernels.cu" /> <CudaCompile Include="..\..\src\deconvolutional_kernels.cu" /> <CudaCompile Include="..\..\src\dropout_layer_kernels.cu" /> <CudaCompile Include="..\..\src\im2col_kernels.cu" /> <CudaCompile Include="..\..\src\maxpool_layer_kernels.cu" /> <CudaCompile Include="..\..\src\network_kernels.cu" /> </ItemGroup> <ItemGroup> <ClCompile Include="..\..\src\activations.c" /> <ClCompile Include="..\..\src\activation_layer.c" /> <ClCompile Include="..\..\src\art.c" /> <ClCompile Include="..\..\src\avgpool_layer.c" /> <ClCompile Include="..\..\src\batchnorm_layer.c" /> <ClCompile Include="..\..\src\blas.c" /> <ClCompile Include="..\..\src\box.c" /> <ClCompile Include="..\..\src\captcha.c" /> <ClCompile Include="..\..\src\cifar.c" /> <ClCompile Include="..\..\src\classifier.c" /> <ClCompile Include="..\..\src\coco.c" /> <ClCompile Include="..\..\src\col2im.c" /> <ClCompile Include="..\..\src\compare.c" /> <ClCompile Include="..\..\src\connected_layer.c" /> <ClCompile Include="..\..\src\convolutional_layer.c" /> <ClCompile Include="..\..\src\conv_lstm_layer.c" /> <ClCompile Include="..\..\src\cost_layer.c" /> <ClCompile Include="..\..\src\cpu_gemm.c" /> <ClCompile Include="..\..\src\crnn_layer.c" /> <ClCompile Include="..\..\src\crop_layer.c" /> <ClCompile Include="..\..\src\darknet.c" /> <ClCompile Include="..\..\src\dark_cuda.c" /> <ClCompile Include="..\..\src\data.c" /> <ClCompile Include="..\..\src\deconvolutional_layer.c" /> <ClCompile Include="..\..\src\demo.c" /> <ClCompile Include="..\..\src\detection_layer.c" /> <ClCompile Include="..\..\src\detector.c" /> <ClCompile Include="..\..\src\dice.c" /> <ClCompile Include="..\..\src\dropout_layer.c" /> <ClCompile Include="..\..\src\gaussian_yolo_layer.c" /> <ClCompile Include="..\..\src\gemm.c" /> <ClCompile Include="..\..\src\getopt.c" /> <ClCompile Include="..\..\src\gettimeofday.c" /> <ClCompile Include="..\..\src\go.c" /> <ClCompile Include="..\..\src\gru_layer.c" /> <ClCompile Include="..\..\src\http_stream.cpp" /> <ClCompile Include="..\..\src\im2col.c" /> <ClCompile Include="..\..\src\image.c" /> <ClCompile Include="..\..\src\image_opencv.cpp" /> <ClCompile Include="..\..\src\layer.c" /> <ClCompile Include="..\..\src\list.c" /> <ClCompile Include="..\..\src\local_layer.c" /> <ClCompile Include="..\..\src\lstm_layer.c" /> <ClCompile Include="..\..\src\matrix.c" /> <ClCompile Include="..\..\src\maxpool_layer.c" /> <ClCompile Include="..\..\src\network.c" /> <ClCompile Include="..\..\src\nightmare.c" /> <ClCompile Include="..\..\src\normalization_layer.c" /> <ClCompile Include="..\..\src\option_list.c" /> <ClCompile Include="..\..\src\parser.c" /> <ClCompile Include="..\..\src\region_layer.c" /> <ClCompile Include="..\..\src\reorg_layer.c" /> <ClCompile Include="..\..\src\reorg_old_layer.c" /> <ClCompile Include="..\..\src\rnn.c" /> <ClCompile Include="..\..\src\rnn_layer.c" /> <ClCompile Include="..\..\src\rnn_vid.c" /> <ClCompile Include="..\..\src\route_layer.c" /> <ClCompile Include="..\..\src\sam_layer.c" /> <ClCompile Include="..\..\src\scale_channels_layer.c" /> <ClCompile Include="..\..\src\shortcut_layer.c" /> <ClCompile Include="..\..\src\softmax_layer.c" /> <ClCompile Include="..\..\src\super.c" /> <ClCompile Include="..\..\src\swag.c" /> <ClCompile Include="..\..\src\tag.c" /> <ClCompile Include="..\..\src\tree.c" /> <ClCompile Include="..\..\src\upsample_layer.c" /> <ClCompile Include="..\..\src\utils.c" /> <ClCompile Include="..\..\src\voxel.c" /> <ClCompile Include="..\..\src\writing.c" /> <ClCompile Include="..\..\src\yolo.c" /> <ClCompile Include="..\..\src\yolo_layer.c" /> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\include\darknet.h" /> <ClInclude Include="..\..\src\activations.h" /> <ClInclude Include="..\..\src\activation_layer.h" /> <ClInclude Include="..\..\src\avgpool_layer.h" /> <ClInclude Include="..\..\src\batchnorm_layer.h" /> <ClInclude Include="..\..\src\blas.h" /> <ClInclude Include="..\..\src\box.h" /> <ClInclude Include="..\..\src\classifier.h" /> <ClInclude Include="..\..\src\col2im.h" /> <ClInclude Include="..\..\src\connected_layer.h" /> <ClInclude Include="..\..\src\convolutional_layer.h" /> <ClInclude Include="..\..\src\conv_lstm_layer.h" /> <ClInclude Include="..\..\src\cost_layer.h" /> <ClInclude Include="..\..\src\crnn_layer.h" /> <ClInclude Include="..\..\src\crop_layer.h" /> <ClInclude Include="..\..\src\dark_cuda.h" /> <ClInclude Include="..\..\src\data.h" /> <ClInclude Include="..\..\src\deconvolutional_layer.h" /> <ClInclude Include="..\..\src\demo.h" /> <ClInclude Include="..\..\src\detection_layer.h" /> <ClInclude Include="..\..\src\dropout_layer.h" /> <ClInclude Include="..\..\src\gaussian_yolo_layer.h" /> <ClInclude Include="..\..\src\gemm.h" /> <ClInclude Include="..\..\src\getopt.h" /> <ClInclude Include="..\..\src\gettimeofday.h" /> <ClInclude Include="..\..\src\gru_layer.h" /> <ClInclude Include="..\..\src\http_stream.h" /> <ClInclude Include="..\..\src\im2col.h" /> <ClInclude Include="..\..\src\image.h" /> <ClInclude Include="..\..\src\image_opencv.h" /> <ClInclude Include="..\..\src\layer.h" /> <ClInclude Include="..\..\src\list.h" /> <ClInclude Include="..\..\src\local_layer.h" /> <ClInclude Include="..\..\src\lstm_layer.h" /> <ClInclude Include="..\..\src\matrix.h" /> <ClInclude Include="..\..\src\maxpool_layer.h" /> <ClInclude Include="..\..\src\network.h" /> <ClInclude Include="..\..\src\normalization_layer.h" /> <ClInclude Include="..\..\src\option_list.h" /> <ClInclude Include="..\..\src\parser.h" /> <ClInclude Include="..\..\src\region_layer.h" /> <ClInclude Include="..\..\src\reorg_layer.h" /> <ClInclude Include="..\..\src\reorg_old_layer.h" /> <ClInclude Include="..\..\src\rnn_layer.h" /> <ClInclude Include="..\..\src\route_layer.h" /> <ClInclude Include="..\..\src\sam_layer.h" /> <ClInclude Include="..\..\src\scale_channels_layer.h" /> <ClInclude Include="..\..\src\shortcut_layer.h" /> <ClInclude Include="..\..\src\softmax_layer.h" /> <ClInclude Include="..\..\src\stb_image.h" /> <ClInclude Include="..\..\src\stb_image_write.h" /> <ClInclude Include="..\..\src\tree.h" /> <ClInclude Include="..\..\src\unistd.h" /> <ClInclude Include="..\..\src\upsample_layer.h" /> <ClInclude Include="..\..\src\utils.h" /> <ClInclude Include="..\..\src\yolo_layer.h" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets" /> </Project>
Windows系统下YOLO动态链接库的封装和调用(Windows10+VS2015+OpenCV3.4.0+CUDA9.0+cuDNN7.0)
https://blog.csdn.net/weixin_42783784/article/details/105741982
https://blog.csdn.net/Schling/article/details/114597758
其他参考
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。