2021-05-12

目录

vs2019中运行NCNN例程

1.先copy一份code

2.配置环境

3.运行


vs2019中运行NCNN例程

在之前配置好ncnn环境后,最近先跑了一下例程,熟悉了一下流程

1.先copy一份code

#include "cpu.h"
#include "net.h"
#include "gpu.h"
#include "benchmark.h"
#include "datareader.h"

#include<vector>
#include<stdio.h>
#include<algorithm>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

int demo(cv::Mat& image, ncnn::Net& detector, int detector_size_with, int detector_size_height) {
	// 检测类别
	static const char* class_names[] = {
		"background","aeroplane","bicycle","bird","boat",
		"bottle","bus","car","cat","chair","cow","diningtable",
		"dog","horse","motorbike","person","pottedplant","sheep",
		"sofa","train","tvmonitor"
	};

	cv::Mat bgr = image.clone();
	int image_width = bgr.cols;
	int image_height = bgr.rows;

	ncnn::Mat input = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB,
		bgr.cols, bgr.rows, detector_size_with, detector_size_height);
	// 数据预处理
	const float mean_vals[3] = { 0.f, 0.f, 0.f };
	const float norm_vals[3] = { 1 / 255.f,1 / 255.f,1 / 255.f };
	input.substract_mean_normalize(mean_vals, norm_vals);

	ncnn::Extractor ex = detector.create_extractor();
	ex.set_num_threads(8);
	ex.input("data", input);
	ncnn::Mat out;
	ex.extract("output", out);

	for (int i = 0; i < out.h; ++i) {
		int label;
		float x1, y1, x2, y2, score;
		const float* values = out.row(i);

		label = values[0];
		score = values[1];
		x1 = values[2] * image_width;
		y1 = values[3] * image_height;
		x2 = values[4] * image_width;
		y2 = values[5] * image_height;
		// 处理越界坐标
		if (x1 < 0)x1 = 0;
		if (y1 < 0)y1 = 0;
		if (x2 < 0)x2 = 0;
		if (y2 < 0)y2 = 0;
		if (x1 > image_width)x1 = image_width;
		if (y1 > image_height)y1 = image_height;
		if (x2 > image_width)x2 = image_width;
		if (y2 > image_height)y2 = image_height;

		cv::rectangle(image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 255, 0), 1, 1, 0);

		char text[256];
		sprintf_s(text, "%s %.1f%%", class_names[label], score * 100);
		int baseLine = 0;
		cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
		cv::putText(image, text, cv::Point(x1, y1 + label_size.height),
			cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
	}
	return 0;
}

int test() {
	// 定义yolo-fatest检测器
	ncnn::Net detector;
	detector.load_param("models/yolo-fastest/yolo-fastest.param");
	detector.load_model("models/yolo-fastest/yolo-fastest.bin");
	// 定义输入图像尺寸
	int detector_size_width = 300;
	int detector_size_height = 300;
	// 测试图像
	cv::Mat image = cv::imread("E:/Algorithm_c++/Project2/test.jpg");
	// 调用函数开始检测
	demo(image, detector, detector_size_width, detector_size_height);
	// 显示检测结果
	cv::imshow("demo", image);
	cv::waitKey(0);
	return 0;
}

int main() {
	test();
	return 0;
}

其中.param和.bin文件需要下载好去加载。我这里并没有下载,只是体验一下这个流程。后面会自己将onnx转为ncnn进行加载。

2.配置环境

为工程文件配置环境

ps:release x64

ps:除此之外还有通过mikefile文件进行配置的,这个暂时没做了,后面用到会进行说明。当然后面也有可能用cmake直接进行编译生成可执行文件。

3.运行

运行后会生成一个.exe可执行文件,到这里就算是先体验一下,熟悉一下流程。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ranran是前鼻音

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

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

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

打赏作者

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

抵扣说明:

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

余额充值