yoloV8训练标注数据生成模型

1、标注工具:vott
yoloV8训练标注数据生成模型_第1张图片

2、yoloV8

2.1 仓库地址

https://github.com/ultralytics/ultralytics

2.2 参考教程文档

文档地址:
https://github.com/ultralytics/ultralytics/wiki

3、数据集|标注数据格式


3.1 数据集格式【coco128】


3.1.1 下载地址:

https://cocodataset.org/#download

3.1.2 官网示例:

yoloV8训练标注数据生成模型_第2张图片

3.2 项目配置coco128数据集

将下载的数据集解压至指定位置,*文件路径固定【跟路径下datasets】

path: ../datasets/coco8 
train: images/train
val: images/val

3.2.1 数据集格式转化

该数据集是使用labelme,对单图进行标注的json格式。转为yolo可以使用的格式

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/6/3
# @Author : CCM
# @Describe : 该数据集是使用labelme,对单图进行标注的json格式。转为yolo可以使用的格式

import os
import json
import base64
from PIL import Image
from io import BytesIO


# 辅助函数解码base64图像数据
def decode_image(image_data):
    img_data = base64.b64decode(image_data)
    img = Image.open(BytesIO(img_data))
    return img


# 将多边形点转换为YOLO bbox格式
def polygon_to_yolo_bbox(points, img_width, img_height):
    x_coords = [point[0] for point in points]
    y_coords = [point[1] for point in points]
    xmin = min(x_coords)
    xmax = max(x_coords)
    ymin = min(y_coords)
    ymax = max(y_coords)
    x_center = (xmin + xmax) / 2.0 / img_width
    y_center = (ymin + ymax) / 2.0 / img_height
    width = (xmax - xmin) / img_width
    height = (ymax - ymin) / img_height
    return [x_center, y_center, width, height]


# Paths
input_dir = r'C:\Users\ccm\Documents\印章数据集\印章标注\印章标注'  # 标注文件夹路径
images_dir = '../datasets/seal/images/train2017'
labels_dir = '../datasets/seal/labels/train2017'

# 如果目录不存在,创建目录
os.makedirs(images_dir, exist_ok=True)
os.makedirs(labels_dir, exist_ok=True)

# 处理输入目录中的每个JSON文件
for filename in os.listdir(input_dir):
    if filename.endswith('.json'):
        json_path = os.path.join(input_dir, filename)
        with open(json_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
        image_name = data['imagePath']
        image_data = decode_image(data['imageData'])
        image_path = os.path.join(images_dir, os.path.basename(image_name))
        # 存图
        image_data.save(image_path)
        # 准备标签文件
        label_path = os.path.join(labels_dir, os.path.splitext(os.path.basename(image_name))[0] + '.txt')
        img_width, img_height = image_data.size
        with open(label_path, 'w') as label_file:
            for shape in data['shapes']:
                label = shape['label']
                points = shape['points']
                bbox = polygon_to_yolo_bbox(points, img_width, img_height)
                # 假设所有标签都是'stamp',类id为0
                label_file.write(f"0 {' '.join(map(str, bbox))}\n")
        print(f"处理和保存图像和标签 {image_name}")
print("完成")

3.2.2 项目配置摘要

yoloV8训练标注数据生成模型_第3张图片

4、训练|验证|推理|存储模型

我使用yolov8n.pt 模型进行,根据自身条件选择合适的模型进行训练。

4.1 训练

学习率|训练次数深度|使用模型|数据集配置yaml|等参数 自我配置

yolo detect train data=coco8.yaml model=yolov8n.pt epochs=100 imgsz=640 device=0,1
yolo detect train data=datasets/seal/seal_ccm.yaml model=yolov8n.yaml pretrained=ultralytics/yolov8n.pt epochs=100 batch=2 lr0=0.01 resume=True 

4.2 验证

yolo detect val  data=datasets/seal/seal_ccm.yaml model=runs/detect/train/weights/best.pt batch=2

4.3 训练验证结果

yoloV8训练标注数据生成模型_第4张图片

4.4 评估模型

results = detect.run(weights='runs/train/exp/weights/best.pt', source='data/images', img_size=img_size)
print(results)

4.5 进行推理

detect.run(weights='runs/train/exp/weights/best.pt', source='path/to/your/test/images', img_size=img_size)

4.6 导出为ONNX或其它格式

python export.py --weights runs/train/exp/weights/best.pt --img 640 --batch 1

4.7 模型使用

import torch
from yolov5 import detect

# 加载模型
model = torch.hub.load('ultralytics/yolov5', 'custom', path='runs/train/exp/weights/best.pt')

# 进行推理
img = 'path/to/your/image.jpg'
results = model(img)
results.show()  # 展示结果
results.save()  # 保存结果

5、倾斜弯曲文字识别算法解析

相关流程和代码【后续更新】

你可能感兴趣的:(目标检测,计算机视觉,YOLO,目标检测,计算机视觉)