将Labelme标注的数据做成COCO格式的数据集(实例分割的
数据集)
这⾥说明⼀下:
Labelme标注数据时候是⽤的多边形框,关于标注,可以看前⾯的博客⽂章
下⾯制作的COCO数据集是⽤于实例分割的数据集。
文件名提取COCO格式数据集的制作
1、labelme标注的数据转coco数据集
Anaconda Prompt⾥ F:\rockdata 下的⽬录运⾏指令:
这⾥需要注意是在activate labelme后,
python labelme2coco.py NoObeject
下载:labelme2coco.py 代码,运⾏,⽆需修改。
运⾏代码会⽣成⼀个⽂件,trainval.json
代码参考:
github/Tony607/labelme2coco
视频参考:
2、labelme转coco数据集
源代码运⾏后,train和val⽂件夹下为空,原因是:写⼊图像的路径可能不对,修改源代码后正常:代码如下:
import os
import json
import numpy as np
import glob
import shutil
import cv2
del_selection import train_test_split
np.random.seed(41)
#rock1
#rock2
#rock3
#rock4
#rock5
#rock6
#rock7
#rock8
#rock9
#rock10
#rock11
#sand_wave1
#sand_wave2
#sand_wave3
#sand_wave4
#sand_wave5
#sand_wave7
#sand_wave8
#sand_wave9
#sand_wave10
#sand_wave11
# 0为背景
classname_to_id = {
"1": 1,
"2": 2,
}
#注意这⾥:yxf
#需要从1开始把对应的Label名字写⼊:这⾥根据⾃⼰的Lable名字修改
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 = ad_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 = {}
from labelme import utils
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")
# 构建COCO的annotation字段
def _annotation(self, shape):
# print('shape', 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'] = 1.0
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_path = "../../../xianjin_data-3/"
#这⾥注意:yxf
#需要把labelme_path修改为⾃⼰放images和json⽂件的路径
labelme_path = "F:\\rockdata\\NoObeject\\"
#saved_coco_path = "../../../xianjin_data-3/"
saved_coco_path = "F:\\rockdata\\COCO\\"
#saved_coco_path = "./"
#要把saved_coco_path修改为⾃⼰放⽣成COCO的路径,这⾥会在我当前COCO的⽂件夹下建⽴⽣成coco⽂件夹。 print('')
# 创建⽂件
if not ists("%scoco/annotations/" % saved_coco_path):
os.makedirs("%scoco/annotations/" % saved_coco_path)
if not ists("%scoco/images/train2017/" % saved_coco_path):
os.makedirs("%scoco/images/train2017" % saved_coco_path)
if not ists("%scoco/images/val2017/" % saved_coco_path):
os.makedirs("%scoco/images/val2017" % saved_coco_path)
# 获取images⽬录下所有的joson⽂件列表
print(labelme_path + "/*.json")
json_list_path = glob.glob(labelme_path + "/*.json")
print('json_list_path: ', len(json_list_path))
# 数据划分,这⾥没有区分val2017和tran2017⽬录,所有图⽚都放在images⽬录下
train_path, val_path = train_test_split(json_list_path, test_size=0.1, train_size=0.9)
#这⾥yxf:将训练集和验证集的⽐例是9:1,可以根据⾃⼰想要的⽐例修改。
print("train_n:", len(train_path), 'val_n:', len(val_path))
# 把训练集转化为COCO的json格式
l2c_train = Lableme2CoCo()
train_instance = _coco(train_path)
l2c_train.save_coco_json(train_instance, '%scoco/annotations/instances_train2017.json' % saved_coco_path)
for file in train_path:
#place("json", "jpg"), "%scoco/images/train2017/" % saved_coco_path)
#print("这⾥测试⼀下file:"+file)
img_name = place('json', 'png')
#print("这⾥测试⼀下img_name:" + img_name)
temp_img = cv2.imread(img_name)
#print(temp_img) 测试图像读取是否正确
try:
#这个这句是原来作者的代码,运⾏之后train⽂件夹下⽣成的是空的
#cv2.imwrite("{}coco/images/train2017/{}".format(saved_coco_path, place('png', 'jpg')),temp_img)
#我⾃⼰放train图像的路径:F:\rockdata\COCO\coco\images\train2017
img_name_jpg=place('png', 'jpg')
print("jpg测试:"+img_name_jpg)
filenames = img_name_jpg.split("\\")[-1]
print(filenames) #这⾥是将⼀个路径中的⽂件名字提取出来
cv2.imwrite("./COCO/coco/images/train2017/{}".format(filenames),temp_img)
#这句写⼊语句,是将 X.jpg 写⼊到指定路径./COCO/coco/images/train2017/X.jpg
except Exception as e:
print(e)
print('Wrong Image:', img_name )
continue
print(img_name + '-->', place('png', 'jpg'))
#print("yxf"+img_name)
for file in val_path:
#place("json", "jpg"), "%scoco/images/val2017/" % saved_coco_path)
img_name = place('json', 'png')
temp_img = cv2.imread(img_name)
try:
#cv2.imwrite("{}coco/images/val2017/{}".format(saved_coco_path, place('png', 'jpg')), temp_img)
img_name_jpg = place('png', 'jpg') #将png⽂件替换成jpg⽂件。
print("jpg测试:" + img_name_jpg)
filenames = img_name_jpg.split("\\")[-1]
print(filenames)
cv2.imwrite("./COCO/coco/images/val2017/{}".format(filenames), temp_img)
except Exception as e:
print(e)
print('Wrong Image:', img_name)
continue
print(img_name + '-->', place('png', 'jpg'))
# 把验证集转化为COCO的json格式
l2c_val = Lableme2CoCo()
val_instance = _coco(val_path)
l2c_val.save_coco_json(val_instance, '%scoco/annotations/instances_val2017.json' % saved_coco_path)
在Anaconda Prompt运⾏:
python labelme2CoCoCo.py
运⾏后,在我当前⽬录的COCO⽂件夹下,⽣成coco⽂件夹:
coco⽂件夹下有annotations⽂件夹和images⽂件夹,
annotations⽂件夹存放2个json⽂件。
images⽂件夹存放train (存放:划分的⽤于训练的图像数据)和val (存放:划分的⽤于验证的图像数据) 两个⽂件夹。
发布评论