【实例分割】YOLO自定义数据集准备:COCO转YOLO(可视化标注)

YOLOv5实例分割数据集准备:

1. 将COCO格式JSON文件划分为多个LabelMe格式文件

【目标检测】JSON转换:COCO格式转LabelMe格式(大JSON文件划分)_ericdiii的博客-CSDN博客将大JSON文件划分为每张图片对应的标注文件https://blog.csdn.net/ericdiii/article/details/128281336?spm=1001.2014.3001.5502

2. 批量转换:LabelMe to YOLO

import glob
import numpy as np
import json
import os
import cv2

json_path = r"coco2labelme/train"
json_files = glob.glob(json_path + "/*.json")
for json_file in json_files:
    print(json_file)
    f = open(json_file)
    json_info = json.load(f)
    # print(json_info.keys())
    img = cv2.imread(os.path.join(json_path, json_info["imagePath"]))
    height, width, _ = img.shape
    np_w_h = np.array([[width, height]], np.int32)
    txt_file = json_file.replace(".json", ".txt")
    f = open(txt_file, "a")
    for point_json in json_info["shapes"]:
        txt_content = ""
        np_points = np.array(point_json["points"], np.int32)
        label = point_json["label"]
        if label == 'class 1':
            label_index = 0
        elif label == 'class 2':
            label_index  = 1
        elif label == 'class 3':
            label_index  = 2
        np_points = np.array(point_json["points"], np.int32)
        norm_points = np_points / np_w_h
        norm_points_list = norm_points.tolist()
        txt_content += f"{label_index} " + " ".join([" ".join([str(cell[0]), str(cell[1])]) for cell in norm_points_list]) + "\n"
        f.write(txt_content)

3. 可视化以检查数据集标注

import cv2
import numpy as np

pic_path = r"coco2labelme/val/1.jpg"
txt_path = r"coco2labelme/val/1.txt"

img = cv2.imread(pic_path)
height, width, _ = img.shape

file_handle = open(txt_path)
cnt_info = file_handle.readlines()
new_cnt_info = [line_str.replace("\n", "").split(" ") for line_str in cnt_info]

color_map = [(0, 255, 255), (255, 0, 255), (255, 255, 0)]
for new_info in new_cnt_info:
    s = []
    for i in range(1, len(new_info), 2):
        b = [float(tmp) for tmp in new_info[i:i + 2]]
        s.append([int(b[0] * width), int(b[1] * height)])
    class_ = new_info[0]
    index = int(class_)
    cv2.polylines(img, [np.array(s, np.int32)], True, color_map[index], thickness = 3)

cv2.imshow('img2', img)
cv2.waitKey()

对子文件夹内所有数据的标注进行可视化并保存:

import cv2
import numpy as np
import glob

# path
pic_path = r"images/train/"
txt_path = r"labels/train/"

pic = glob.glob(pic_path + "*.jpg")

for pic_file in pic:
    img = cv2.imread(pic_file)
    substrings = pic_file.split('\\')
    substrings = substrings[1].split('.')
    num=substrings[0]
    height, width, _ = img.shape
    txt_file = txt_path + num + ".txt"
    file_handle = open(txt_file)
    cnt_info = file_handle.readlines()
    new_cnt_info = [line_str.replace("\n", "").split(" ") for line_str in cnt_info]

    color_map = [(0, 255, 255), (255, 0, 255), (255, 255, 0)]
    for new_info in new_cnt_info:
        s = []
        for i in range(1, len(new_info), 2):
            b = [float(tmp) for tmp in new_info[i:i + 2]]
            s.append([int(b[0] * width), int(b[1] * height)])
        class_ = new_info[0]
        index = int(class_)
        cv2.polylines(img, [np.array(s, np.int32)], True, color_map[index], thickness = 3)

    save_path = 'visual/train/' + num + '.jpg'
    cv2.imwrite(save_path, img)

参考:[1]

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
COCO格式的数据换为YOLO格式需要进行以下步骤: 1. 读取COCO格式的标注文件,包括图像路径、图像宽度、图像高度、类别标签和边界框坐标等信息。 2. 根据类别标签生成类别列表,并将类别标签换为对应的整数编号。 3. 将边界框坐标换为YOLO格式的相对坐标,即中心坐标和宽高比例,并将相对坐标换为整数格式,以便于后续处理。 4. 将每个图像的标注信息存储为一行文本,并将图像路径和标注信息分别保存为两个文件。 下面是一个Python示例代码,用于将COCO格式的标注文件换为YOLO格式: ```python import json # 读取COCO格式的标注文件 with open('coco_annotations.json', 'r') as f: coco_data = json.load(f) # 生成类别列表 categories = coco_data['categories'] class_names = {} for category in categories: class_names[category['id']] = category['name'] # 标注信息为YOLO格式 yolo_data = [] for annotation in coco_data['annotations']: image_id = annotation['image_id'] image_width = coco_data['images'][image_id]['width'] image_height = coco_data['images'][image_id]['height'] class_id = annotation['category_id'] class_label = class_names[class_id] bbox = annotation['bbox'] x, y, w, h = bbox x_center = x + w / 2 y_center = y + h / 2 x_rel = x_center / image_width y_rel = y_center / image_height w_rel = w / image_width h_rel = h / image_height yolo_line = f"{class_id} {x_rel:.6f} {y_rel:.6f} {w_rel:.6f} {h_rel:.6f}" yolo_data.append((f"{image_id}.jpg", yolo_line)) # 保存YOLO格式的标注文件 with open('yolo_annotations.txt', 'w') as f: for data in yolo_data: f.write(f"{data[0]} {data[1]}\n") ``` 其中,`coco_annotations.json`是COCO格式的标注文件,`yolo_annotations.txt`是换后的YOLO格式标注文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值