Convert semantic segmentation data set to yolo format data set

Preface

Recently, I have been doing real-time calculations. I initially used semantic segmentation unet, and found that its inference was very slow under server-level GPUs, but the accuracy was pretty good. But what we are pursuing is real-time performance, so we still use target detection. Finally selected yolov5

As we all know about the yolov5 data set, we need to know each anchor box, which requires image and txt, and my semantic segmentation data set has a json file,
as shown in the figure
Insert image description here
#!/usr/bin/python

8 annotation folder below Below is what some json format files
look like in json format:

Insert image description here
There is only one category here. Points are the coordinates of some boxes marked in the original picture (not marked with labelme). For example, there is a mouse hole in my land, and I need to mark it, so it is generated. json format file

import argparse
import json
import os
import os.path as osp
import base64
import warnings
import PIL.Image
import yaml
from labelme import utils

def main():
	json_file = "D:/sd/裁切/8标注"

# freedom
list_path = os.listdir(json_file)
print('freedom =', json_file)
for i in range(0, len(list_path)):
    path = os.path.join(json_file, list_path[i])
    if os.path.isfile(path):

        data = json.load(open(path))
        img = utils.img_b64_to_arr(data['imageData'])
        lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])

        captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]

        lbl_viz = utils.draw_label(lbl, img, captions)
        # out_dir = osp.basename(path).replace('.', '_')
        out_dir = osp.basename(path).split('.json')[0]
        save_file_name = out_dir
        # out_dir = osp.join(osp.dirname(path), out_dir)

        if not osp.exists(json_file + 'mask'):
            os.mkdir(json_file + 'mask')
        maskdir = json_file + 'mask'

        if not osp.exists(json_file + 'mask_viz'):
            os.mkdir(json_file + 'mask_viz')
        maskvizdir = json_file + 'mask_viz'

        out_dir1 = maskdir
        # if not osp.exists(out_dir1):
        #     os.mkdir(out_dir1)

        # PIL.Image.fromarray(img).save(out_dir1 + '\\' + save_file_name + '_img.png')
        PIL.Image.fromarray(lbl).save(out_dir1 + '/' + save_file_name + '.png')

        PIL.Image.fromarray(lbl_viz).save(maskvizdir + '/' + save_file_name +
                                          '_label_viz.png')


        with open(osp.join(out_dir1, 'label_names.txt'), 'w') as f:
            for lbl_name in lbl_names:
                f.write(lbl_name + '\n')

        warnings.warn('info.yaml is being replaced by label_names.txt')
        info = dict(label_names=lbl_names)
        with open(osp.join(out_dir1, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        print('Saved to: %s' % out_dir1)
if __name__ == '__main__':
# base64path = argv[1]
	main()

Conversion result:

We generated a txt file corresponding to the image,
Insert image description here
such as 100_0001_0007_1_4.jpg, which corresponds to the txt in the above picture, so our approach saves you from using labelme to label one by one (if you label one by one, it is estimated that it will take a year to detect so many object), so this python script is still very successful.
txt rendering:

The first one is the category, because there is only one category, and the following ones are the values ​​of each anchor box.

thank you all

Guess you like

Origin blog.csdn.net/weixin_51038781/article/details/121938265