yolo训练实例python篇(二)
文章目录
- yolo版本
- 训练50次生成模型
- 模型效果实验
- cv2 实验结果
- yolo对比试验
- 或者
- 打印配置
- android部署
yolo版本
yolo --version
WARNING argument '--version' does not require leading dashes '--', updating to 'version'.
8.3.178
训练50次生成模型
from ultralytics import YOLO# Create a new YOLO model from scratch
model = YOLO("yolo11n.yaml")# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolo11n.pt")# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=50)# Evaluate the model's performance on the validation set
results = model.val()# Perform object detection on an image using the model
results = model("https://ultralytics.com/images/bus.jpg")# Export the model to ONNX format
success = model.export(format="onnx")
模型效果实验
import cv2
import numpy as np# 1. 加载ONNX模型
net = cv2.dnn.readNetFromONNX("best.onnx")
# 设置推理后端和目标设备
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) # 若支持GPU可改为DNN_TARGET_CUDA# 2. 读取图像并预处理
image = cv2.imread("bus.jpg")
if image is None:raise FileNotFoundError("无法找到图像文件,请检查路径是否正确")# 获取原图尺寸
H, W = image.shape[:2]# 预处理:转换为模型输入格式(640x640,归一化,BGR转RGB)
blob = cv2.dnn.blobFromImage(image, scalefactor=1/255.0, # 归一化到0-1size=(640, 640), # 模型输入尺寸swapRB=True, # BGR转RGBcrop=False # 不裁剪
)
net.setInput(blob)# 3. 执行推理
outputs = net.forward()# 4. 后处理:解析输出并筛选有效目标
# 输出格式:[num_detections, 6],每个目标包含(x1, y1, x2, y2, confidence, class_id)
outputs = np.squeeze(outputs) # 去除批次维度# 筛选置信度大于0.5的目标(可根据需求调整阈值)
conf_threshold = 0.5
valid_detections = outputs[outputs[:, 4] > conf_threshold]# 提取边界框、置信度和类别ID
boxes = valid_detections[:, :4] # [x1, y1, x2, y2](相对于640x640的坐标)
confidences = valid_detections[:, 4]
class_ids = valid_detections[:, 5].astype(int)# 将边界框坐标从640x640映射回原图尺寸
boxes[:, [0, 2]] = boxes[:, [0, 2]] * W / 640 # x坐标映射
boxes[:, [1, 3]] = boxes[:, [1, 3]] * H / 640 # y坐标映射
boxes = boxes.astype(int) # 转换为整数坐标# 非极大值抑制(NMS):去除重叠度高的重复框
iou_threshold = 0.4 # 交并比阈值,可调整
indices = cv2.dnn.NMSBoxes(bboxes=boxes.tolist(),scores=confidences.tolist(),score_threshold=conf_threshold,nms_threshold=iou_threshold
)# 5. 定义类别名称(需与训练时的数据集对应,这里以COCO8为例)
# 实际使用时请替换为你的数据集类别
class_names = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck"
]# 6. 绘制检测结果
if len(indices) > 0:indices = indices.flatten() # 展平索引数组for i in indices:x1, y1, x2, y2 = boxes[i]confidence = confidences[i]class_id = class_ids[i]class_name = class_names[class_id] if class_id < len(class_names) else f"class_{class_id}"# 绘制边界框(绿色,线宽2)cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)# 绘制类别标签和置信度(左上角显示)label = f"{class_name}: {confidence:.2f}"# 计算文本框位置(避免标签超出图像范围)label_y = y1 - 10 if y1 > 10 else y1 + 20cv2.putText(image, label, (x1, label_y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, # 字体大小(0, 255, 0), # 绿色2 # 线宽)# 7. 保存结果到本地
output_path = "detection_result.jpg"
cv2.imwrite(output_path, image)
print(f"检测结果已保存到:{output_path}")# 可选:显示结果窗口
cv2.imshow("YOLO Detection", image)
cv2.waitKey(0) # 按任意键关闭窗口
cv2.destroyAllWindows()
cv2 实验结果
图片大小:宽 = 810 像素,高 = 1080 像素识别到的目标信息:
目标 1:类别:未知类别(43) (置信度:45.73)左上角坐标:(0, 64)右下角坐标:(0, 92)宽度:0 像素,高度:28 像素
--------------------------------------------------检测结果已保存为:detection_result.jpg
yolo对比试验
4 persons, 1 bus, 31.4ms
(.venv) d:\LinuxWorkSpace\work-space-py> yolo predict model=best.onnx source=bus.jpg
WARNING Unable to automatically guess model task, assuming 'task=detect'. Explicitly define task for your model, i.e. 'task=detect', 'segment', 'classify','pose' or 'obb'.
Ultralytics 8.3.178 Python-3.12.8 torch-2.8.0+cpu CPU (13th Gen Intel Core(TM) i5-13500H)
Loading best.onnx for ONNX Runtime inference...
Using ONNX Runtime CPUExecutionProviderimage 1/1 d:\LinuxWorkSpace\work-space-py\bus.jpg: 640x640 4 persons, 1 bus, 31.4ms
Speed: 3.6ms preprocess, 31.4ms inference, 3.4ms postprocess per image at shape (1, 3, 640, 640)
Results saved to d:\LinuxWorkSpace\work-space-py\runs\detect\predictLearn more at https://docs.ultralytics.com/modes/predict
VS Code: view Ultralytics VS Code Extension at https://docs.ultralytics.com/integrations/vscode
或者
from ultralytics import YOLO
import argparsedef main():# 解析命令行参数(模拟yolo predict的使用方式)parser = argparse.ArgumentParser()parser.add_argument("model", help="ONNX模型路径")parser.add_argument("source", help="输入图片路径")args = parser.parse_args()# 加载模型model = YOLO(args.model)# 执行预测results = model(args.source)# 处理结果并打印坐标for result in results:# 打印图片信息img_path = result.pathimg_shape = result.orig_shape # (高, 宽)print(f"图片路径:{img_path}")print(f"图片大小:宽={img_shape[1]} 像素,高={img_shape[0]} 像素")print("\n识别到的目标信息:")# 遍历每个检测到的目标for idx, box in enumerate(result.boxes, 1):# 获取顶点坐标(左上角x1,y1,右下角x2,y2)x1, y1, x2, y2 = box.xyxy[0].tolist() # 原始像素坐标# 获取类别和置信度cls_id = int(box.cls[0])confidence = box.conf[0].item()class_name = model.names[cls_id]# 打印信息print(f"目标 {idx}:")print(f" 类别:{class_name} (置信度:{confidence:.2f})")print(f" 左上角坐标:({int(x1)}, {int(y1)})")print(f" 右下角坐标:({int(x2)}, {int(y2)})")print(f" 宽度:{int(x2 - x1)} 像素,高度:{int(y2 - y1)} 像素")print("-" * 60)# 保存预测结果图片result.save("prediction_result.jpg")print(f"\n预测结果已保存至:prediction_result.jpg")if __name__ == "__main__":main()
# python yolo_predict_with_coords.py best.onnx bus.jpg
(.venv) d:\LinuxWorkSpace\work-space-py>python yolo_predict_with_coords.py best.onnx bus.jpg
WARNING Unable to automatically guess model task, assuming 'task=detect'. Explicitly define task for your model, i.e. 'task=detect', 'segment', 'classify','pose' or 'obb'.
Loading best.onnx for ONNX Runtime inference...
Using ONNX Runtime CPUExecutionProviderimage 1/1 d:\LinuxWorkSpace\work-space-py\bus.jpg: 640x640 4 persons, 1 bus, 30.9ms
Speed: 3.2ms preprocess, 30.9ms inference, 2.9ms postprocess per image at shape (1, 3, 640, 640)
图片路径:d:\LinuxWorkSpace\work-space-py\bus.jpg
图片大小:宽=810 像素,高=1080 像素识别到的目标信息:
目标 1:类别:bus (置信度:0.94)左上角坐标:(20, 231)右下角坐标:(802, 740)宽度:782 像素,高度:509 像素
------------------------------------------------------------
目标 2:类别:person (置信度:0.90)左上角坐标:(47, 397)右下角坐标:(244, 905)宽度:196 像素,高度:507 像素
------------------------------------------------------------
目标 3:类别:person (置信度:0.84)左上角坐标:(669, 390)右下角坐标:(810, 879)宽度:140 像素,高度:489 像素
------------------------------------------------------------
目标 4:类别:person (置信度:0.84)左上角坐标:(223, 404)右下角坐标:(345, 860)宽度:121 像素,高度:455 像素
------------------------------------------------------------
目标 5:类别:person (置信度:0.37)左上角坐标:(0, 549)右下角坐标:(65, 870)宽度:65 像素,高度:320 像素
------------------------------------------------------------预测结果已保存至:prediction_result.jpg
可见opencv对转换的onnx识别效果不太行啊
打印配置
yolo settings
android部署
https://onnxruntime.ai/docs/build/android.html