qt+opencv+caffe 在开发板上显示

20 篇文章 2 订阅
5 篇文章 0 订阅

 编译:


source /opt/fsl-imx-x11/4.1.15-2.1.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi

qmake

make
 

 

挂载:

mount -t nfs -o nolock,nfsvers=3,vers=3 172.16.10.214:/home/xxp/linux/nfs/mount /mnt

关闭现有界面:

/etc/init.d/psplash.sh

加载挂载文件夹的lib文件:

cp -r /mnt/lib/. /lib

代码:

xxx.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

INCLUDEPATH += /usr/local/arm/opencv-3.4.5/install/include

LIBS += /usr/local/arm/opencv-3.4.5/install/lib/libopencv_core.so \
        /usr/local/arm/opencv-3.4.5/install/lib/libopencv_highgui.so \
        /usr/local/arm/opencv-3.4.5/install/lib/libopencv_imgproc.so \
        /usr/local/arm/opencv-3.4.5/install/lib/libopencv_videoio.so \
        /usr/local/arm/opencv-3.4.5/install/lib/libopencv_imgcodecs.so \
        /usr/local/arm/opencv-3.4.5/install/lib/libopencv_dnn.so \
        /usr/local/arm/opencv-3.4.5/install/lib/libopencv_shape.so \
       -lpthread

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&theTimer, &QTimer::timeout, this, &MainWindow::updateImage);
    if(videoCap.open(1))
        {
            srcImage = Mat::zeros(videoCap.get(CV_CAP_PROP_FRAME_HEIGHT), videoCap.get(CV_CAP_PROP_FRAME_WIDTH), CV_8UC3);
            //cvtColor(srcImage, srcImage, COLOR_BGRA2GRAY);
            theTimer.start(33);
        }

        //设置显示视频用的Label
        imageLabel = new QLabel(this);

        ui->verticalLayout->addWidget(imageLabel);

}

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

void MainWindow::paintEvent(QPaintEvent *e)
{
    QImage image2 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
    imageLabel->setPixmap(QPixmap::fromImage(image2));
    //imageLabel->resize(image2.size());
    imageLabel->show();
}

void MainWindow::updateImage()
{
    videoCap>>srcImage;

    float min_confidence = 0.5;
        String modelConfiguration = "deploy.prototxt";
        String modelBinary = "res10_300x300_ssd_iter_140000.caffemodel";
        dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
        Mat inputBlob = blobFromImage(srcImage, inScaleFactor,
                Size(inWidth, inHeight), meanVal, false, false);
                net.setInput(inputBlob, "data");
                Mat detection = net.forward("detection_out");

                vector<double> layersTimings;
                double freq = getTickFrequency() / 1000;
                double time = net.getPerfProfile(layersTimings) / freq;
                Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());

                ostringstream ss;
                ss << "FPS: " << 1000 / time << " ; time: " << time << " ms";
                putText(srcImage, ss.str(), Point(30, 80), 2, 2.5, Scalar(0, 0, 255));

                float confidenceThreshold = min_confidence;
                for (int i = 0; i < detectionMat.rows; i++)
                {
                    float confidence = detectionMat.at<float>(i, 2);
                    if (confidence > confidenceThreshold)
                    {
                        int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * srcImage.cols);
                        int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * srcImage.rows);
                        int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * srcImage.cols);
                        int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * srcImage.rows);
                        Rect object((int)xLeftBottom, (int)yLeftBottom,
                            (int)(xRightTop - xLeftBottom),
                            (int)(yRightTop - yLeftBottom));
                        rectangle(srcImage, object, Scalar(0, 255, 0),8);
                        ss.str("");
                        ss << confidence;
                        String conf(ss.str());
                        String label = "Face: " + conf;
                        int baseLine = 0;
                        Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 1, 4, &baseLine);
                        rectangle(srcImage, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
                            Size(labelSize.width, labelSize.height + baseLine)),
                            Scalar(255, 255, 255), CV_FILLED);
                        putText(srcImage, label, Point(xLeftBottom, yLeftBottom),
                            FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 0));
                    }
                }


    if(srcImage.data)
    {
        cvtColor(srcImage, srcImage, CV_BGR2RGB);//Qt中支持的是RGB图像, OpenCV中支持的是BGR
        //image=srcImage;
        this->update();	//发送刷新消息
    }
}

ui

加载一个verticallayout

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值