libtorch 语义分割 C++推断

本文介绍如何使用libtorch库进行语义分割的C++推断过程,适用于自动驾驶等领域的实时图像处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语义分割C++推断

#pragma once
#include<string>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>


class _declspec(dllexport)Runtime_Segmentation_new  
{
   

public:
	/*构造函数,对模型进行参数初始化*/
	Runtime_Segmentation_new(const std::string model_path_, int image_size_, int num_threads_);

	/*对模型的名称进行参数解析*/
	int change_parameters(const std::string model_path_, int image_size_, int num_threads_);

	/*分割推断函数*/
	int Runing_infer(std::vector<cv::Mat>&orc_image, std::vector<cv::Mat> & pre_result);

	~Runtime_Segmentation_new();
private:

	std::string model_path;
	int image_size;
	int num_threads;
	std::string Device;
};


#include "Runtime_Segmentation_new.h"

#include <torch/torch.h>
#include <torch/script.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <direct.h>
#include <io.h>

#include <windows.h>

using namespace std;
using namespace cv;

static torch::jit::script::Module model_Segmentation_new;
static torch::Device device_all_Segmentation = torch::kCPU;//首先设置为CPU版本 torch::kGRU 为GPU版本

static int nw; static int nh; //resize的尺寸

/*用于分割字符串*/
static void Stringsplit(std::string str, const const char split, vector<std::string>& res)
{
   
	istringstream iss(str);	// 输入流 	
	string token;			// 接收缓冲区 	
	while (getline(iss, token, split))	// 以split为分隔符 	
	{
   
		res.push_back(token);
	}
}

/*分解一个路径*/
static std::vector<std::string> get_FilePath_many_information(std::string path)
{
   
	std::vector<std::string> information;

	const char *file;
	file = path.c_str();
	char szDrive[_MAX_DRIVE];   //磁盘名
	char szDir[_MAX_DIR];       //路径名
	char szFname[_MAX_FNAME];   //文件名
	char szExt[_MAX_EXT];       //后缀名
	_splitpath(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐雨清风 จุ๊บ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值