将COCO转json:
import json
with open('/home/lixuan/fromLX/train.json') as f:
jsdict = json.load(f)
images = jsdict['images']
categories = jsdict['categories']
annotations = jsdict['annotations']
print(annotations[0])
exit()
imagedict = {}
for image in images:
id = image['id']
del image['id']
imagedict[id] = image
imagedict[id]['annotation'] = []
for annotation in annotations:
image_id = annotation['image_id']
del annotation['id']
del annotation['image_id']
imagedict[image_id]['annotation'].append(annotation)
将datatrucks转json:
import json
import cv2
import numpy as np
def getbox(cnt):
cnt = np.array(cnt)
rect = cv2.minAreaRect(cnt) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
box = cv2.boxPoints(rect) # 获取最小外接矩形的4个顶点坐标(ps: cv2.boxPoints(rect) for OpenCV 3.x)
return [max(int(np.min(box[:, 0])),0), max(int(np.min(box[:, 1])),0), int(np.max(box[:, 0])), int(np.max(box[:, 1]))]
def getarea(points):
return cv2.contourArea(np.array(points))
with open('IcecreamSeg_第一组.json') as f:
for data in f.readlines():
data = data.strip()
dic = json.loads(data)
content = dic['content']
annotation = dic['annotation']
file_name = content.split('/')[-1]
height = annotation[0]['imageHeight']
width = annotation[0]['imageWidth']
image = {}
image['height'] = height
image['width'] = width
image['file_name'] = file_name
image['annotation'] = []
for anno in annotation:
annodict = {}
annodict['iscrowd'] = 0
points = anno['points']
counts = []
# if [None, None] not in points:
for point in points:
counts.append(point[0] * width)
counts.append(point[1] * height)
point[0] = int(point[0] * width)
point[1] = int(point[1] * height)
area = getarea(points)
bbox = getbox(points)
annodict['bbox'] = bbox
annodict['segmentation'] = [counts]
if anno['label'][0] == 'interference':
annodict['category_id'] = 1
elif anno['label'][0] == 'icecream_clear':
annodict['category_id'] = 2
elif anno['label'][0] == 'icecream_notso_clear':
annodict['category_id'] = 3
else:
annodict['category_id'] = 4
annodict['area'] = area
image['annotation'].append(annodict)
with open(file_name.split('.')[0] + '.json','w') as fw:
json.dump(image,fw)
将json合成为COCO:
import json
import os
COCO_dict = {}
COCO_dict['images'] = []#{'height': 1920, 'width': 2560, 'id': 1, 'file_name': 'val_95.jpg'}
COCO_dict['categories'] = [{'supercategory': 'interference', 'id': 1, 'name': 'interference'}, {'supercategory': 'icecream_clear', 'id': 2, 'name': 'icecream_clear'}, {'supercategory': 'icecream_not_so_clear', 'id': 3, 'name': 'icecream_not_so_clear'}, {'supercategory': 'adv', 'id': 4, 'name': 'adv'}]
COCO_dict['annotations'] = []
"""
{'iscrowd': 0, 'image_id': 1, 'bbox': [1936.0, 1497.0, 218.0, 268.0], 'segmentation': [[]], 'category_id': 1, 'id': 1, 'area': 4915200, 'extreme_points': [2129.0, 1497.0, 1936.0, 1540.0, 2061.0, 1765.0, 2154.0, 1527.0, 2045.0, 1631.0]}
"""
image_id = 1
id = 1
for file in os.listdir('/home/lixuan/fromLX/icecreamtxt'):
with open('/home/lixuan/fromLX/icecreamtxt/{}'.format(file)) as f:
dic = json.load(f)
image = {}
image['height'] = dic['height']
image['width'] = dic['width']
image['id'] = image_id
image['file_name'] = dic['file_name']
COCO_dict['images'].append(image)
for anno in dic['annotation']:
anno['image_id'] = image_id
anno['id'] = id
COCO_dict['annotations'].append(anno)
id += 1
image_id += 1
with open('train.json','w') as f:
json.dump(COCO_dict,f)