1.安装失败请检查网络或开启VPN来进行安装
2.安装完成后打开失败请检查系统环境配置
1. 找到 yolo 版本,我的路径:D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\Lib\site-packages\ultralytics
2. 设置配置文件 ai_card_config.yaml,请查看官网详细说明
3. 检测方案 yolov8n.yaml
4. 执行终端管理员命令 切换盘符 CD F:\YoloV8
data: 配置
model: 模型默认 yolov8n.yaml
epochs:训练次数
imgsz:图片大小
执行训练,完成时间与训练次数挂钩,我的训练只是图像识别
yolo detect train data=ai_card_config.yaml model=yolov8n.yaml epochs=100 imgsz=640
训练完成会得到一个 pt 后缀文件
# -*- coding: utf-8 -*-
from ultralytics import YOLO
# 加载训练后的模型权重文件
model = YOLO('best.pt')
# 设置要进行推理的图像路径
image_path = '3.jpg'
# 进行推理
results = model(image_path)
# 输出结果
results.print() # 打印结果
results.save('output/') # 保存带有预测结果的图像
# 查看预测的标签和置信度
for result in results:
print(f'Label: {result.labels}, Confidence: {result.conf}')
1.再把 pt 后缀文件转换成 onnx 后缀文件供.net 库的使用
# Load a model
model = YOLO("best.pt") # load a custom trained model
# Export the model
model.export(format="onnx")
2. 事先到nuget安装 YoloDotNet 与 Snet.Core 两个动态库,复制如下代码即可使用
/// <summary>
/// yolov8 检测操作数据
/// </summary>
public class YoloDetectData
{
/// <summary>
/// 基础数据
/// </summary>
public class Basics
{
/// <summary>
/// 模型路径
/// </summary>
public string ModelPath { get; set; }
}
}
/// <summary>
/// yolov8 检测操作
/// </summary>
public class YoloDetectOperate : CoreAbstract<YoloDetectOperate, YoloDetectData.Basics>, IGetStatus, IDisposable
{
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="basics">基础数据</param>
public YoloDetectOperate(YoloDetectData.Basics basics) : base(basics)
{
yolo = new Yolo(new YoloOptions
{
OnnxModel = basics.ModelPath, // Your Yolov8 or Yolov10 model in onnx format
ModelType = ModelType.ObjectDetection, // Model type
Cuda = false, // Use CPU or CUDA for GPU accelerated inference. Default = true
GpuId = 0, // Select Gpu by id. Default = 0
PrimeGpu = false, // Pre-allocate GPU before first. Default = false
});
}
/// <summary>
/// 检测服务
/// </summary>
private Yolo yolo;
/// <summary>
/// 运行检测
/// </summary>
/// <param name="image">图片源</param>
/// <returns>操作结果</returns>
public OperateResult Run(SKImage image)
{
//检测
List<ObjectDetection> results = yolo.RunObjectDetection(image);
//results 返回的结果,自行修改此处代码
return OperateResult.CreateSuccessResult("");
}
/// <summary>
/// 运行检测
/// </summary>
/// <param name="imagePath">图片路径</param>
/// <returns>操作结果</returns>
public OperateResult Run(string imagePath) => Run(SKImage.FromEncodedData(imagePath));
/// #<inheritdoc/>
public OperateResult GetStatus()
{
if (yolo == null)
{
return OperateResult.CreateFailureResult("模型未加载");
}
return OperateResult.CreateSuccessResult("模型已加载");
}
public async Task<OperateResult> GetStatusAsync(CancellationTokenSource? token = null) => await Task.Run(() => GetStatus(), token?.Token ?? CancellationToken.None);
}
以上是简单的阐述AI的应用,YOLO还支持其他的识别并且速度很快,后续会出一个详细的视频来讲解