Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/DOTA_devkit/DOTA2JSON.py
Views: 475
import json1import os2import os.path as osp3import random45from PIL import Image67from dota_poly2rbox import poly2rbox_single_v2, poly2rbox_single8910def parse_ann_info(img_base_path, label_base_path, img_name):11lab_path = osp.join(label_base_path, img_name+'.txt')12bboxes, labels, bboxes_ignore, labels_ignore = [], [], [], []13with open(lab_path, 'r') as f:14for ann_line in f.readlines():15ann_line = ann_line.strip().split(' ')16bbox = [float(ann_line[i]) for i in range(8)]17# 8 point to 5 point xywha18bbox = poly2rbox_single_v2(bbox)19class_name = ann_line[8]20difficult = int(ann_line[9])21# ignore difficult =222if difficult == 0:23bboxes.append(bbox)24labels.append(class_name)25elif difficult == 1:26bboxes_ignore.append(bbox)27labels_ignore.append(class_name)28return bboxes, labels, bboxes_ignore, labels_ignore293031def generate_txt_labels(src_path, out_path, trainval=True):32"""Generate .txt labels recording img_names33Args:34src_path: dataset path containing images and labelTxt folders.35out_path: output txt file path36trainval: trainval or test?37"""38img_path = os.path.join(src_path, 'images')39label_path = os.path.join(src_path, 'labelTxt')40img_lists = os.listdir(img_path)41with open(out_path, 'w') as f:42for img in img_lists:43img_name = osp.splitext(img)[0]44label = os.path.join(label_path, img_name+'.txt')45if(trainval == True):46if(os.path.exists(label) == False):47print('Label:'+img_name+'.txt'+' Not Exist')48else:49f.write(img_name+'\n')50else:51f.write(img_name+'\n')525354def generate_json_labels(src_path, out_path, trainval=True):55"""Generate .json labels which is similar to coco format56Args:57src_path: dataset path containing images and labelTxt folders.58out_path: output json file path59trainval: trainval or test?60"""61img_path = os.path.join(src_path, 'images')62label_path = os.path.join(src_path, 'labelTxt')63img_lists = os.listdir(img_path)6465data_dict = []6667with open(out_path, 'w') as f:68for id, img in enumerate(img_lists):69img_info = {}70img_name = osp.splitext(img)[0]71label = os.path.join(label_path, img_name+'.txt')72img = Image.open(osp.join(img_path, img))73img_info['filename'] = img_name+'.png'74img_info['height'] = img.height75img_info['width'] = img.width76img_info['id'] = id77if(trainval == True):78if(os.path.exists(label) == False):79print('Label:'+img_name+'.txt'+' Not Exist')80else:81bboxes, labels, bboxes_ignore, labels_ignore = parse_ann_info(82img_path, label_path, img_name)83ann = {}84ann['bboxes'] = bboxes85ann['labels'] = labels86ann['bboxes_ignore'] = bboxes_ignore87ann['labels_ignore'] = labels_ignore88img_info['annotations'] = ann89data_dict.append(img_info)90json.dump(data_dict, f)919293if __name__ == '__main__':94generate_json_labels('/data1/dataset_demo/DOTA_demo/',95'/data1/OrientedRepPoints/data/dota/trainval_split/trainval.json')96generate_json_labels('/data1/dataset_demo/DOTA_demo/',97'/data1/OrientedRepPoints/data/dota/test_split/test.json', trainval=False)98print('done!')99100101