分割数据集格式转DOTA_obb格式
分割数据集格式:
1.json
2.json
…
转DOTA格式:
1.txt
2.txt
…
import cv2
import numpy as np
import json
import os
rootdir = '/home/ai-i-zhangbo/obb_dataset/val2017/images'
def image_id(root_dir):
a = []
for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
filename=filename.strip('.png')
print(filename)
a.append(filename)
return a
def convert_annotation(image_id):
load_f=open("/home/ai-i-zhangbo/obb_dataset/val2017/annotation/%s.json"%(image_id),'r')
load_dict=json.load(load_f)
out_file=open('/home/ai-i-zhangbo/seg2obb/demotxt/%s.txt'%(image_id), 'w')
bbox = []
dict = {'1':'solar', '2':'cabin', '3':'sensor', '4':'antenna'}
trans = load_dict['shapes']
num = len(trans)
for i in range(num):
a = trans[i]
cnt = a['points']
classname = dict[a['label']]
label_num = int(a['label'])
cnt = np.float32(cnt)
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
out_file.write(str(box)+'\n'+classname+" "+str(label_num)+'\n')
bbox.append(box)
###读取图片并画图demo#############
bgr_img = cv2.imread("/home/ai-i-zhangbo/obb_dataset/val2017/images/%s.png"%(image_id))
for j in range(num):
bgr_img = cv2.drawContours(bgr_img, [bbox[j]], 0, (0, 0, 255), 2)
cv2.imwrite('/home/ai-i-zhangbo/seg2obb/demopic/%s.png'%(image_id), bgr_img)
names = image_id(rootdir)
for image_id in names:
convert_annotation(image_id)