首页 > 其他分享 >labelme转yolo格式txt 目标检测和实例分割的脚本

labelme转yolo格式txt 目标检测和实例分割的脚本

时间:2024-07-04 10:33:31浏览次数:12  
标签:labelme yolo list json points file txt os dir

labelme标注后的数据转yolo目标检测格式txt的脚本

点击查看代码
# https://blog.csdn.net/m0_63172128/article/details/135942221
import base64
import random
import shutil
from tqdm import tqdm
import math
import json
import os
import numpy as np
import PIL.Image
import PIL.ImageDraw
import cv2


class ConvertManager(object):
    def __init__(self):
        pass

    def base64_to_numpy(self, img_bs64):
        img_bs64 = base64.b64decode(img_bs64)
        img_array = np.frombuffer(img_bs64, np.uint8)
        cv2_img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
        return cv2_img

    @classmethod
    def load_labels(cls, name_file):
        '''
        load names from file.one name one line
        :param name_file:
        :return:
        '''
        with open(name_file, 'r') as f:
            lines = f.read().rstrip('\n').split('\n')
        return lines

    def get_class_names_from_all_json(self, json_dir):
        classnames = []
        for file in os.listdir(json_dir):
            if not file.endswith('.json'):
                continue
            with open(os.path.join(json_dir, file), 'r', encoding='utf-8') as f:
                data_dict = json.load(f)
                for shape in data_dict['shapes']:
                    if not shape['label'] in classnames:
                        classnames.append(shape['label'])
        return classnames

    def create_save_dir(self, save_dir):
        images_dir = os.path.join(save_dir, 'images')
        labels_dir = os.path.join(save_dir, 'labels')
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
            os.mkdir(images_dir)
            os.mkdir(labels_dir)
        else:
            if not os.path.exists(images_dir):
                os.mkdir(images_dir)
            if not os.path.exists(labels_dir):
                os.mkdir(labels_dir)
        return images_dir + os.sep, labels_dir + os.sep

    def save_list(self, data_list, save_file):
        with open(save_file, 'w') as f:
            f.write('\n'.join(data_list))

    def __rectangle_points_to_polygon(self, points):
        xmin = 0
        ymin = 0
        xmax = 0
        ymax = 0
        if points[0][0] > points[1][0]:
            xmax = points[0][0]
            ymax = points[0][1]
            xmin = points[1][0]
            ymin = points[1][1]
        else:
            xmax = points[1][0]
            ymax = points[1][1]
            xmin = points[0][0]
            ymin = points[0][1]
        return [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]

    def convert_dataset(self, json_dir, json_list, images_dir, labels_dir, names, save_mode='train'):
        images_dir = os.path.join(images_dir, save_mode) + os.sep
        labels_dir = os.path.join(labels_dir, save_mode) + os.sep
        if not os.path.exists(images_dir):
            os.mkdir(images_dir)
        if not os.path.exists(labels_dir):
            os.mkdir(labels_dir)
        for file in tqdm(json_list):
            with open(os.path.join(json_dir, file), 'r', encoding='utf-8') as f:
                data_dict = json.load(f)
            image_file = os.path.join(json_dir, os.path.basename(data_dict['imagePath']))
            if os.path.exists(image_file):
                shutil.copyfile(image_file, images_dir + os.path.basename(image_file))
            else:
                imageData = data_dict.get('imageData')
                if not imageData:
                    imageData = base64.b64encode(imageData).decode('utf-8')
                    img = self.img_b64_to_arr(imageData)
                    PIL.Image.fromarray(img).save(images_dir + file[:-4] + 'png')
            # convert to txt
            width = data_dict['imageWidth']
            height = data_dict['imageHeight']
            line_list = []
            for shape in data_dict['shapes']:
                data_list = []
                data_list.append(str(names.index(shape['label'])))
                if shape['shape_type'] == 'rectangle':
                    points = self.__rectangle_points_to_polygon(shape['points'])
                    for point in points:
                        data_list.append(str(point[0] / width))
                        data_list.append(str(point[1] / height))


                elif shape['shape_type'] == 'polygon':
                    points = shape['points']
                    for point in points:
                        data_list.append(str(point[0] / width))
                        data_list.append(str(point[1] / height))
                line_list.append(' '.join(data_list))

            self.save_list(line_list, labels_dir + file[:-4] + "txt")

    def split_train_val_test_dataset(self, file_list, train_ratio=0.9, trainval_ratio=0.9, need_test_dataset=False,
                                     shuffle_list=True):
        if shuffle_list:
            random.shuffle(file_list)
        total_file_count = len(file_list)
        train_list = []
        val_list = []
        test_list = []
        if need_test_dataset:
            trainval_count = int(total_file_count * trainval_ratio)
            trainval_list = file_list[:trainval_count]
            test_list = file_list[trainval_count:]
            train_count = int(train_ratio * len(trainval_list))
            train_list = trainval_list[:train_count]
            val_list = trainval_list[train_count:]
        else:
            train_count = int(train_ratio * total_file_count)
            train_list = file_list[:train_count]
            val_list = file_list[train_count:]
        return train_list, val_list, test_list

    def start(self, json_dir, save_dir, names=None, train_ratio=0.9):
        images_dir, labels_dir = self.create_save_dir(save_dir)
        if names is None or len(names) == 0:
            print('class names will load from all json file')
            names = self.get_class_names_from_all_json(json_dir)
        print('find {} class names :'.format(len(names)), names)
        if len(names) == 0:
            return

        self.save_list(names, os.path.join(save_dir, 'labels.txt'))
        print('start convert')
        all_json_list = []
        for file in os.listdir(json_dir):
            if not file.endswith('.json'):
                continue
            all_json_list.append(file)
        train_list, val_list, test_list = self.split_train_val_test_dataset(all_json_list, train_ratio)
        self.convert_dataset(json_dir, train_list, images_dir, labels_dir, names, 'train')
        self.convert_dataset(json_dir, val_list, images_dir, labels_dir, names, 'val')


if __name__ == '__main__':
    cm = ConvertManager()
    cm.start(r'D:\pic\pcb\shenMangKong\src_detect', r'D:\pic\pcb\shenMangKong\txt_detect')
labelme标注后的数据转yolo实例分割格式txt的脚本
点击查看代码
# -*- coding: utf-8 -*-
import json
import os
import argparse
from tqdm import tqdm
import glob
import cv2
import numpy as np


def convert_label_json(json_dir, save_dir, classes):
    json_paths = os.listdir(json_dir)
    classes = classes.split(',')

    for json_path in tqdm(json_paths):
        # for json_path in json_paths:
        path = os.path.join(json_dir, json_path)
        # print(path)
        with open(path, 'r', encoding='utf-8') as load_f:
            print(load_f)
            #json_dict = json.load(load_f, encoding='utf-8')
            json_dict = json.load(load_f)
        h, w = json_dict['imageHeight'], json_dict['imageWidth']

        # save txt path
        txt_path = os.path.join(save_dir, json_path.replace('json', 'txt'))
        txt_file = open(txt_path, 'w')

        for shape_dict in json_dict['shapes']:
            label = shape_dict['label']
            label_index = classes.index(label)
            points = shape_dict['points']

            points_nor_list = []

            for point in points:
                points_nor_list.append(point[0] / w)
                points_nor_list.append(point[1] / h)

            points_nor_list = list(map(lambda x: str(x), points_nor_list))
            points_nor_str = ' '.join(points_nor_list)

            label_str = str(label_index) + ' ' + points_nor_str + '\n'
            txt_file.writelines(label_str)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='json convert to txt params')
    parser.add_argument('--json-dir', type=str, default='dataset/t1', help='json path dir')
    parser.add_argument('--save-dir', type=str, default='dataset/t2', help='txt save dir')
    parser.add_argument('--classes', type=str, default='Primary_Particle', help='classes') # 设置标签名
    args = parser.parse_args()
    json_dir = args.json_dir
    save_dir = args.save_dir
    classes = args.classes
    convert_label_json(json_dir, save_dir, classes)


标签:labelme,yolo,list,json,points,file,txt,os,dir
From: https://www.cnblogs.com/SunshineWeather/p/18283088

相关文章

  • 分别使用CMAKE和CLION编译,同一个cmakelists.txt, 为什么clion出错和cmake正常?clion出
    求助!!我在github上找到了一个大型的应用软件的开源代码,使用CMAKE编译,再用VS2017以生成应用程序。因为想改代码,所以使用了CLion在本地运行。但是cmake能够正常通过的文件代码,clion却出错。用的同一个cmakelists.txt,请问为什么clion出错和cmake正常呢?求求~改动了很久cmakelist......
  • 智慧课堂基于YOLOv8的学生上课行为检测
    数据集学生上课行为检测,我们直接使用公开数据集共三类行为:举手、读书、写字数据集已经按照YOLO格式配置好,数据内容如下模型训练​采用YOLOv8模型进行训练,官方代码首先是划分数据集,分为训练集、验证,已经划分好之后修改数据集配置文件:需要修改数据集路径以及各个......
  • Yolo
    ONNXformat是什么?ONNX(OpenNeuralNetworkExchange),中文名是开放神经网络交换格式,是一种针对机器学习所设计的开放式的文件格式,用于存储训练好的模型。它使得不同的人工智能框架(如PyTorch、MXNet)可以采用相同格式存储模型数据并交互。ONNX的规范及代码主要由微软、亚马逊、Face......
  • 基于YOLOv5的人脸关键点检测(附代码)
    人脸关键点检测项目说明本项目的实现主要依靠两个算法:yolov5目标检测和resnet人脸关键点算法。其中目标检测算法为人脸关键点检测算法的前置算法,使用目标检测算法将人脸信息进行提取(起到前景与背景的分离),然后再对box内的人脸信息进行关键点检测。本项目支持功能:人脸关键......
  • 海思3559 yolov5模型转wk详细笔记
    文章目录   前言   1.编译caffer       1.1安装虚拟机       1.2安装caffer       1.3编译python接口   2.适应wk的yolov5模型训练       2.1下载yolov5-6.0项目源码       2.2安装yolov5-6.0运行环境       2.3修改......
  • YOLOv8改进 | 卷积模块 | 减少冗余计算和内存访问的PConv【CVPR2023】
    秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 ......
  • YOLOv5改进 | 主干网络 | ODConv + ConvNeXt 增强目标特征提取能力
    秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 ......
  • YOLOv10全网最新创新点改进系列:YOLOv10+ICCV 2023 - 动态蛇形卷积(Dynamic Snake Convo
    YOLOv10全网最新创新点改进系列:YOLOv10+ICCV2023-动态蛇形卷积(DynamicSnakeConvolution)采用管状结构,拉升模型小目标、遮挡目标检测效果!所有改进代码均经过实验测试跑通!截止发稿时YOLOv10已改进40+!自己排列组合2-4种后,考虑位置不同后可排列组合上千万种!改进不重样!!专注A......
  • YOLOv10添加输出各类别训练过程指标
    昨天有群友,在交流群【群号:392784757】里提到了这个需求,进行实现一下V10官方代码结构相较于V8稍微复杂一些yolov10是基于v8的代码完成开发,yolov10进行了继承来简化代码开发因此V10的代码修改基本和V8这篇一致https://blog.csdn.net/csy1021/article/details/1344......
  • YOLOv10改进 | 注意力篇 | YOLOv10引入24年最新Mamba注意力机制MLLAttention
    1. MLLAttention介绍1.1 摘要: Mamba是一种有效的状态空间模型,具有线性计算复杂度。最近,它在处理各种视觉任务的高分辨率输入方面表现出了令人印象深刻的效率。在本文中,我们揭示了强大的Mamba模型与线性注意力Transformer具有惊人的相似之处,而线性注意力Transform......