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/dota_poly2rbox.py
Views: 475
import os1import math2import argparse3import os.path as osp45import numpy as np67def cal_line_length(point1, point2):8return math.sqrt(math.pow(point1[0] - point2[0], 2) + math.pow(point1[1] - point2[1], 2))910def get_best_begin_point_single(coordinate):11x1, y1, x2, y2, x3, y3, x4, y4 = coordinate12xmin = min(x1, x2, x3, x4)13ymin = min(y1, y2, y3, y4)14xmax = max(x1, x2, x3, x4)15ymax = max(y1, y2, y3, y4)16combinate = [[[x1, y1], [x2, y2], [x3, y3], [x4, y4]], [[x2, y2], [x3, y3], [x4, y4], [x1, y1]],17[[x3, y3], [x4, y4], [x1, y1], [x2, y2]], [[x4, y4], [x1, y1], [x2, y2], [x3, y3]]]18dst_coordinate = [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]19force = 100000000.020force_flag = 021for i in range(4):22temp_force = cal_line_length(combinate[i][0], dst_coordinate[0]) \23+ cal_line_length(combinate[i][1], dst_coordinate[1]) \24+ cal_line_length(combinate[i][2], dst_coordinate[2]) \25+ cal_line_length(combinate[i][3], dst_coordinate[3])26if temp_force < force:27force = temp_force28force_flag = i29if force_flag != 0:30pass31# print("choose one direction!")32return np.array(combinate[force_flag]).reshape(8)3334def poly2rbox_single(poly):35"""36poly:[x0,y0,x1,y1,x2,y2,x3,y3]37to38rrect:[x_ctr,y_ctr,w,h,angle]39"""40poly = np.array(poly[:8], dtype=np.float32)4142pt1 = (poly[0], poly[1])43pt2 = (poly[2], poly[3])44pt3 = (poly[4], poly[5])45pt4 = (poly[6], poly[7])4647edge1 = np.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) +48(pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))49edge2 = np.sqrt((pt2[0] - pt3[0]) * (pt2[0] - pt3[0]) +50(pt2[1] - pt3[1]) * (pt2[1] - pt3[1]))5152angle = 053width = 054height = 05556if edge1 > edge2:57width = edge158height = edge259angle = np.arctan2(60np.float(pt2[1] - pt1[1]), np.float(pt2[0] - pt1[0]))61elif edge2 >= edge1:62width = edge263height = edge164angle = np.arctan2(65np.float(pt4[1] - pt1[1]), np.float(pt4[0] - pt1[0]))6667if angle > np.pi*3/4:68angle -= np.pi69if angle < -np.pi/4:70angle += np.pi7172x_ctr = np.float(pt1[0] + pt3[0]) / 273y_ctr = np.float(pt1[1] + pt3[1]) / 274rbox = np.array([x_ctr, y_ctr, width, height, angle])7576return rbox7778def norm_angle(angle, range=[-np.pi / 4, np.pi]):79return (angle - range[0]) % range[1] + range[0]808182def poly2rbox_single_v2(poly):83"""84poly:[x0,y0,x1,y1,x2,y2,x3,y3]85to86rrect:[x_ctr,y_ctr,w,h,angle]87"""88poly = np.array(poly[:8], dtype=np.float32)8990pt1 = (poly[0], poly[1])91pt2 = (poly[2], poly[3])92pt3 = (poly[4], poly[5])93pt4 = (poly[6], poly[7])9495edge1 = np.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) +96(pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))97edge2 = np.sqrt((pt2[0] - pt3[0]) * (pt2[0] - pt3[0]) +98(pt2[1] - pt3[1]) * (pt2[1] - pt3[1]))99100angle = 0101width = 0102height = 0103104if edge1 > edge2:105width = edge1106height = edge2107angle = np.arctan2(108np.float(pt2[1] - pt1[1]), np.float(pt2[0] - pt1[0]))109elif edge2 >= edge1:110width = edge2111height = edge1112angle = np.arctan2(113np.float(pt4[1] - pt1[1]), np.float(pt4[0] - pt1[0]))114115# if angle > np.pi*3/4:116# angle -= np.pi117# if angle < -np.pi/4:118# angle += np.pi119angle = norm_angle(angle)120121x_ctr = np.float(pt1[0] + pt3[0]) / 2122y_ctr = np.float(pt1[1] + pt3[1]) / 2123124return float(x_ctr), float(y_ctr), float(width), float(height), float(angle)125126127def poly2rbox_single_v3(poly):128"""129poly:[x0,y0,x1,y1,x2,y2,x3,y3]130to131rrect:[x_ctr,y_ctr,w,h,angle]132"""133poly = np.array(poly[:8], dtype=np.float32)134135pt1 = (poly[0], poly[1])136pt2 = (poly[2], poly[3])137pt3 = (poly[4], poly[5])138pt4 = (poly[6], poly[7])139140edge1 = np.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) +141(pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))142edge2 = np.sqrt((pt2[0] - pt3[0]) * (pt2[0] - pt3[0]) +143(pt2[1] - pt3[1]) * (pt2[1] - pt3[1]))144145max_edge = max(edge1, edge2)146min_edge = min(edge1, edge2)147ratio = max_edge / min_edge148# print(ratio)149if ratio < 1.15:150151width = max_edge152height = min_edge153angle1 = np.arctan2(np.float(pt2[1] - pt1[1]), np.float(pt2[0] - pt1[0]))154# elif edge2 >= edge1:155angle2 = np.arctan2(np.float(pt4[1] - pt1[1]), np.float(pt4[0] - pt1[0]))156157angle1_norm = norm_angle(angle1)158angle2_norm = norm_angle(angle2)159# if abs(angle1_norm) > abs(angle2_norm):160# final_angle = angle2_norm161# else:162# final_angle = angle1_norm163if abs(angle1_norm) > abs(angle2_norm):164final_angle = angle2_norm165else:166final_angle = angle1_norm167168else:169final_angle = 0170width = 0171height = 0172173if edge1 > edge2:174width = edge1175height = edge2176final_angle = np.arctan2(177np.float(pt2[1] - pt1[1]), np.float(pt2[0] - pt1[0]))178elif edge2 >= edge1:179width = edge2180height = edge1181final_angle = np.arctan2(182np.float(pt4[1] - pt1[1]), np.float(pt4[0] - pt1[0]))183184final_angle = norm_angle(final_angle)185186x_ctr = np.float(pt1[0] + pt3[0]) / 2187y_ctr = np.float(pt1[1] + pt3[1]) / 2188189return float(x_ctr), float(y_ctr), float(width), float(height), float(final_angle)190191192def rbox2poly_single(rrect):193"""194rrect:[x_ctr,y_ctr,w,h,angle]195to196poly:[x0,y0,x1,y1,x2,y2,x3,y3]197"""198x_ctr, y_ctr, width, height, angle = rrect[:5]199tl_x, tl_y, br_x, br_y = -width/2, -height/2, width/2, height/2200rect = np.array([[tl_x, br_x, br_x, tl_x], [tl_y, tl_y, br_y, br_y]])201R = np.array([[np.cos(angle), -np.sin(angle)],202[np.sin(angle), np.cos(angle)]])203poly = R.dot(rect)204x0, x1, x2, x3 = poly[0, :4] + x_ctr205y0, y1, y2, y3 = poly[1, :4] + y_ctr206poly = np.array([x0, y0, x1, y1, x2, y2, x3, y3], dtype=np.float32)207poly = get_best_begin_point_single(poly)208return poly209210def convert2rbox(src_path):211image_path = osp.join(src_path, 'images/')212src_label_path = osp.join(src_path, 'labelTxt/')213dst_label_path = osp.join(src_path, 'labelTxtRbox/')214if not osp.exists(dst_label_path):215os.mkdir(dst_label_path)216217image_list = os.listdir(image_path)218image_list.sort()219220for image in image_list:221img_name = osp.basename(image)222print(img_name)223ann_name = img_name.split('.')[0]+'.txt'224lab_path = osp.join(src_label_path, ann_name)225dst_path = osp.join(dst_label_path, ann_name)226out_str = ''227228# import time229# half the time used by poly2rbox230with open(lab_path, 'r') as f:231for ann_line in f.readlines():232ann_line = ann_line.strip().split(' ')233bbox = [np.float32(ann_line[i]) for i in range(8)]234# 8 point to 5 point xywha235x_ctr, y_ctr, width, height, angle = poly2rbox_single(bbox)236class_name = ann_line[8]237difficult = int(ann_line[9])238239out_str += "{} {} {} {} {} {} {}\n".format(str(x_ctr), str(240y_ctr), str(width), str(height), str(angle), class_name, difficult)241with open(dst_path, 'w') as fdst:242fdst.write(out_str)243244245if __name__ == '__main__':246parser = argparse.ArgumentParser()247parser.add_argument('-p', '--path', type=str, required=True)248args = parser.parse_args()249convert2rbox(args.path)250251252