首页 > 其他分享 >Qt+opencv dnn模块调用tensorflow模型

Qt+opencv dnn模块调用tensorflow模型

时间:2023-07-06 22:35:52浏览次数:54  
标签:Qt int frame dnn opencv background size

参考网址(1条消息) Qt+opencv dnn模块调用tensorflow模型_vs qt 调用 tensorflow_街道口扛把子的博客-CSDN博客
代码地址:GitHub - Whu-wxy/Simple_Qt_opencv_dnn: Using deep learning model with opencv in Qt
修改运行后的代码如下:

#include <QCoreApplication>
#include<opencv2\opencv.hpp>
#include<opencv2\dnn.hpp>
#include <iostream>
#include<map>
#include<string>
#include<time.h>

using namespace std;
using namespace cv;

const size_t inWidth = 300;
const size_t inHeight = 300;
const float WHRatio = inWidth / (float)inHeight;
const char* classNames[]= {"background", "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant", "background", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "background", "backpack",
"umbrella", "background", "background", "handbag", "tie", "suitcase", "frisbee","skis", "snowboard", "sports ball", "kite", "baseball bat","baseball glove", "skateboard", "surfboard", "tennis racket",
"bottle", "background", "wine glass", "cup", "fork", "knife", "spoon","bowl", "banana",  "apple", "sandwich", "orange","broccoli", "carrot", "hot dog",  "pizza", "donut",
"cake", "chair", "couch", "potted plant", "bed", "background", "dining table", "background", "background", "toilet", "background","tv", "laptop", "mouse", "remote", "keyboard",
"cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "background","book", "clock", "vase", "scissors","teddy bear", "hair drier", "toothbrush"};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    clock_t start, finish;
    double totaltime;
    Mat frame;

    String weights = "C:/Users/Administrator/Downloads/Simple_Qt_opencv_dnn-master/Simple_Qt_opencv_dnn-master/ssd_mobilenet_v1_coco_11_06_2017/frozen_inference_graph.pb";
    String prototxt = "C:/Users/Administrator/Downloads/Simple_Qt_opencv_dnn-master/Simple_Qt_opencv_dnn-master/ssd_mobilenet_v1_coco_11_06_2017/ssd_mobilenet_v1_coco.pbtxt";
    dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);
    frame = imread("E:/0.jpg");

    start = clock();
    Size frame_size = frame.size();

    Size cropSize;
    if (frame_size.width / (float)frame_size.height > WHRatio)
    {
        cropSize = Size(static_cast<int>(frame_size.height * WHRatio),
                        frame_size.height);
    }
    else
    {
        cropSize = Size(frame_size.width,
                        static_cast<int>(frame_size.width / WHRatio));
    }

    Rect crop(Point((frame_size.width - cropSize.width) / 2,
                    (frame_size.height - cropSize.height) / 2),
              cropSize);


    cv::Mat blob = cv::dnn::blobFromImage(frame, 1. / 255, Size(300, 300));
    // 1*3*300*300
    cout << "blob size: " << blob.size << endl;

    net.setInput(blob);
    Mat output = net.forward();
    // 1*1*100*7
    cout << "output size: " << output.size << endl;

    Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());

    frame = frame(crop);
    float confidenceThreshold = 0.50;
    for (int i = 0; i < detectionMat.rows; i++)
    {
        float confidence = detectionMat.at<float>(i, 2);

        if (confidence > confidenceThreshold)
        {
            size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));

            int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
            int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
            int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
            int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);

            ostringstream ss;
            ss << confidence;
            String conf(ss.str());

            Rect object((int)xLeftBottom, (int)yLeftBottom,
                        (int)(xRightTop - xLeftBottom),
                        (int)(yRightTop - yLeftBottom));

            rectangle(frame, object, Scalar(0, 255, 0), 1);
            //cout << "objectClass:" << objectClass << endl;
            String label = String(classNames[objectClass]) + ": " + conf;
            //cout << "label"<<label << endl;
            int baseLine = 0;
            Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
            rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
                                  Size(labelSize.width, labelSize.height + baseLine)),
                      Scalar(0, 255, 0), CV_FILLED);
            putText(frame, label, Point(xLeftBottom, yLeftBottom),
                    FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
        }
    }
    finish = clock();
    totaltime = finish - start;
    cout << "识别图像所用的时间为:" << totaltime <<"ms"<< endl;
    namedWindow("result", 0);
    imshow("result", frame);

    waitKey(0);


    return a.exec();
}

 

标签:Qt,int,frame,dnn,opencv,background,size
From: https://www.cnblogs.com/dq0618/p/17533508.html

相关文章

  • 在全志V853上进行Opencv库的编译步骤
    OpenCV(OpenSourceComputerVision)是一个开放源代码的计算机视觉库,它提供了一系列函数和算法,用于处理图像和视频。通过使用OpenCV,您可以进行各种计算机视觉任务,例如图像处理、对象识别、目标追踪、人脸检测和机器学习等。它提供了底层图像处理功能,以及高级功能和模块,如特征提取、......
  • C/C++ Qt 数据库与SqlTableModel组件应用
    SqlTableModel组件可以将数据库中的特定字段动态显示在TableView表格组件中,通常设置QSqlTableModel类的变量作为数据模型后就可以显示数据表内容,界面组件中则通过QDataWidgetMapper类实例设置为与某个数据库字段相关联,则可以实现自动显示字段的内容,不仅是显示,其还支持动态增删改查......
  • Qt 四种程序的架构设计方法
    四种常见的关于Qt程序的架构设计方法:1.使用MVC设计模式MVC是Model-View-Controller的缩写,是应用程序开发中常用的设计模式。在Qt中,可以使用QAbstractItemModel和QTreeView等类来实现MVC模式。 2.使用信号和槽机制Qt中的信号和槽机制是一种灵活的方式,可以将不同的组件连接在......
  • Docker+Emqx+Nginx集群及负载均衡的搭建 MQTT服务器
    Docker+Emqx+Nginx集群及负载均衡的搭建MQTT服务器1.拉取镜像dockerpullemqx/emqx2.创建虚拟网络#-d参数默认为bridge,可省略dockernetworkcreate-dbridge--subnet=172.18.0.0/16emqx_bridge3.创建Docker容器#节点1dockerrun-d--hostnameemqx01--nameem......
  • Python QT5 使用笔记[随意记]
    self.rkDialog.tableWidget.findItems() 是一个在Qt中用于在表格小部件(TableWidget)中查找匹配项的方法。它的作用是查找满足特定条件的单元格项,并返回一个包含这些项的列表。这个方法的用法如下: items=self.rkDialog.tableWidget.findItems(text,flags) 参数说明:......
  • python opencv无法编码h264、opencv编码的mp4视频无法在网页中播放
    pythonopencv无法编码h264、opencv编码的mp4视频无法在网页中播放,这好像是因为开源许可的协议不同,导致pythonopencv中没有内置h264的编码,无法以h264的格式保存视频。所以我就直接使用webm格式的视频:output_path='output_video.webm'output_codec=cv2.VideoWriter_fourcc......
  • Qt给一个形状添加外发光的效果
    我们知道给控件添加特效可以使用QGraphicsEffect类。但是如果不针对整个控件而只是针对控件内的某个元素怎么添加外发光效果呢?比如说我在控件内绘制一个六边形,要给这个六边形添加外发光效果。一般的做法是先对六边形模糊一下,然后先绘制模糊的六边形再在其上叠加绘制原始六边形。绘......
  • C/C++ Qt 数据库SqlRelationalTable关联表
    在上一篇博文中详细介绍了SqlTableModle组件是如何使用的,本篇博文将介绍SqlRelationalTable关联表组件,该组件其实是SqlTableModle组件的扩展类,SqlRelationalTable组件可以关联某个主表中的外键,例如将主表中的某个字段与附加表中的特定字段相关联起来,QSqlRelation(关联表名,关联ID,......
  • Qt防止程序重复运行
    1.使用共享内存的方法 弊端:使用共享内存方式,当第二个进程启动时,判断内存区数据是否建立,如有,则退出;这种方式有弊端,在程序发生崩溃时,未及时清除共享区数据,导致程序不能正常启动。参考:qt之使程序只运行一个实例,若再次点击exe则将已运行的实例置在最顶层显示_大桶矿泉水......
  • Linux系统编程 C/C++ 以及Qt 中的零拷贝技术: 从底层原理到高级应用
    https://blog.csdn.net/qq_21438461/article/details/130764349Linux系统编程C/C++以及Qt中的零拷贝技术:从底层原理到高级应用一、零拷贝技术的概念与价值(Zero-CopyConceptandValue)1.1什么是零拷贝(WhatisZero-Copy)1.2为什么我们需要零拷贝(WhyWeNeedZero-C......