Win10 vs2017 configure Libtorch and opencv

1 Download Libtorch and OpenCV

1.1 Download Libtorch

The pytorch version here needs to be consistent with the pytorch version of the yolov5 training model. I use cuda10.2, and the download address is as follows:
Libtorch download address
After the download is complete, you get a compressed package:
insert image description here

Unzip to get a libtorch folder:
insert image description here

1.2 download opencv

Opencv download address
After downloading, you get an exe file:
insert image description here

Unzip the exe to get an opencv folder:
insert image description here

2 Configure environment variables

2.1 Configure Libtorch environment variables

Configure to lib file

2.2 Configure OpenCV environment variables

insert image description here

3 Create a new empty project with vs2017

3.1 New project

insert image description here

3.2 Configure the OpenCV environment and test of VS

1. Configure the include directory
Project --> Properties --> VC++ directory --> Include directory: C:\Program Files (x86)\opencv\build\include
insert image description here
2. Configure library directory
VC++ directory --> Library directory: C: \Program Files (x86)\opencv\build\x64\vc15\lib
Note: vc14 is selected for vs2015; vc15 is selected for vs2017 and vs2019
insert image description here
3. Add the name of the OpenCV library file Linker
--> Input --> Additional dependencies: opencv_world455d.lib
insert image description here
So far, VS configuration OpenCV has been completed.

4. Create a source file: test_opencv.cpp, add the following code, change the operating environment to x64, and click Run.

#include<opencv2\opencv.hpp>


int main(int argc, char**argv)
{
    
    
	cv::Mat image = cv::imread("E:\\zhouyi\\practice code\\opencv\\OpenCV_Libtorch_Test\\images\\bus.jpg",1);
	cv::namedWindow("output");
	cv::imshow("output", image);
	cv::waitKey(0);

	system("pause");
	return 0;

}

5. Running effect
insert image description here
So far, VS has finished configuring OpenCV and testing.

3.3 Configure the Libtorch environment and test of VS

1. Configure the include directory
Project --> Properties --> VC++ directory --> Include directory: C:\Program Files\libtorch\include and C:\Program Files\libtorch\include\torch\csrc\api\include
insert image description here
2. Configure library directory
VC++ directory --> library directory: C:\Program Files\libtorch\lib
insert image description here
3. Add Libtorch's dependent library
linker --> input: c10.lib, torch.lib, torch_cpu.lib and torch_cuda.lib
insert image description here
to here , VS configuration Libtorch environment has been completed.

4. Create a new source file: test_libtorch.cpp, add the following code and run it:

#include<torch/torch.h>
#include<iostream>

int main(int argc, char**argv)
{
    
    
	torch::Tensor data = torch::rand({
    
     3,3 });
	std::cout << data << std::endl;
	std::cout << torch::cuda::is_available() << std::endl;

	system("pause");
	return 0;
}

If it prompts an error of "std" ambiguous symbol, project --> properties --> C/C++ --> language --> compliance mode: change "yes" to " no " .
insert image description here
5. Running effect
insert image description here
Among them, 0 means that cuda is not used.
How to configure to support cuda?
Properties --> Linker --> Command line: /INCLUDE:?warp_size@cuda@at@@YAHXZ
Note: cuda10 add: /INCLUDE:?warp_size@cuda@at@@YAHXZ
cuda11 add: /INCLUDE:?searchsorted_cuda@ native@at@@YA?AVTensor@2@AEBV32@0_N1@Z

insert image description here
and then run the program, the effect is as follows:
insert image description here
"1" indicates that cuda can be supported.
So far, VS configuration Libtorch and OpenCV and testing have been completed.

Guess you like

Origin blog.csdn.net/Kigha/article/details/127305725