跳转到主要内容

yolov8图像识别

官方网址

https://docs.ultralytics.com/

安装

# 使用清华大学源加速
https://pypi.tuna.tsinghua.edu.cn/simple/ 

# 查看已经安装的模块
pip list

# 卸载模块
pip uninstall <package-Name>

# 安装opencv
pip install python-opencv -i https://pypi.tuna.tsinghua.edu.cn/simple/

# 安装opencv 扩展
pip install opencv-contrib-python  -i https://pypi.tuna.tsinghua.edu.cn/simple/

# 安装pytorch
# https://pytorch.org/get-started/locally/
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple/

# 安装yolov8
# https://docs.ultralytics.com/quickstart/
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple/

使用命令行

# 命令行格式
yolo TASK MODE ARGS

# TASK:[detect : 侦测], [segment :分割], [classify :分类], [pose :姿态]
# MODE :[train:训练], [val:验证], [predict:预测/测试], [export:导出], [track:跟踪]
# 使用yolov8n.pt 预测图片
yolo detect predict model="./yolo/yolov8n.pt" source="./images/1.jpg"

# 使用yolov8n-seg.pt 分割图片
yolo segment predict model="./yolo/yolov8n-seg.pt" source="./images/1.jpg"

image-20240425224143608

训练模型

# 使用coco128数据集进行模型训练
yolo detect train data=./yolo/coco128.yaml model=./yolo/yolov8n.pt epochs=100 imgsz=640
# 数据集实际上一个yaml配置文件
# 文件示例:https://github.com/ultralytics/ultralytics/tree/main/ultralytics/cfg/datasets
# coco128数据集

path: ../datasets/coco128 # dataset root dir
train: images/train2017 # train images (relative to 'path') 128 images
val: images/train2017 # val images (relative to 'path') 128 images
test: # test images (optional)

# Classes
names:
  0: person
  1: bicycle
 
download: https://ultralytics.com/assets/coco128.zip

python代码

from ultralytics import YOLO

model = YOLO('./yolo/yolov8n.pt')  # load a pretrained model (recommended for training)
results = model.train(data='./images/coco128.yaml', epochs=100, imgsz=640)