自己的转labelme
# -*- coding:utf-8 -*-
import argparse
import json
import os
import os.path as osp
import warnings
import copy
import numpy as np
import PIL.Image
from skimage import io
import yaml
import cv2
import copy
def mkdir_os(path):
if not os.path.exists(path):
os.makedirs(path)
#https://blog.csdn.net/u010103202/article/details/81635436
#labelme标注的数据分析
#https://blog.csdn.net/wc781708249/article/details/79595174
#数据先转换成lableme,然后调用labelme的函数转换为VOC
#https://github.com/wkentaro/labelme/tree/master/examples/semantic_segmentation
ori_path = "./luosi_phone_seg_20191008"
save_path = "./seg_groundtruth"
json_path = "./labelme_json"
gray_path = "./JPEGImages_gray"
mkdir_os(save_path)
mkdir_os(json_path)
mkdir_os(gray_path)
#path = "luosi_phone_seg_20191008_20191012_185541.json"
path = "luosi_phone_seg_20191008_20191014_194416.json"
data = []
with open(path) as f:
for line in f:
data.append(json.loads(line))
num = 0
lendata_num = 0
count = len(data)
file_error = open('error_null_json_biaozhu.txt',"wr")
train_txt = open('train.txt',"wr")
test_txt = open('test.txt',"wr")
for lab in range(count):
num += 1
#print num,"/",count
onedate = data[lab]
name = onedate["url_image"]
name = str(name).split("/")[-1]
print(name)
# if len(onedate["result"])<2:
# print name
# continue
# X1 = onedate["result"][0]["data"]
# Y1 = onedate["result"][1]["data"]
# assert len(X1)==len(Y1),"error len"
img = cv2.imread(os.path.join(ori_path,name))
tempimg = copy.deepcopy(img)
grayimg = copy.deepcopy(img)
hh,ww,c = img.shape
point_size = 1
point_color = (0, 0, 255)
thickness = 4
# VID_20190924_143239_000511_img_good_931_735_1111_895
if(len(onedate["result"])==0):
print(name)
file_error.write(name)
file_error.write("\n")
continue
if name == "VID_20190924_145531_000961_img_bad_791_480_1006_675.jpg":
a = 1
if 'data' in onedate["result"] or 'data' in onedate["result"][0]:
for key in range(len(onedate["result"])):
ndata = onedate["result"][key]["data"]
for k in range(len(ndata)/2):
cv2.circle(img, (ndata[2*k],ndata[2*k+1]), point_size, point_color, thickness)
grayimg=cv2.cvtColor(grayimg,cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join(gray_path,name),grayimg)
cv2.imwrite(os.path.join(save_path,name),img)
else:
print(name)
file_error.write(name)
file_error.write("\n")
continue
json_jpg={}
json_jpg["imagePath"] = str(os.path.join(ori_path,name))
json_jpg["imageData"] = None
shapes=[]
#if name=="VID_20190924_142716_000261_img_good_670_716_772_813.jpg":
# sss=1
#VID_20190924_145903
#VID_20190924_145950
for key in range(len(onedate["result"])):
ndata = onedate["result"][key]["data"]
points=[]
if len(ndata)< 6:
lendata_num += 1
continue
else:
for k in range(len(ndata)/2):
cv2.circle(img, (ndata[2*k],ndata[2*k+1]), point_size, point_color, thickness)
points.append([ndata[2*k],ndata[2*k+1]])
one_shape = {}
one_shape["line_color"] = None
one_shape["shape_type"] = "polygon"
one_shape["points"] = points
one_shape["flags"] = {}
one_shape["fill_color"] = None
one_shape["label"] = "luosi"
shapes.append(one_shape)
json_jpg["shapes"] = shapes
json_jpg["version"] = "3.16.7"
json_jpg["flags"] = {}
json_jpg["fillColor"] = [
255,
0,
0,
128
]
json_jpg["lineColor"] = [
0,
255,
0,
128
]
json_jpg["imageWidth"] = ww
json_jpg["imageHeight"] = hh
jsonData = json.dumps(json_jpg, indent=4)
jsonname = name.split(".")[0]
jsonname = jsonname+".json"
fileObject = open(os.path.join(json_path,jsonname), 'w')
#cv2.imwrite(os.path.join(json_path,name),tempimg)
fileObject.write(jsonData)
fileObject.close()
txtname = name.split(".")[0]
if "VID_20190924_145903" in txtname or "VID_20190924_145950" in txtname:
test_txt.write(txtname)
test_txt.write("\n")
else:
train_txt.write(txtname)
train_txt.write("\n")
print("lendata_num:",lendata_num)
file_error.close()
test_txt.close()
train_txt.close()
labelme转voc
#!/usr/bin/env python
from __future__ import print_function
import argparse
import glob
import json
import os
import os.path as osp
import sys
import numpy as np
import PIL.Image
import labelme
def mkdir_os(path):
if not os.path.exists(path):
os.makedirs(path)
def main():
# parser = argparse.ArgumentParser(
# formatter_class=argparse.ArgumentDefaultsHelpFormatter
# )
# parser.add_argument('input_dir', help='input annotated directory', default='./labelme_json')
# parser.add_argument('output_dir', help='output dataset directory', default='./voc_json')
# parser.add_argument('--labels', help='labels file', default='labels.txt', required=True)
# args = parser.parse_args()
# if osp.exists(args.output_dir):
# print('Output directory already exists:', args.output_dir)
# sys.exit(1)
input_dir = './labelme_json'
output_dir = './voc_json'
mkdir_os(output_dir)
mkdir_os(osp.join(output_dir, 'JPEGImages'))
mkdir_os(osp.join(output_dir, 'SegmentationClass'))
mkdir_os(osp.join(output_dir, 'SegmentationClassPNG'))
mkdir_os(osp.join(output_dir, 'SegmentationClassVisualization'))
print('Creating dataset:', output_dir)
class_names = []
class_name_to_id = {}
for i, line in enumerate(open('labels.txt').readlines()):
class_id = i - 1 # starts with -1
class_name = line.strip()
class_name_to_id[class_name] = class_id
if class_id == -1:
assert class_name == '__ignore__'
continue
elif class_id == 0:
assert class_name == '_background_'
class_names.append(class_name)
class_names = tuple(class_names)
print('class_names:', class_names)
out_class_names_file = osp.join(output_dir, 'class_names.txt')
with open(out_class_names_file, 'w') as f:
f.writelines('\n'.join(class_names))
print('Saved class_names:', out_class_names_file)
colormap = labelme.utils.label_colormap(255)
for label_file in glob.glob(osp.join(input_dir, '*.json')):
print('Generating dataset from:', label_file)
with open(label_file) as f:
base = osp.splitext(osp.basename(label_file))[0]
out_img_file = osp.join(
output_dir, 'JPEGImages', base + '.jpg')
out_lbl_file = osp.join(
output_dir, 'SegmentationClassPNG', base + '.npy')
#SegmentationClassPNG
#SegmentationClass
# out_lbl_file = osp.join(
# output_dir, 'SegmentationClass', base + '.png')
out_png_file = osp.join(
output_dir, 'SegmentationClass', base + '.png')
out_viz_file = osp.join(
output_dir,
'SegmentationClassVisualization',
base + '.jpg',
)
data = json.load(f)
#保存出错:VID_20190924_142150_000251_img_good_891_387_976_475
#img_file = osp.join(osp.dirname(label_file), data['imagePath'])
img = np.asarray(PIL.Image.open(data['imagePath']))
PIL.Image.fromarray(img).save(out_img_file)
lbl = labelme.utils.shapes_to_label(
img_shape=img.shape,
shapes=data['shapes'],
label_name_to_value=class_name_to_id,
)
labelme.utils.lblsave(out_png_file, lbl)
np.save(out_lbl_file, lbl)
viz = labelme.utils.draw_label(
lbl, img, class_names, colormap=colormap)
PIL.Image.fromarray(viz).save(out_viz_file)
if __name__ == '__main__':
main()
labelme转coco
# -*- coding: utf-8 -*-
import os
import json
import numpy as np
import glob
import shutil
from sklearn.model_selection import train_test_split
np.random.seed(41)
import cv2
#0为背景
classname_to_id = {"luosi": 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):
import io
#json.dump(instance, io.open(save_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=1) # indent=2 更加美观显示
with io.open(save_path, 'w', encoding="utf-8") as outfile:
my_json_str = json.dumps(instance, ensure_ascii=False, indent=1)
if isinstance(my_json_str, str):
my_json_str = my_json_str.decode("utf-8")
outfile.write(my_json_str)
# 由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 = {}
from labelme import utils
#img_x = utils.img_b64_to_arr(obj['imageData'])
img_x = cv2.imread(str(obj['imagePath']))
img_x = cv2.cvtColor(img_x, cv2.COLOR_BGR2RGB)
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'] = 1.0
return annotation
# 读取json文件,返回一个json对象
def read_jsonfile(self, path):
import io
#with io.open(path, "r", encoding='utf-8') as f:
with open(path, "r") 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 = "labelme_json/"
saved_coco_path = "./"
# 创建文件
if not os.path.exists("%scoco/annotations/"%saved_coco_path):
os.makedirs("%scoco/annotations/"%saved_coco_path)
if not os.path.exists("%scoco/images/train2017/"%saved_coco_path):
os.makedirs("%scoco/images/train2017"%saved_coco_path)
if not os.path.exists("%scoco/images/val2017/"%saved_coco_path):
os.makedirs("%scoco/images/val2017"%saved_coco_path)
# 获取images目录下所有的joson文件列表
json_list_path = glob.glob(labelme_path + "/*.json")
# 数据划分,这里没有区分val2017和tran2017目录,所有图片都放在images目录下
train_path, val_path = train_test_split(json_list_path, test_size=0)
val_path = train_path
train_path = []
print("train_n:", len(train_path), 'val_n:', len(val_path))
# 把训练集转化为COCO的json格式
if len(train_path):
l2c_train = Lableme2CoCo()
train_instance = l2c_train.to_coco(train_path)
l2c_train.save_coco_json(train_instance, '%scoco/annotations/instances_train2017.json'%saved_coco_path)
for file in train_path:
name = file.split('/')[-1]
name = "./images_shachepian/train2017/" + name
shutil.copy(name.replace("json","jpg"),"%scoco/images/train2017/"%saved_coco_path)
if len(val_path):
# 把验证集转化为COCO的json格式
l2c_val = Lableme2CoCo()
val_instance = l2c_val.to_coco(val_path)
l2c_val.save_coco_json(val_instance, '%scoco/annotations/instances_val2017.json'%saved_coco_path)
for file in val_path:
name = file.split('/')[-1]
name = "./images_shachepian/val2017/" + name
shutil.copy(name.replace("json","jpg"),"%scoco/images/val2017/"%saved_coco_path)