YOLO分割数据集转化(json转TXT)

一、数据集转化

python 复制代码
import json
import os
from tqdm import tqdm
import glob
import os.path as osp

def json_to_txt(jsonfilePath, resultDirPath):
    """
    jsonfilePath: labelme标注好的*.json文件所在文件夹
    resultDirPath: 转换好后的*.txt保存文件夹
    """

    class_names = []
    jsonfileList = glob.glob(osp.join(jsonfilePath, "*.json"))

    for jsonfile in tqdm(jsonfileList, desc='Processing'):
        with open(jsonfile, "r", encoding='UTF-8') as f:
            file_in = json.load(f)
            shapes = file_in["shapes"]
            txt_file = osp.join(resultDirPath, osp.basename(jsonfile).replace(".json", ".txt"))

            with open(txt_file, "w") as file_handle:
                for shape in shapes:
                    class_name = shape["label"]
                    if class_name not in class_names:
                        class_names.append(class_name)
                    class_id = class_names.index(class_name)

                    points = shape["points"]
                    normalized_points = [
                        f"{point[0] / file_in['imageWidth']} {point[1] / file_in['imageHeight']}"
                        for point in points
                    ]

                    file_handle.write(f"{class_id} {' '.join(normalized_points)}\n")

    with open(osp.join(resultDirPath, 'classes.txt'), 'w') as f:
        f.write('\n'.join(class_names))

if __name__ == "__main__":
    jsonfilePath = r"E:\241231数据扩充图片\标签"  # 要转换的json文件所在目录
    resultDirPath = r"D:\TXT"  # 要生成的txt文件夹
    json_to_txt(jsonfilePath=jsonfilePath, resultDirPath=resultDirPath)

二、TXT标签验证

验证转化后的的标签是否正确,代码如下:

python 复制代码
import os
import numpy as np
import cv2

IMG_FORMATS = ['.jpg', '.png', '.jpeg']
COLORS = [(0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), (0, 128, 128),
          (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128),
          (64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128),
          (128, 64, 12)]

def get_files(img_path, label_path):
    """
    获取图像和标签文件列表
    """
    img_list = [i for i in os.listdir(img_path) if os.path.splitext(i)[-1].lower() in IMG_FORMATS]
    label_list = [i.replace('.txt', '') for i in os.listdir(label_path) if i.endswith('.txt')]

    assert img_list, f"在 {img_path} 中未找到图像文件"
    assert label_list, f"在 {label_path} 中未找到标签文件"

    img_without_label = set(i.rsplit('.', 1)[0] for i in img_list) - set(label_list)
    if img_without_label:
        print(f"没有对应标签的图像: {list(img_without_label)}")

    ok_img = list(set(i.rsplit('.', 1)[0] for i in img_list) - img_without_label)
    return ok_img, os.path.splitext(img_list[0])[-1].lower()

def convert_and_draw(x, w, h, img):
    """
    转换坐标并绘制多边形和标签
    """
    label, *points = x
    points = np.array(points, dtype=np.float32).reshape(-1, 2) * [w, h]
    points = points.astype(np.int32)

    color = COLORS[int(label) % len(COLORS)]
    cv2.fillPoly(img, [points], color=color)
    cv2.putText(img, text=str(int(label)), org=tuple(points[0]), fontFace=cv2.FONT_HERSHEY_COMPLEX,
                fontScale=1, color=color, thickness=3)

    return img

def main(img_path, label_path, outfile, if_show, show_num):
    if os.path.isdir(img_path):
        ok_img, img_ext = get_files(img_path, label_path)
        for i, img_name in enumerate(ok_img, 1):
            img = cv2.imread(os.path.join(img_path, img_name + img_ext))
            h, w = img.shape[:2]
            with open(os.path.join(label_path, img_name + '.txt'), 'r') as f:
                labels = [x.split() for x in f.read().strip().splitlines()]
            for label in labels:
                img = convert_and_draw(label, w, h, img)
            if if_show:
                cv2.namedWindow('Image with label', cv2.WINDOW_NORMAL)
                cv2.imshow('Image with label', img)
                cv2.waitKey(0)
            if i == show_num:
                break
    else:
        img = cv2.imread(img_path)
        h, w = img.shape[:2]
        with open(label_path, 'r') as f:
            labels = [x.split() for x in f.read().strip().splitlines()]
        for label in labels:
            img = convert_and_draw(label, w, h, img)
        if if_show:
            cv2.namedWindow('Image with label', cv2.WINDOW_NORMAL)
            cv2.imshow('Image with label', img)
            cv2.waitKey(0)

if __name__ == '__main__':
    # 修改输入图片文件夹
    img_path = r"C:\Users\Admin\Desktop\ultralytics-8.2.87\datasets\coco8-seg\images\train/000000000034.jpg"
    # 修改输入标签文件夹
    label_path = r"C:\Users\Admin\Desktop\ultralytics-8.2.87\datasets\coco8-seg\labels\train/000000000034.txt"
    # 输出文件夹
    outfile = './'
    # 是否展示绘制的图片
    if_show = True
    # 最大展示图片的数量(按空格切换)
    show_num = 3

    main(img_path, label_path, outfile, if_show, show_num)

三、结果显示

收工!!

相关推荐
yidaqiqi2 小时前
[目标检测] YOLO系列算法讲解
算法·yolo·目标检测
小吕学编程3 小时前
Jackson使用详解
java·javascript·数据库·json
双叶8365 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)(网页版预告)(html)(js)(json)
c语言·javascript·数据结构·html·json
hao_wujing15 小时前
YOLOv8在单目向下多车辆目标检测中的应用
人工智能·yolo·目标检测
Dneccc21 小时前
YOLO+UI(C#)开发
yolo
电商数据girl1 天前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
南斯拉夫的铁托1 天前
labelimg安装及使用指南(yolo)
开发语言·python·yolo
MUTA️2 天前
ultalytics代码中模型接收多层输入的处理
深度学习·算法·yolo·机器学习·计算机视觉
bing_1582 天前
Spring MVC 根据请求头 (如 Accept) 怎么返回 JSON 或 XML 数据?
spring·json·mvc
半路_出家ren2 天前
python处理异常,JSON
python·json·异常处理