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
将模型导出为ONNX格式
success = model.export(format=‘onnx’)
## 二、图像预测
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/969e2039b44e4ff79029b0f51458eefd.png#pic_center)
在机器学习和计算机视觉领域,从可视数据中找出意义的过程被称为 "推理 "或 “预测”。
### 预测模式的主要功能
**1.返回一个包含 stream=False**
from ultralytics import YOLO
Load a model
加载模型
model = YOLO(‘yolov8n.pt’) # pretrained YOLOv8n model
Run batched inference on a list of images
在图像列表上运行批处理推理
results = model([‘im1.jpg’, ‘im2.jpg’]) # return a list of Results objects
Process results list
处理结果列表
for result in results:
boxes = result.boxes # Boxes object for bounding box outputs
# 用于边界框输出的边界框对象
masks = result.masks # Masks object for segmentation masks outputs
# 用于分割掩模输出的掩模对象
keypoints = result.keypoints # Keypoints object for pose outputs
# 用于姿势输出的关键点对象
probs = result.probs # Probs object for classification outputs
# 用于分类输出的概率对象
result.show() # display to screen
# 显示到屏幕
result.save(filename=‘result.jpg’) # save to disk
# 保存到磁盘
**2.返回一个带有 stream=True**
from ultralytics import YOLO
Load a model
model = YOLO(‘yolov8n.pt’) # pretrained YOLOv8n model
加载一个模型:使用预训练的YOLOv8n模型
Run batched inference on a list of images
results = model([‘im1.jpg’, ‘im2.jpg’], stream=True) # return a generator of Results objects
对图像列表进行批量推理:返回一个Results对象的生成器
Process results generator
for result in results:
boxes = result.boxes # Boxes object for bounding box outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
result.show() # display to screen
result.save(filename=‘result.jpg’) # save to disk
# 处理结果生成器:boxes表示边界框输出的Boxes对象
# masks表示分割掩模输出的Masks对象
# keypoints表示姿势输出的Keypoints对象
# probs表示分类输出的Probs对象
# 显示到屏幕
# 保存到磁盘
>
> 使用 stream=True 用于处理长视频或大型数据集,以有效管理内存。当 stream=False在这种情况下,所有帧或数据点的结果都会存储在内存中,这可能会迅速累加,并导致大量输入出现内存不足错误。与此形成鲜明对比的是 stream=True 利用生成器,它只将当前帧或数据点的结果保存在内存中,从而大大减少了内存消耗并防止出现内存不足的问题。
>
>
>
### 推理源
**1.图像**:在图像文件上运行推理。
from ultralytics import YOLO
Load a pretrained YOLOv8n model
加载预训练的YOLOv8n模型
model = YOLO(‘yolov8n.pt’)
Define path to the image file
定义图像文件的路径
source = ‘path/to/image.jpg’
Run inference on the source
在源图像上运行推理
results = model(source) # list of Results objects
results 是结果对象的列表
**2.截图**:以屏幕截图的形式对当前屏幕内容进行推理。
from ultralytics import YOLO
Load a pretrained YOLOv8n model
model = YOLO(‘yolov8n.pt’)
Define current screenshot as source
source = ‘screen’
Run inference on the source
results = model(source) # list of Results objects
**3.网址**:通过 URL 对远程托管的图像或视频进行推理。
from ultralytics import YOLO
Load a pretrained YOLOv8n model
model = YOLO(‘yolov8n.pt’)
Define remote image or video URL
source = ‘https://ultralytics.com/images/bus.jpg’
Run inference on the source
results = model(source) # list of Results objects
**4.PIL**:在使用Python Imaging Library (PIL) 打开的图像上运行推理。
from PIL import Image
from ultralytics import YOLO
Load a pretrained YOLOv8n model
model = YOLO(‘yolov8n.pt’)
Open an image using PIL
source = Image.open(‘path/to/image.jpg’)
Run inference on the source
results = model(source) # list of Results objects
**5.OpenCV**:在使用 OpenCV 读取的图像上运行推理。
import cv2
from ultralytics import YOLO
Load a pretrained YOLOv8n model
model = YOLO(‘yolov8n.pt’)
Read an image using OpenCV
source = cv2.imread(‘path/to/image.jpg’)
Run inference on the source
results = model(source) # list of Results objects
**6.numpy:在以 numpy 数组表示的图像上运行推理。**
import numpy