def eval_rotatexml(GT_xml_dir, det_xml_dir, NAME_LABEL_MAP, file_ext='.xml', ovthresh=0.5): # GT_xml_path 是需要转换为txt的xml文件 # txt_dir_h是将要写入的xml转换为txt的文件路径 # annopath 是GT的txt文件 # 读取原图路径 LABEl_NAME_MAP = get_label_name_map(NAME_LABEL_MAP) file_paths = get_file_paths_recursive(GT_xml_dir, '.xml') angle_det_all=[] for count, xml_path in enumerate(file_paths): img_size,gsd,imagesource,gtbox_label,extra=read_rotate_xml(xml_path,NAME_LABEL_MAP) det_xml=xml_path.replace(GT_xml_dir,det_xml_dir) try: img_size,gsd,imagesource,detbox_label,extra=read_rotate_xml(det_xml,NAME_LABEL_MAP) except : continue cvrboxes=[] for box in gtbox_label: cvrboxes.append(rotate_rect2cv_np(box)) for box in detbox_label: cvrboxes.append(rotate_rect2cv_np(box)) cvrboxes = np.array(cvrboxes) score=np.ones((len(gtbox_label))) score_det=np.array(extra) score=np.hstack((score,score_det)) # gtbox_label=np.array(gtbox_label+detbox_label) if len(cvrboxes)>0:##斜框NMS keep ,angle_det= nms_rotate_cpu(cvrboxes, score, ovthresh, 200) #这里可以改 angle_det=np.array(angle_det) inx=angle_det>0 angle_det=angle_det[inx] angle_det=angle_det.tolist() # assert(len(angle_det)==len(gtbox_label)) print(len(angle_det)) angle_det_all+=angle_det print(len(angle_det_all)) angle_det_all=np.array(angle_det_all) mean_angle=np.mean(angle_det_all) print('Mean of angle is {}'.format(mean_angle))
GT_xml_path = '/media/zf/U/2021ZKXT_aug/annotations/valrotatexml' txt_dir_h = '/media/zf/U/2021ZKXT_aug/annotations/GT' imagesetfile = osp.join(osp.dirname(GT_xml_path), 'gt_list.txt') generate_file_list(GT_xml_path, imagesetfile, file_ext='.xml') # LABEl_NAME_MAP = get_label_name_map(NAME_LABEL_MAP) LABEl_NAME_MAP_eval = get_label_name_map(NAME_LABEL_MAP_eval) file_paths = get_file_paths_recursive(GT_xml_path, '.xml') if os.path.isdir(txt_dir_h): shutil.rmtree(txt_dir_h, True) os.makedirs(txt_dir_h) ### GT for count, xml_path in enumerate(file_paths): img_size, gsd, imagesource, gtbox_label, extra = read_rotate_xml( xml_path, NAME_LABEL_MAP) # eval txt CLASS_DOTA = NAME_LABEL_MAP.keys() # Task1 # write_handle_h = open( os.path.join( txt_dir_h, '{}.txt'.format(os.path.splitext(os.path.split(xml_path)[1])[0])), 'w') #Task1_gt_ # gtbox_label=np.array(gtbox_label) ploys = rotated_box_to_poly_np(gtbox_label) for i, rect_box in enumerate(ploys): # rbox[4]=0 # rbox_cv=rotate_rect2cv(rbox)
def merge_rotatexml(xmldir, merge_xmldir, NAME_LABEL_MAP, NAME_LONG_MAP, std_scale, score_threshold): LABEL_NAME_MAP = get_label_name_map(NAME_LABEL_MAP) if not os.path.exists(merge_xmldir): os.mkdir(merge_xmldir) imgs_path = get_file_paths_recursive(xmldir, '.xml') name_det = {} for num, img_path in enumerate(imgs_path, 0): str_num = img_path.replace('.xml', '').strip().split('__') img_name = str_num[0] scale = np.float(str_num[1]) ww_ = np.int(str_num[2]) hh_ = np.int(str_num[3]) img_size, gsd, imagesource, gtbox, extra = read_rotate_xml( img_path, NAME_LABEL_MAP) gtbox = np.array(gtbox) if len(gtbox) > 0: gtbox[:, 6] = extra inx = gtbox[:, 6] > score_threshold rotateboxes = gtbox[inx] gtbox[:, 0] = (gtbox[:, 0] + ww_) / scale gtbox[:, 1] = (gtbox[:, 1] + hh_) / scale gtbox[:, 2] /= scale gtbox[:, 3] /= scale if not img_name in name_det: name_det[img_name] = gtbox else: name_det[img_name] = np.vstack((name_det[img_name], gtbox)) # else: # print(' ') # # #将所有检测结果综合起来 #正框mns for img_name, rotateboxes in name_det.items(): if std_scale == 0: print('no nms') else: inx, scores = py_cpu_label_nms( dets=np.array(rotateboxes, np.float32), thresh=nms_threshold, max_output_size=500, LABEL_NAME_MAP=LABEL_NAME_MAP_USnavy_20, NAME_LONG_MAP=NAME_LONG_MAP_USnavy, std_scale=std_scale) rotateboxes = rotateboxes[inx] # result[:, 4]=scores[inx] # inx=result[:, 4]>score_threshold_2 # result=result[inx] cvrboxes = [] for box in rotateboxes: cvrboxes.append(rotate_rect2cv_np(box)) cvrboxes = np.array(cvrboxes) if len(cvrboxes) > 0: ##斜框NMS keep = nms_rotate_cpu(cvrboxes, rotateboxes[:, 6], rnms_threshold, 4000) #这里可以改 rotateboxes = rotateboxes[keep] # keep=[]#去掉过 尺寸异常的目标 # for i in range(rotateboxes.shape[0]): # box=rotateboxes[i,:] # actual_long=box[3]*scale # standad_long = NAME_LONG_MAP[LABEL_NAME_MAP[box[5]]] # STD_long = NAME_LONG_MAP[LABEL_NAME_MAP[box[5]]] # STD_long *= std_scale # if np.abs(actual_long-standad_long)/standad_long < STD_long *1.2 and box[2]>16: # keep.append(i) # else: # print('{} hh:{} ww:{}'.format(img_name,hh_,ww_)) # print('{} th label {} is wrong,long is {} normal long is {} width is {}'.format(i+1,LABEl_NAME_MAP[box[5]],actual_long,standad_long,box[3])) # rotateboxes=rotateboxes[keep] #保存检测结果 为比赛格式 # image_dir,image_name=os.path.split(img_path) # gt_box=rotateboxes[:,0:5] # label=rotateboxes[:,6] write_rotate_xml(merge_xmldir, '{}.jpg'.format(img_name), [1024, 1024, 3], 0.5, 'USnavy', rotateboxes, LABEL_NAME_MAP, rotateboxes[:, 6]) #size,gsd,imagesource
def eval_rotatexml(GT_xml_dir, det_xml_dir, NAME_LABEL_MAP, file_ext='.xml', ovthresh=0.5): # GT_xml_path 是需要转换为txt的xml文件 # txt_dir_h是将要写入的xml转换为txt的文件路径 # annopath 是GT的txt文件 # 读取原图路径 LABEl_NAME_MAP = get_label_name_map(NAME_LABEL_MAP) file_paths = get_file_paths_recursive(GT_xml_dir, '.xml') num_det_all=[] num_tp_all=[] num_gt_all=[] for count, xml_path in enumerate(file_paths): img_size,gsd,imagesource,gtbox_label,extra=read_rotate_xml(xml_path,NAME_LABEL_MAP) # num_gt = np.ones((len(gtbox_label))) num_det = np.zeros((len(gtbox_label))) num_tp = np.zeros((len(gtbox_label))) det_xml=xml_path.replace(GT_xml_dir,det_xml_dir) try: img_size,gsd,imagesource,detbox_label,extra=read_rotate_xml(det_xml,NAME_LABEL_MAP) except : continue if len(detbox_label)>0: for i,box in enumerate( gtbox_label): gt_cvrbox=rotate_rect2cv_np(box) gt_center=np.array([box[0],box[1]]) gt_centers=np.repeat(gt_center[None], len(detbox_label), axis=0) det_centers=np.array(detbox_label)[:,0:2] diff=det_centers-gt_centers dist=np.sqrt(np.square(diff[:,0])+np.square(diff[:,1]),) index=np.argmin(dist) det_gt_box=detbox_label[index] det_cvrbox=rotate_rect2cv_np(det_gt_box) r1 = ((gt_cvrbox[0], gt_cvrbox[1]), (gt_cvrbox[2], gt_cvrbox[3]), gt_cvrbox[4]) area_r1 = gt_cvrbox[2] * gt_cvrbox[3]#计算当前检测框的面积 r2 = ((det_cvrbox[0], det_cvrbox[1]), (det_cvrbox[ 2], det_cvrbox[3]), det_cvrbox[4]) area_r2 = det_cvrbox[2] * det_cvrbox[3] inter = 0.0 int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]#求两个旋转矩形的交集,并返回相交的点集合 if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True)#求点集的凸边形 int_area = cv2.contourArea(order_pts)#计算当前点集合组成的凸边形的面积 inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 0.0000001) if inter >= ovthresh:#对大于设定阈值的检测框进行滤除 num_det[i]=1 # angle_diff=np.min([np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]), # np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]+np.pi*2), # np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]-np.pi*2), # np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]+np.pi), # np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]-np.pi)]) angle_diff=np.min([np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]), np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]+np.pi*2), np.abs(gtbox_label[i][4]-detbox_label[int(index)][4]-np.pi*2)]) if angle_diff<np.pi/18: num_tp[i]=1 else: print('xmlpath {}'.format(xml_path)) print('gt index {}'.format(i)) print('det index {}'.format(index)) num_det=num_det.tolist() num_tp=num_tp.tolist() # num_gt=num_gt.tolist() # assert(len(angle_det)==len(gtbox_label)) print(len(num_det)) num_det_all+=num_det num_tp_all+=num_tp print(len(num_det_all)) num_det_all=np.array(num_det_all) mean_det=np.mean(num_det_all) num_tp_all=np.array(num_tp_all) mean_tp=np.mean(num_tp_all) print('Mean of det is {}'.format(mean_det)) print('Mean of gt is {}'.format(mean_tp)) print('bow acc rate {}'.format(mean_tp/mean_det))