Skip to content

add libtorch demo #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions 实践案例/qt+opencv+libtorch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Set the minimum version of CMake that can be used
# 设置CMake最小版本
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.19.1)

# Set the project name
# 设置工程名
project(qui)


# 如果设置了环境变量可以省略以下步奏,若没有设置则参照如下路径设置自己的
set(Qt5_DIR "D:/Qt/Qt5.13.2/5.13.2/msvc2017_64/lib/cmake/Qt5")
set(OpenCV_DIR "D:/opencv452/build")
set(Torch_DIR "D:/LibTorch_1.8.2.R/share/cmake/Torch")
# In ubuntu
# set(Qt5_DIR "/home/jedi/Qt5.14.2/5.14.2/gcc_64/lib/cmake/Qt5")
# set(Torch_DIR "/home/jedi/libtorch/share/cmake/Torch")


#注意,在一个大工程里,这几个开关一定要尽量放在前面打开,否则有可能会报无法生成ui文件的错误
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#FIND_PACKAGE(Qt5 COMPONENTS Core Gui Qml Quick Widgets REQUIRED)
FIND_PACKAGE(Qt5 COMPONENTS Core Widgets REQUIRED)


# 2 find open cv
#FIND_PACKAGE( OpenCV REQUIRED )
find_package(OpenCV CONFIG REQUIRED PATHS)
MESSAGE("OpenCV version : ${OpenCV_VERSION}")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})
MESSAGE("OpenCV_LIB_DIR : ${OpenCV_LIB_DIR}")
MESSAGE("OpenCV_LIBS : ${OpenCV_LIBS}")

# 3 find libtorch
find_package(Torch REQUIRED)



SET(qui_SOURCES
src/main.cpp
src/mainwindow.cpp
)

SET(qui_HEADERS
src/mainwindow.h
)

SET(qui_FORMS
src/mainwindow.ui
)

#SET(qui_RESOURCES
# img.qrc
#)

#调用预编译器moc,需要使用 QT5_WRAP_CPP宏
QT5_WRAP_CPP(qui_HEADERS_MOC ${qui_HEADERS})

#使用uic处理.ui文件
QT5_WRAP_UI(qui_FORMS_HEADERS ${qui_FORMS})

#使用rcc处理.qrc文件
#QT5_ADD_RESOURCES(qui_RESOURCES_RCC ${qui_RESOURCES})

#这些生成的中间文件都会在build目录下,这样的话,编译器则不能定位由uic程序产生的诸如_ui_mainwindow.h等文件。所以,我们需要把build目录添加到包含目录中
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

#生成可执行文件
ADD_EXECUTABLE(${PROJECT_NAME}
${qui_SOURCES}
${qui_HEADERS_MOC}
${qui_FORMS_HEADERS}
#${qui_RESOURCES_RCC}
)

#为target添加需要链接的共享库
#TARGET_LINK_LIBRARIES(${PROJECT_NAME} Qt5::Core Qt5::Gui Qt5::Qml Qt5::Quick ${Qt5Widgets_LIBRARIES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Qt5Widgets_LIBRARIES})
INSTALL(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION lib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)

# Link your application with OpenCV libraries
target_link_directories(${PROJECT_NAME} PUBLIC
${OpenCV_LIB_DIRS}
)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
target_include_directories(${PROJECT_NAME} PUBLIC
${OpenCV_INCLUDE_DIRS}
)
MESSAGE("OpenCV_INCLUDE_DIRS : ${OpenCV_INCLUDE_DIRS}")


# Link your application with LibTorch libraries
target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}")
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
39 changes: 39 additions & 0 deletions 实践案例/qt+opencv+libtorch/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include "mainwindow.h"
#include <QApplication>
#include <QWidget>
#include <QDebug>
#include <thread>
#include <QTime>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>


#undef slots
#include<torch/script.h>
#include<torch/torch.h>
#include<torch/csrc/api/include/torch/utils.h>
#define slots Q_SLOTS


using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
cout << "Hello Qt-CMake!" << endl;

QApplication a(argc, argv);

MainWindow mainWin;
mainWin.show();

Mat image(Size(100, 100), CV_8UC1);
imshow("black",image);

torch::Tensor tensor = torch::rand({3,3});
cout << tensor << endl;

return a.exec();
}
19 changes: 19 additions & 0 deletions 实践案例/qt+opencv+libtorch/src/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

void MainWindow::on_pushButton_pressed()
{
ui->label_2->setText(ui->lineEdit->text());
}
25 changes: 25 additions & 0 deletions 实践案例/qt+opencv+libtorch/src/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_pushButton_pressed();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
118 changes: 118 additions & 0 deletions 实践案例/qt+opencv+libtorch/src/mainwindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>299</width>
<height>366</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>CMake-Qt-GUI</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>显示</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>显示按钮</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>299</width>
<height>28</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>