如何使用 opencv 加载 darknet yolo 预训练模型?

opencv 版本 > 3.4 以上

constexpr const char *image_path = "darknet.jpg";//待检测图片
constexpr const char *darknet_cfg = "darknet.cfg";//网络文件
constexpr const char *darknet_weights = "darknet.weights";//训练模型
const std::vector<std::string> class_labels = {"darknet","yolo"};//类标签 void darknetDetection(const std::string &path,const std::string &darknet_cfg,const std::string &darknet_weights,std::vector<std::string> class_labels,float confidenceThreshold)
{
// 加载模型
cv::dnn::Net net = cv::dnn::readNetFromDarknet(darknet_cfg,darknet_weights); // 加载标签集
std::vector<std::string> classLabels = class_labels; // 读取待检测图片
cv::Mat img = cv::imread(path);
cv::Mat blob = cv::dnn::blobFromImage(img,1.0/255.0,{416,416},0.00392,true);
net.setInput(blob,"data"); // 检测
cv::Mat detectionMat = net.forward("detection_out");// 6 845 1 W x H x C // 获取网络每层的用时并获取总用时
std::vector<double> layersTimings;
double freq = cv::getTickFrequency() / 1000;
double time = net.getPerfProfile(layersTimings) / freq;
std::ostringstream ss;
ss << "detection time: " << time << " ms";
// 绘制总用时至原始图片
cv::putText(img, ss.str(), cv::Point(20, 20), 0, 0.5, cv::Scalar(0, 0, 255)); // 遍历所有结果集
for(int i = 0;i < detectionMat.rows;++i){
const int probability_index = 5;
const int probability_size = detectionMat.cols - probability_index;
float *prob_array_ptr = &detectionMat.at<float>(i, probability_index);
size_t objectClass = std::max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index); // 比较置信度并绘制满足条件的置信度
if (confidence > confidenceThreshold)
{
float x = detectionMat.at<float>(i, 0);
float y = detectionMat.at<float>(i, 1);
float width = detectionMat.at<float>(i, 2);
float height = detectionMat.at<float>(i, 3); int xLeftBottom = static_cast<int>((x - width / 2) * img.cols);
int yLeftBottom = static_cast<int>((y - height / 2) * img.rows);
int xRightTop = static_cast<int>((x + width / 2) * img.cols);
int yRightTop = static_cast<int>((y + height / 2) * img.rows); cv::Rect object(xLeftBottom, yLeftBottom,xRightTop - xLeftBottom,yRightTop - yLeftBottom);//x y w h
cv::rectangle(img, object, cv::Scalar(0, 0, 255), 2, 8); // 判断类id是否符合标签范围并绘制该标签,也就是矩阵的下标索引
if (objectClass < classLabels.size())
{
cv::String label = cv::String(classLabels[objectClass]) + ": " + std::to_string(confidence);
int baseLine = 0;
cv::Size labelSize = cv::getTextSize(label,cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
cv::rectangle(img, cv::Rect(cv::Point(xLeftBottom, yLeftBottom),cv::Size(labelSize.width, labelSize.height + baseLine)),cv::Scalar(255, 255, 255), cv::FILLED);
cv::putText(img, label, cv::Point(xLeftBottom, yLeftBottom + labelSize.height),cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}
}
} // 显示图片
cv::imshow("Darknet",img);
cv::waitKey(0);
}

如何使用 opencv 加载 darknet yolo 预训练模型?的更多相关文章

  1. PyTorch保存模型与加载模型+Finetune预训练模型使用

    Pytorch 保存模型与加载模型 PyTorch之保存加载模型 参数初始化参 数的初始化其实就是对参数赋值.而我们需要学习的参数其实都是Variable,它其实是对Tensor的封装,同时提供了da ...

  2. qt: 打不开png图像以及opencv加载中文路径问题;

    经过亲测, QT(版本: 5.9.4)提供的QImageReader或者函数load在加载本地png图像时,均会提示失败, 按照网上的方法,将Qt plugins下的imageformats 拷贝到e ...

  3. 预加载与智能预加载(iOS)

    来源:Draveness(@Draveness) 链接:http://www.jianshu.com/p/1519a5302141 前两次的分享分别介绍了 ASDK 对于渲染的优化以及 ASDK 中使 ...

  4. Opencv加载网络图片

    opencv加载网络图片 #include <iostream> #include <opencv2/opencv.hpp> using namespace std; usin ...

  5. 【原生JS】图片预加载之无序预加载

    图片预加载之无序预加载 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset= ...

  6. 文字检测模型EAST应用详解 ckpt pb的tf加载,opencv加载

    参考链接:https://github.com/argman/EAST (项目来源) https://github.com/opencv/opencv/issues/12491  (遇到的问题)    ...

  7. OpenCV加载图像并显示

    从文件中读取一直一张图片,并加载出来 代码: #include "stdafx.h" #include "iostream" using namespace s ...

  8. 图片预加载 js css预加载

    图片预加载, 效果非常明显, 特别是有多个图, 方法很简单 , 体验提升了不少 <div class="hidden">        <script type= ...

  9. opencv 加载 修改 保存 图像

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; /* 1 加载图像 cv::imre ...

随机推荐

  1. jquery 怎么触发select的change事件

    可以使用jQuery的trigger() 方法来响应事件 定义和用法 trigger() 方法触发被选元素的指定事件类型. 语法 $(selector).trigger(event,[param1,p ...

  2. dp之多重背包poj2392

    题意:有k种石头,高为hi,在不超过ai的高度下,这种石头可以放置,有ci种这个石头,求这些石头所能放置的最高高度......... 思路:以往的什么硬币种数,最大硬币数之类的,他们的硬币都已经是排好 ...

  3. 微博excel数据清洗(Java版)

    微博数据清洗(Java版) 原创 2013年12月10日 10:58:24 2979 大数据公益大学提供的一份数据,义务处理一下,原始数据是Excel,含有html标签,如下:   要求清洗掉html ...

  4. USB转TTL 下载线 线序定义

    产品名称:USB转TTL 下载线 长度       :1米 芯片       :PL2303HX.PL2303TA. 线序定义红+5V, 黑GND, 绿TXD,白RXD

  5. 决Ubuntu使用`make menuconfig`配置Linux 内核时,出现缺少'ncurses-devel'库支持。

    *** Unable to find the ncurses libraries or the *** required header files. *** 'make menuconfig' req ...

  6. C#中利用JQuery实现视频网站的缩略图采集

    最近有朋友想要采集优酷的视频标题和缩略图 (哈哈, 并非商业目的). 找到我帮忙, 考虑到有我刚刚发布的SpiderStudio, 我毫不犹豫的答应了. 首先在网页上视频的基本结构为: div.v - ...

  7. Redis 响应延迟问题排查

    计算延迟时间 如果你正在经历响应延迟问题,你或许能够根据应用程序的具体情况算出它的延迟响应时间,或者你的延迟问题非常明显,宏观看来,一目了然.不管怎样吧,用redis-cli可以算出一台Redis 服 ...

  8. ci框架简单出现的错误[Undefined property: MContacts::$db]

    出现这样的错误时说明自己忘记加载数据库了, application/config/aotuload.php     $autoload['libraries'] = array('database') ...

  9. 数据库 proc编程五

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  10. i2c 异常之i2c1 prob 检测超时

    在没加atl 的fpga 时 i2c1上的tvp5150 vpss驱动加载没问题, 加了之后出现超时 I2C: timed out in wait_for_bb: I2C_IRQSTATUS=1000 ...