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

 编译:


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 layersTimings;
                double freq = getTickFrequency() / 1000;
                double time = net.getPerfProfile(layersTimings) / freq;
                Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr());

                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(i, 2);
                    if (confidence > confidenceThreshold)
                    {
                        int xLeftBottom = static_cast(detectionMat.at(i, 3) * srcImage.cols);
                        int yLeftBottom = static_cast(detectionMat.at(i, 4) * srcImage.rows);
                        int xRightTop = static_cast(detectionMat.at(i, 5) * srcImage.cols);
                        int yRightTop = static_cast(detectionMat.at(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

你可能感兴趣的:(caffe,opencv,算法,深度学习)