QT+ubuntu16.04+opencv410+darknet yolo v3编译测试

1、QT安装和opencv编译省略,网上一大堆

2、编译darknet

1)下载源码 git clone https://github.com/AlexeyAB/darknet.git

2)进入源码目录创建build文件夹,然后cd到build目录,由于我工程目录下已经有了build,因此我直接创建build2

mkdir build2
cd build2

2)开始编译

cmake ..
make
sudo make install

 

3、配置QT+代码测试,测试代码可以在yolo_console_dll.cpp里面提取,我这里已经缩减只剩下几行代码了

std::string  names_file = "/home/bzl/QT_Projection/TestYolo/data/qrcode.names";
    std::string  cfg_file = "/home/bzl/QT_Projection/TestYolo/data/yolov3-tiny.cfg";
    std::string  weights_file = "/home/bzl/QT_Projection/TestYolo/backup/yolov3-tiny_10000.weights";
    std::string  filename = "/home/bzl/QT_Projection/TestYolo/data/img/1.bmp";

    Detector detector(cfg_file, weights_file);

    auto obj_names = objects_names_from_file(names_file);
    std::cout << "input image or video filename: ";
    cv::Mat mat_img = cv::imread(filename);


    auto det_image = detector.mat_to_image_resize(mat_img);
    std::cout << det_image->w << std::endl;
    std::cout << det_image->h << std::endl;
    std::cout << mat_img.size()<< std::endl;
    double timeConsume,start;
    start = static_cast<double>(cv::getTickCount());
    std::vector<bbox_t> result_vec = detector.detect_resized(*det_image, mat_img.size().width, mat_img.size().height);
    timeConsume = ((double)cv::getTickCount() - start)*1000 / cv::getTickFrequency();
    qDebug()<<"time count:"<<timeConsume<<endl;
    draw_boxes(mat_img, result_vec, obj_names);
    cv::imwrite("111.jpg", mat_img);

4、QT pro文件配置

INCLUDEPATH += opencv-410/linux/include/ \
               opencv-410/linux/include/opencv2 \

LIBS += $$PWD/opencv-410/linux/lib/libopencv_highgui.so \
        $$PWD/opencv-410/linux/lib/libopencv_core.so    \
        $$PWD/opencv-410/linux/lib/libopencv_imgproc.so \
        $$PWD/opencv-410/linux/lib/libopencv_imgcodecs.so \
        $$PWD/opencv-410/linux/lib/libopencv_video.so \
        $$PWD/opencv-410/linux/lib/libopencv_videoio.so

INCLUDEPATH += yolo/include/
LIBS +=/home/bzl/QT_Projection/TestYolo/yolo/lib/libdarknet.so

这里在配置完libdarknet.so文件后,直接使用QT编译会出现找不到文件的情况

解决办法:

1)设置

sudo gedit /etc/ld.so.conf

2)添加库路径,然后保存关闭

include /etc/ld.so.conf.d/*.conf

/usr/local/lib
/home/bzl/QT_Projection/TestYolo/yolo/lib

3)使刚修改的配置生效

sudo ldconfig

参考链接:https://blog.csdn.net/qc530167365/article/details/91491851

附整版代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <fstream>
#include <QDebug>
#include <opencv2/opencv.hpp>
#include <yololib/include/yolo_v2_class.hpp>
#include <yololib/include/darknet.h>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

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;
}

void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
    int current_det_fps = -1, int current_cap_fps = -1)
{
    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 max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
            max_width = std::max(max_width, (int)i.w + 2);
            //max_width = std::max(max_width, 283);
            std::string coords_3d;
            if (!std::isnan(i.z_3d)) {
                std::stringstream ss;
                ss << std::fixed << std::setprecision(2) << "x:" << i.x_3d << "m y:" << i.y_3d << "m z:" << i.z_3d << "m ";
                coords_3d = ss.str();
                cv::Size const text_size_3d = getTextSize(ss.str(), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, 1, 0);
                int const max_width_3d = (text_size_3d.width > i.w + 2) ? text_size_3d.width : (i.w + 2);
                if (max_width_3d > max_width) max_width = max_width_3d;
            }

            cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 35, 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 - 16), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(0, 0, 0), 2);
            if(!coords_3d.empty()) putText(mat_img, coords_3d, cv::Point2f(i.x, i.y-1), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cv::Scalar(0, 0, 0), 1);
        }
    }
    if (current_det_fps >= 0 && current_cap_fps >= 0) {
        std::string fps_str = "FPS detection: " + std::to_string(current_det_fps) + "   FPS capture: " + std::to_string(current_cap_fps);
        putText(mat_img, fps_str, cv::Point2f(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2);
    }
}

void MainWindow::on_pushButton_clicked()
{
    std::string  names_file = "data/qrcode.names";
        std::string  cfg_file = "data/yolov3-tiny.cfg";
        std::string  weights_file = "data/backup/yolov3-tiny_10000.weights";
        std::string  filename = "data/img/1.bmp";

        Detector detector(cfg_file, weights_file);

        auto obj_names = objects_names_from_file(names_file);
        std::cout << "input image or video filename: ";
        cv::Mat mat_img = cv::imread(filename);


        auto det_image = detector.mat_to_image_resize(mat_img);
        std::cout << det_image->w << std::endl;
        std::cout << det_image->h << std::endl;
        std::cout << mat_img.size()<< std::endl;
        double timeConsume,start;
        start = static_cast<double>(cv::getTickCount());
        std::vector<bbox_t> result_vec = detector.detect_resized(*det_image, mat_img.size().width, mat_img.size().height);
        timeConsume = ((double)cv::getTickCount() - start)*1000 / cv::getTickFrequency();
        qDebug()<<"time count:"<<timeConsume<<endl;
        draw_boxes(mat_img, result_vec, obj_names);
        cv::imwrite("111.jpg", mat_img);
}

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LoveWeeknd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值