使用Opencv运行onnx模型-从训练到部署

3 篇文章 0 订阅
2 篇文章 0 订阅

1. 首先使用pytorch训练一个简单的猫狗分类模型
具体代码参考
pytorch实现kaggle猫狗识别(超详细)
记得先下载猫狗数据集,然后改一下代码里的路径

2. 将保存的模型转为onnx格式

model = torch.load('model.pt',map_location=lambda storage, loc: storage)
dummy = torch.randn(1,3,224,224)
out = model(dummy)
torch.onnx.export(model,dummy,"classifier.onnx",opset_version=12,input_names=['input'],output_names=['output'],
                  dynamic_axes={'input':{0:'batch_size'},'output':{0:'batch_size'}})

对这部分代码不熟的话,可以参考Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime
3. 准备一个label.txt,表明label和数字的关系,内容如下

0:cat
1:dog

4.opencv 的C++代码

#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/dnn/dnn.hpp>
#include<string>
#include<iostream>
#include<fstream>
#include<vector>


class ONNXClassifier
{
public:
	ONNXClassifier(const std::string& model_path, const std::string& label_path, cv::Size _input_size);
	void Classify(const cv::Mat& input_image, std::string& out_name);
private:
	void preprocess_input(cv::Mat& image);
	bool read_labels(const std::string& label_paht);
private:
	cv::Size input_size;
	cv::dnn::Net net;
	cv::Scalar default_mean;
	cv::Scalar default_std;
	std::vector<std::string> labels;

};

ONNXClassifier::ONNXClassifier(const std::string& model_path, const std::string& label_path, cv::Size _input_size):default_mean(0.485, 0.456, 0.406),
default_std(0.229, 0.224, 0.225),input_size(_input_size)
{
	if (!read_labels(label_path))
	{
		throw std::runtime_error("label read fail!");
	}
	net = cv::dnn::readNet(model_path);
	net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
	net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
}
bool ONNXClassifier::read_labels(const std::string& label_path)
{
	std::ifstream ifs(label_path);
	assert(ifs.is_open());
	std::string line;
	while (std::getline(ifs,line))
	{
		std::size_t index = line.find_first_of(':');
		labels.push_back(line.substr(index + 1));
	}
	if (labels.size() > 0)
		return true;
	else
		return false;
}
void ONNXClassifier::preprocess_input(cv::Mat& image)
{
	image.convertTo(image, CV_32F,1.0/255.0);
	cv::subtract(image,default_mean,image);
	cv::divide(image, default_std, image);
}

void ONNXClassifier::Classify(const cv::Mat& input_image, std::string& out_name)
{
	out_name.clear();
	cv::Mat image = input_image.clone();
	preprocess_input(image);
	cv::Mat input_blob = cv::dnn::blobFromImage(image, 1.0, input_size, cv::Scalar(0, 0, 0), true);
	net.setInput(input_blob);
	const std::vector<cv::String>& out_names = net.getUnconnectedOutLayersNames();
	cv::Mat out_tensor = net.forward(out_names[0]);
        cv::Point maxLoc;
        cv::minMaxLoc(out_tensor,(double*)0,(double*)0,(cv::Point*)0,&maxLoc);
        out_name = labels[maxLoc.x];
}

int main(int argc, char* argv[])
{
	if (argc != 2)
	{
		std::cout << "input a image file path" << std::endl;
		return -1;
	}
	std::string model_path("../model/classifier.onnx");
	std::string label_path("../model/labels.txt");
	cv::Size input_size(224, 224);
	cv::Mat test_image = cv::imread(argv[1]);
	ONNXClassifier classifier(model_path, label_path, input_size);
	std::string result;
	classifier.Classify(test_image, result);
        std::cout<<"result: "<<result<<std::endl;
	return 0;
}

注意

opencv必须含有dnn模块,推荐opencv4.5

C++完整功能包下载
C++代码和模型文件

  • 5
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,下面是使用OpenCVONNXRuntime来部署YOLOV7实时目标检测的代码: ```python import cv2 import numpy as np import onnxruntime as ort # 加载模型 model_path = "yolov7.onnx" session = ort.InferenceSession(model_path) # 输入、输出名 input_name = session.get_inputs()[0].name output_names = [session.get_outputs()[i].name for i in range(len(session.get_outputs()))] # 类别名称 class_names = ["person", "car", "truck", "bus"] # 预处理函数 def preprocess(image, target_shape): # 图像缩放 h, w = image.shape[:2] scale = min(target_shape[0] / h, target_shape[1] / w) new_h, new_w = int(h * scale), int(w * scale) image_resized = cv2.resize(image, (new_w, new_h)) # 图像填充 pad_h = target_shape[0] - new_h pad_w = target_shape[1] - new_w top, bottom = pad_h // 2, pad_h - pad_h // 2 left, right = pad_w // 2, pad_w - pad_w // 2 image_padded = cv2.copyMakeBorder(image_resized, top, bottom, left, right, cv2.BORDER_CONSTANT) # 图像归一化 image_scaled = image_padded / 255.0 image_normalized = (image_scaled - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225] image_transposed = np.transpose(image_normalized, [2, 0, 1]) image_batched = np.expand_dims(image_transposed, axis=0) return image_batched # 后处理函数 def postprocess(outputs, conf_threshold, iou_threshold): # 输出解码 objects = [] for i, output in enumerate(outputs): grid_size = output.shape[2] anchor_size = 3 num_classes = output.shape[1] - 5 boxes = output.reshape([-1, 5 + num_classes]) boxes[:, 0:2] = (boxes[:, 0:2] + np.arange(grid_size).reshape([1, -1, 1])) / grid_size boxes[:, 2:4] = np.exp(boxes[:, 2:4]) * anchor_size / grid_size boxes[:, 4:] = np.exp(boxes[:, 4:]) / (1 + np.exp(-boxes[:, 4:])) boxes[:, 5:] = boxes[:, 4:5] * boxes[:, 5:] mask = boxes[:, 4] > conf_threshold boxes = boxes[mask] classes = np.argmax(boxes[:, 5:], axis=-1) scores = boxes[:, 4] * boxes[:, 5 + classes] mask = scores > conf_threshold boxes = boxes[mask] classes = classes[mask] scores = scores[mask] for cls, score, box in zip(classes, scores, boxes): if cls >= num_classes: continue x, y, w, h = box[:4] x1, y1, x2, y2 = x - w / 2, y - h / 2, x + w / 2, y + h / 2 objects.append([x1, y1, x2, y2, score, class_names[cls]]) # 非极大抑制 objects = sorted(objects, key=lambda x: x[4], reverse=True) for i in range(len(objects)): if objects[i][4] == 0: continue for j in range(i + 1, len(objects)): if iou(objects[i][:4], objects[j][:4]) > iou_threshold: objects[j][4] = 0 # 输出筛选 objects = [obj for obj in objects if obj[4] > conf_threshold] return objects # IOU计算函数 def iou(box1, box2): x1, y1, x2, y2 = box1 x3, y3, x4, y4 = box2 left = max(x1, x3) top = max(y1, y3) right = min(x2, x4) bottom = min(y2, y4) intersection = max(0, right - left) * max(0, bottom - top) area1 = (x2 - x1) * (y2 - y1) area2 = (x4 - x3) * (y4 - y3) union = area1 + area2 - intersection return intersection / (union + 1e-6) # 摄像头读取 cap = cv2.VideoCapture(0) while True: # 读取帧 ret, frame = cap.read() # 预处理 image = preprocess(frame, (416, 416)) # 推理 outputs = session.run(output_names, {input_name: image}) # 后处理 objects = postprocess(outputs, conf_threshold=0.5, iou_threshold=0.5) # 可视化 for obj in objects: x1, y1, x2, y2, score, class_name = obj cv2.rectangle(frame, (int(x1 * frame.shape[1]), int(y1 * frame.shape[0])), (int(x2 * frame.shape[1]), int(y2 * frame.shape[0])), (0, 255, 0), 2) cv2.putText(frame, class_name + ": " + str(round(score, 2)), (int(x1 * frame.shape[1]), int(y1 * frame.shape[0]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) # 显示结果 cv2.imshow("YOLOV7", frame) # 退出 if cv2.waitKey(1) == ord("q"): break # 释放资源 cap.release() cv2.destroyAllWindows() ``` 这段代码通过摄像头实时读取视频流,对每一帧进行目标检测,并将检测结果可视化显示在窗口中。在代码中,我们首先加载了YOLOV7模型,并定义了输入、输出名和类别名称。接着,我们定义了预处理函数和后处理函数,用于对输入图像进行预处理和输出结果进行解码、筛选和可视化。最后,我们通过OpenCV读取摄像头视频流,对每一帧进行目标检测并实时显示在窗口中,直到按下“q”键退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值