labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割

参考:https://github.com/wkentaro/labelme 

一.labelme标注文件转coco json

1.标注时带图片ImageData信息,将一个文件夹下的照片和labelme的标注文件,分成了train和val的coco json文件和照片 

import os
import json
import numpy as np
import glob
import shutil
from sklearn.model_selection import train_test_split
from labelme import utils

np.random.seed(41)

#0为背景
classname_to_id = {"Red": 1}

class Lableme2CoCo:

    def __init__(self):
        self.images = []
        self.annotations = []
        self.categories = []
        self.img_id = 0
        self.ann_id = 0

    def save_coco_json(self, instance, save_path):
        json.dump(instance, open(save_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=1)  # indent=2 更加美观显示

    # 由json文件构建COCO
    def to_coco(self, json_path_list):
        self._init_categories()
        for json_path in json_path_list:
            obj = self.read_jsonfile(json_path)
            self.images.append(self._image(obj, json_path))
            shapes = obj['shapes']
            for shape in shapes:
                annotation = self._annotation(shape)
                self.annotations.append(annotation)
                self.ann_id += 1
            self.img_id += 1
        instance = {}
        instance['info'] = 'spytensor created'
        instance['license'] = ['license']
        instance['images'] = self.images
        instance['annotations'] = self.annotations
        instance['categories'] = self.categories
        return instance

    # 构建类别
    def _init_categories(self):
        for k, v in classname_to_id.items():
            category = {}
            category['id'] = v
            category['name'] = k
            self.categories.append(category)

    # 构建COCO的image字段
    def _image(self, obj, path):
        image = {}

        img_x = utils.img_b64_to_arr(obj['imageData'])
        h, w = img_x.shape[:-1]
        image['height'] = h
        image['width'] = w
        image['id'] = self.img_id
        image['file_name'] = os.path.basename(path).replace(".json", ".jpg")
        return image

    # 构建COCO的annotation字段
    def _annotation(self, shape):
        label = shape['label']
        points = shape['points']
        annotation = {}
        annotation['id'] = self.ann_id
        annotation['image_id'] = self.img_id
        annotation['category_id'] = int(classname_to_id[label])
        annotation['segmentation'] = [np.asarray(points).flatten().tolist()]
        annotation['bbox'] = self._get_box(points)
        annotation['iscrowd'] = 0
        annotation['area'] = annotation['bbox'][-1]*annotation['bbox'][-2]
        return annotation

    # 读取json文件,返回一个json对象
    def read_jsonfile(self, path):
        with open(path, "r", encoding='utf-8') as f:
            return json.load(f)

    # COCO的格式: [x1,y1,w,h] 对应COCO的bbox格式
    def _get_box(self, points):
        min_x = min_y = np.inf
        max_x = max_y = 0
        for x, y in points:
            min_x = min(min_x, x)
            min_y = min(min_y, y)
            max_x = max(max_x, x)
            max_y = max(max_y, y)
        return [min_x, min_y, max_x - min_x, max_y - min_y]


if __name__ == '__main__':
    #将一个文件夹下的照片和labelme的标注文件,分成了train和val的coco json文件和照片
    labelme_path = './data/fzh_img_label_example'
    train_img_out_path='./train_img'
    val_img_out_path = './val_img'
    if not (os.path.exists(train_img_out_path) and os.path.exists(val_img_out_path)):
        os.mkdir(train_img_out_path)
        os.mkdir(val_img_out_path)

    # 获取images目录下所有的joson文件列表
    json_list_path = glob.glob(labelme_path + "/*.json")
    print('json_list_path=', json_list_path)
    # 数据划分,这里没有区分val2017和tran2017目录,所有图片都放在images目录下
    train_path, val_path = train_test_split(json_list_path, test_size=0.5)
    print("train_n:", len(train_path), 'val_n:', len(val_path))
    print('train_path=',train_path)
    # 把训练集转化为COCO的json格式
    l2c_train = Lableme2CoCo()
    train_instance = l2c_train.to_coco(train_path)
    l2c_train.save_coco_json(train_instance, 'train.json')

    # 把验证集转化为COCO的json格式
    l2c_val = Lableme2CoCo()
    val_instance = l2c_val.to_coco(val_path)
    l2c_val.save_coco_json(val_instance, 'val.json')


    for file in train_path:
        shutil.copy(file.replace("json",'png' or "jpg"),train_img_out_path)
    for file in val_path:
        shutil.copy(file.replace("json",'png' or "jpg"),val_img_out_path)

labelme标注好的文件和json格式 

labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第1张图片

生成的训练集                                                                         生成的验证集

labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第2张图片       labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第3张图片

生成的训练集和验证集的json文件

labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第4张图片

label me标注的json格式                                                             转好的coco json格式

labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第5张图片labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第6张图片

2.标注时图片不带ImageData信息

import os
import json
import numpy as np
import glob
import shutil
from sklearn.model_selection import train_test_split
from labelme import utils
import cv2

np.random.seed(41)

# 0为背景
classname_to_id = {"person": 1,"bus": 6}

class Lableme2CoCo:

    def __init__(self):
        self.images = []
        self.annotations = []
        self.categories = []
        self.img_id = 0
        self.ann_id = 0

    def save_coco_json(self, instance, save_path):
        json.dump(instance, open(save_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=1)  # indent=2 更加美观显示

    # 由json文件构建COCO
    def to_coco(self, json_path_list):
        self._init_categories()
        for json_path in json_path_list:
            obj = self.read_jsonfile(json_path)
            self.images.append(self._image(obj, json_path))
            shapes = obj['shapes']
            for shape in shapes:
                annotation = self._annotation(shape)
                self.annotations.append(annotation)
                self.ann_id += 1
            self.img_id += 1
        instance = {}
        instance['info'] = 'spytensor created'
        instance['license'] = ['license']
        instance['images'] = self.images
        instance['annotations'] = self.annotations
        instance['categories'] = self.categories
        return instance

    # 构建类别
    def _init_categories(self):
        for k, v in classname_to_id.items():
            category = {}
            category['id'] = v
            category['name'] = k
            self.categories.append(category)

    # 构建COCO的image字段
    def _image(self, obj, path):
        image = {}
        img_path = path.replace(".json", ".jpg")
        # img_x = utils.img_b64_to_arr(obj['imageData'])
        # h, w = img_x.shape[:-1]
        # print("img_path:",img_path)
        img_x = cv2.imread(img_path)
        h, w = img_x.shape[:-1]
        image['height'] = h
        image['width'] = w
        image['id'] = self.img_id
        image['file_name'] = os.path.basename(path).replace(".json", ".jpg")
        return image

    # 构建COCO的annotation字段
    def _annotation(self, shape):
        label = shape['label']
        points = shape['points']
        annotation = {}
        annotation['id'] = self.ann_id
        annotation['image_id'] = self.img_id
        annotation['category_id'] = int(classname_to_id[label])
        annotation['segmentation'] = [np.asarray(points).flatten().tolist()]
        annotation['bbox'] = self._get_box(points)
        annotation['iscrowd'] = 0
        annotation['area'] = annotation['bbox'][-1] * annotation['bbox'][-2]
        return annotation

    # 读取json文件,返回一个json对象
    def read_jsonfile(self, path):
        with open(path, "r", encoding='utf-8') as f:
            return json.load(f)

    # COCO的格式: [x1,y1,w,h] 对应COCO的bbox格式
    def _get_box(self, points):
        min_x = min_y = np.inf
        max_x = max_y = 0
        for x, y in points:
            min_x = min(min_x, x)
            min_y = min(min_y, y)
            max_x = max(max_x, x)
            max_y = max(max_y, y)
        return [min_x, min_y, max_x - min_x, max_y - min_y]


if __name__ == '__main__':
    # 将一个文件夹下的照片和labelme的标注文件,分成了train和val的coco json文件和照片
    labelme_path = './data'
    train_img_out_path = './train_img'
    val_img_out_path = './val_img'
    if not (os.path.exists(train_img_out_path) and os.path.exists(val_img_out_path)):
        os.mkdir(train_img_out_path)
        os.mkdir(val_img_out_path)

    # 获取images目录下所有的joson文件列表
    json_list_path = glob.glob(labelme_path + "/*.json")
    print('json_list_path=', json_list_path)
    # 数据划分,这里没有区分val2017和tran2017目录,所有图片都放在images目录下
    train_path, val_path = train_test_split(json_list_path, test_size=0)
    print("train_n:", len(train_path), 'val_n:', len(val_path))
    print('train_path=', train_path)
    # 把训练集转化为COCO的json格式
    l2c_train = Lableme2CoCo()
    train_instance = l2c_train.to_coco(train_path)
    l2c_train.save_coco_json(train_instance, 'train.json')

    # 把验证集转化为COCO的json格式
    l2c_val = Lableme2CoCo()
    val_instance = l2c_val.to_coco(val_path)
    l2c_val.save_coco_json(val_instance, 'val.json')

    for file in train_path:
        shutil.copy(file.replace("json", "jpg"), train_img_out_path)
    for file in val_path:
        shutil.copy(file.replace("json",  "jpg"), val_img_out_path)

二.coco josn转yolo txt格式

from __future__ import print_function
import os, sys, zipfile
import json


def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]

    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)


json_file = 'train.json'  # # Object Instance 类型的标注

data = json.load(open(json_file, 'r'))

ana_txt_save_path = "./new"  # 保存的路径
if not os.path.exists(ana_txt_save_path):
    os.makedirs(ana_txt_save_path)

for img in data['images']:
    # print(img["file_name"])
    filename = img["file_name"]
    img_width = img["width"]
    img_height = img["height"]
    # print(img["height"])
    # print(img["width"])
    img_id = img["id"]
    ana_txt_name = filename.split(".")[0] + ".txt"  # 对应的txt名字,与jpg一致
    print(ana_txt_name)
    f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')
    for ann in data['annotations']:
        if ann['image_id'] == img_id:
            # annotation.append(ann)
            # print(ann["category_id"], ann["bbox"])
            box = convert((img_width, img_height), ann["bbox"])
            f_txt.write("%s %s %s %s %s\n" % (ann["category_id"], box[0], box[1], box[2], box[3]))
    f_txt.close()

labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第7张图片

labelme标注文件转coco json+图片也自动分成训练集与验证集,coco json转yolo txt格式,labelme标注文件转分割_第8张图片

三.labelme标注文件转成分割图片

import json
import os
import os.path as osp
import numpy as np
import PIL.Image
import yaml

from labelme.logger import logger
from labelme import utils
import cv2

def main():
    img_path = './002.jpg'
    json_file = './002.json'
    output_path = './output'
    if not os.path.exists(output_path):
        os.mkdir(output_path)

    json_data = json.load(open(json_file))

    img = cv2.imread(img_path)
    print('img.shape:',img.shape)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(json_data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        print('label_name:',label_name)
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            print('label_value:',label_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, json_data['shapes'], label_name_to_value)
    cv2.imwrite(output_path+'/'+img_path.split('/')[-1], lbl * 255)

你可能感兴趣的:(目标检测)