// Tencent is pleased to support the open source community by making ncnn available.// 说明:这是 Tencent 开源 ncnn 框架的一部分声明,强调版权和许可信息。// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except// in compliance with the License. You may obtain a copy of the License at// https://opensource.org/licenses/BSD-3-Clause// Unless required by applicable law or agreed to in writing, software distributed// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR// CONDITIONS OF ANY KIND, either express or implied. See the License for the// specific language governing permissions and limitations under the License.#ifndefYOLOV8_H#defineYOLOV8_H// 防止头文件重复包含(Include Guard),确保编译器只处理一次 YOLOv8_H#include<opencv2/core/core.hpp>// 包含 OpenCV 核心模块,用于处理 cv::Mat、cv::Point、cv::Rect 等基础类型#include<net.h>// 包含 ncnn 的网络类 ncnn::Net,YOLOv8 内部用它加载和运行模型// 定义关键点结构体structKeyPoint{cv::Point2f p;// 关键点的二维坐标 (x, y),浮点类型float prob;// 关键点的置信度};// 定义检测目标对象结构体structObject{cv::Rect_<float> rect;// 目标的矩形边界框(普通矩形)cv::RotatedRect rrect;// 目标的旋转矩形(可旋转检测框)int label;// 目标类别索引float prob;// 目标置信度int gindex;// 全局索引或分组索引(根据业务可能用作 tracking 或类别分组)cv::Mat mask;// 目标的掩码(用于分割任务)std::vector<KeyPoint> keypoints;// 目标关键点列表(用于姿态检测)};// YOLOv8 基类classYOLOv8{public:virtual~YOLOv8();// 虚析构函数,确保继承类析构时能正确释放资源// 从文件路径加载模型参数和权重intload(constchar* parampath,constchar* modelpath,bool use_gpu =false);// 从 Android AAssetManager 资产加载模型(适用于 Android 平台)intload(AAssetManager* mgr,constchar* parampath,constchar* modelpath,bool use_gpu =false);// 设置检测输入的目标尺寸(通常用于缩放输入图像)voidset_det_target_size(int target_size);// 纯虚函数:检测函数,每个子类必须实现virtualintdetect(const cv::Mat& rgb, std::vector<Object>& objects)=0;// 纯虚函数:绘制函数,用于在图像上绘制检测结果,每个子类必须实现virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects)=0;protected:ncnn::Net yolov8;// NCNN 网络对象,用于加载和运行 YOLOv8 模型int det_target_size;// 检测输入尺寸};// YOLOv8 检测子类(普通目标检测)classYOLOv8_det:publicYOLOv8{public:virtualintdetect(const cv::Mat& rgb, std::vector<Object>& objects);// YOLOv8_det 提供 detect 函数实现};// YOLOv8 检测 + COCO 绘制风格classYOLOv8_det_coco:publicYOLOv8_det{public:virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects);// 实现 draw 函数,绘制 COCO 风格框和标签};// YOLOv8 检测 + OIV7 绘制风格classYOLOv8_det_oiv7:publicYOLOv8_det{public:virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects);// 实现 draw 函数,绘制 OIV7 风格框和标签};// YOLOv8 分割子类classYOLOv8_seg:publicYOLOv8{public:virtualintdetect(const cv::Mat& rgb, std::vector<Object>& objects);virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects);// detect 实现分割预测,draw 绘制掩码等结果};// YOLOv8 姿态子类classYOLOv8_pose:publicYOLOv8{public:virtualintdetect(const cv::Mat& rgb, std::vector<Object>& objects);virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects);// detect 实现关键点检测,draw 绘制关键点和骨架};// YOLOv8 分类子类classYOLOv8_cls:publicYOLOv8{public:virtualintdetect(const cv::Mat& rgb, std::vector<Object>& objects);virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects);// detect 实现图像分类,draw 绘制分类结果};// YOLOv8 旋转检测子类classYOLOv8_obb:publicYOLOv8{public:virtualintdetect(const cv::Mat& rgb, std::vector<Object>& objects);virtualintdraw(cv::Mat& rgb,const std::vector<Object>& objects);// detect 实现旋转框检测,draw 绘制旋转框};#endif// YOLOV8_H// 结束头文件防重复包含
// Tencent is pleased to support the open source community by making ncnn available.// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except// in compliance with the License. You may obtain a copy of the License at// https://opensource.org/licenses/BSD-3-Clause// Unless required by applicable law or agreed to in writing, software distributed// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR// CONDITIONS OF ANY KIND, either express or implied. See the License for the// specific language governing permissions and limitations under the License.#include"yolov8.h"// 包含类声明(YOLOv8)的头文件,通常定义了类成员、ncnn::Net yolov8 等// 析构函数YOLOv8::~YOLOv8(){det_target_size =320;// 在对象销毁时将 det_target_size 设为 320// 注意:在析构函数里设置成员值通常没有实际意义(对象马上就要被销毁)。// 这行更像是应放在构造函数中以初始化默认值。建议改为在构造函数中初始化 det_target_size。}intYOLOv8::load(constchar* parampath,constchar* modelpath,bool use_gpu){yolov8.clear();// 清除 ncnn::Net 中已有的参数/模型,保证从空状态重新加载yolov8.opt = ncnn::Option();// 重置 ncnn 的运行选项为默认值(构造一个新的 Option)#ifNCNN_VULKANyolov8.opt.use_vulkan_compute = use_gpu;// 如果编译时开启了 Vulkan 支持,则根据 use_gpu 设置是否使用 Vulkan 计算后端#endifyolov8.load_param(parampath);// 从文件加载网络结构参数(.param 文件或类似)yolov8.load_model(modelpath);// 从文件加载网络权重(二进制 .bin 文件或类似)return0;// 返回 0 表示成功(本函数没有做错误检查,若 load_* 失败则不会通过返回值上报)}intYOLOv8::load(AAssetManager* mgr,constchar* parampath,constchar* modelpath,bool use_gpu){yolov8.clear();// 同上,清理 Netyolov8.opt = ncnn::Option();// 重置 Option#ifNCNN_VULKANyolov8.opt.use_vulkan_compute = use_gpu;// Vulkan 后端设置(同上)#endifyolov8.load_param(mgr, parampath);// 从 Android 资产管理器(AAssetManager)中加载 param(适用于 Android 平台)yolov8.load_model(mgr, modelpath);// 从 AAssetManager 加载 model(二进制权重)return0;// 返回 0 表示成功}voidYOLOv8::set_det_target_size(int target_size){det_target_size = target_size;// 设置检测输入的目标尺寸(通常用于内部预处理时的缩放/letterbox)}