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)

三、结果显示

收工!!

相关推荐
FL16238631291 小时前
遥感图像塔吊检测数据集VOC+YOLO格式1567张1类别
深度学习·yolo·机器学习
LeeZhao@7 小时前
【项目】多模态图文理解-GLM-Edge实战
人工智能·yolo·计算机视觉·语言模型·aigc
程序媛晓晓13 小时前
yolo小damo合集
yolo
深蓝海拓14 小时前
基于深度学习的视觉检测小项目(六) 项目的信号和变量的规划
pytorch·深度学习·yolo·视觉检测·pyqt
m0_7482487717 小时前
YOLOv5部署到web端(flask+js简单易懂)
前端·yolo·flask
大学生毕业题目18 小时前
毕业项目推荐:基于yolov8/yolov5的行人检测识别系统(python+卷积神经网络)
python·yolo·cnn
goomind1 天前
YOLOv8实战人员跌倒检测
人工智能·深度学习·yolo·计算机视觉·目标跟踪·跌倒检测
彭世瑜1 天前
C语言:cJSON将struct结构体与JSON互相转换
c语言·开发语言·json
小李学AI1 天前
基于YOLOv8的道路缺陷检测系统
人工智能·深度学习·神经网络·yolo·目标检测·机器学习·计算机视觉
LBJ辉2 天前
第 23 章 JSON
开发语言·前端·javascript·json·ecmascript