opencv 调用 pytorch训练好的模型

#include "pch.h"
#include 
#include 
#include 
#include 


using namespace cv;
using namespace cv::dnn;
using namespace std;


int main(int argc, char **argv)
{
	string modelBin = "E:/未来项目/炼数成金/(录制中)端到端/L5/models/instance_norm/starry_night.t7";
	string imageFile = "E:/template/library.jpg";

	float scale = 1.0;
	cv::Scalar mean{ 103.939, 116.779, 123.68 };
	bool swapRB = false;
	bool crop = false;
	bool useOpenCL = false;

	Mat img = imread(imageFile);
	pyrDown(img, img);
	if (img.empty()) {
		cout << "Can't read image from file: " << imageFile << endl;
		return 2;
	}

	// Load model
	Net net = dnn::readNetFromTorch(modelBin);
	if (useOpenCL)
		net.setPreferableTarget(DNN_TARGET_OPENCL);

	// Create a 4D blob from a frame.
	Mat inputBlob = blobFromImage(img, scale, img.size(), mean, swapRB, crop);

	// forward netword
	net.setInput(inputBlob);
	Mat output = net.forward();

	// process output
	Mat(output.size[2], output.size[3], CV_32F, output.ptr(0, 0)) += 103.939;
	Mat(output.size[2], output.size[3], CV_32F, output.ptr(0, 1)) += 116.779;
	Mat(output.size[2], output.size[3], CV_32F, output.ptr(0, 2)) += 123.68;

	std::vector ress;
	imagesFromBlob(output, ress);

	// show res
	Mat res;
	ress[0].convertTo(res, CV_8UC3);
	imshow("reslut", res);

	imshow("origin", img);

	waitKey();
	return 0;
}

结果:

opencv 调用 pytorch训练好的模型_第1张图片opencv 调用 pytorch训练好的模型_第2张图片

你可能感兴趣的:(Opencv,pytorch)