import cv2 import numpy as np import torch # from loguru import logger def pannel_post_process(preds, pars): # pars={'solar':0} ''' 将光伏板上覆盖物、裂缝识别出来 ''' # print(preds[0]) # logger.info('\n分类结果返回:%s'%preds) preds = torch.tensor(preds[0]) preds = preds.tolist() preds = [[*sublist[:-1], int(sublist[-1])] for sublist in preds] # 类别从浮点型转为整型 # print(preds) # 设置空的列表 # 1、判断类别中哪些有太阳能板?取出太阳能板检测结果,并取出覆盖物、裂缝检测结果。 preds_solar = [] preds_others = [] for i in range(len(preds)): if preds[i][5] in pars['objs']: # 识别为光伏板 preds_solar.append(preds[i]) else: # 识别为裂缝、覆盖物 preds_others.append(preds[i]) return point_in_rectangle(preds_others, preds_solar) def center_coordinate(boundbxs): ''' 根据检测矩形框,得到其矩形长度和宽度 输入:两个对角坐标xyxy 输出:矩形框重点坐标xy ''' boundbxs_x1 = boundbxs[0] boundbxs_y1 = boundbxs[1] boundbxs_x2 = boundbxs[2] boundbxs_y2 = boundbxs[3] center_x = 0.5 * (boundbxs_x1 + boundbxs_x2) center_y = 0.5 * (boundbxs_y1 + boundbxs_y2) return center_x, center_y def fourcorner_coordinate(boundbxs): ''' 通过矩形框对角xyxy坐标,得到矩形框轮廓 输入:两个对角坐标xyxy 输出:矩形框四个角点坐标,以contours顺序。 ''' boundbxs_x1 = boundbxs[0] boundbxs_y1 = boundbxs[1] boundbxs_x2 = boundbxs[2] boundbxs_y2 = boundbxs[3] wid = boundbxs_x2 - boundbxs_x1 hei = boundbxs_y2 - boundbxs_y1 boundbxs_x3 = boundbxs_x1 + wid boundbxs_y3 = boundbxs_y1 boundbxs_x4 = boundbxs_x1 boundbxs_y4 = boundbxs_y1 + hei contours_rec = [[boundbxs_x1, boundbxs_y1], [boundbxs_x3, boundbxs_y3], [boundbxs_x2, boundbxs_y2], [boundbxs_x4, boundbxs_y4]] return contours_rec def point_in_rectangle(preds_others, preds_solar): ''' 遍历所有光伏板异常目标,并输出 ''' if not preds_solar: return [[],''] preds = [] for i in range(len(preds_others)): for solar in preds_solar: solar_contour = fourcorner_coordinate(solar) solar_contour = np.array(solar_contour, dtype=np.float32) center_x, center_y = center_coordinate(preds_others[i]) # print(cv2.pointPolygonTest(solar_contour, (center_x, center_y), False)) if cv2.pointPolygonTest(solar_contour, (center_x, center_y), False) == 1: preds.append(preds_others[i]) # logger.info('\n分类结果返回:%s' % preds) return [preds,''] if __name__ == "__main__": # 对应DJI_20230306140129_0001_Z_165.jpg检测结果 # preds=[[6.49000e+02, 2.91000e+02, 1.07900e+03, 7.33000e+02, 9.08165e-01, 3.00000e+00], # [8.11000e+02, 2.99000e+02, 1.31200e+03, 7.65000e+02, 8.61268e-01, 3.00000e+00], # [7.05000e+02, 1.96000e+02, 7.19000e+02, 2.62000e+02, 5.66877e-01, 0.00000e+00]] # 对应DJI_20230306152702_0001_Z_562.jpg检测结果 preds = [[7.62000e+02, 7.14000e+02, 1.82800e+03, 9.51000e+02, 9.00902e-01, 3.00000e+00], [2.00000e+01, 3.45000e+02, 1.51300e+03, 6.71000e+02, 8.81440e-01, 3.00000e+00], [8.35000e+02, 8.16000e+02, 8.53000e+02, 8.30000e+02, 7.07651e-01, 0.00000e+00], [1.35600e+03, 4.56000e+02, 1.42800e+03, 4.94000e+02, 6.70549e-01, 2.00000e+00]] print('before :\n ', preds) # preds=torch.tensor(preds) #返回的预测结果 imgwidth = 1920 imgheight = 1680 pars = {'imgSize': (imgwidth, imgheight), 'wRation': 1 / 6.0, 'hRation': 1 / 6.0, 'smallId': 0, 'bigId': 3, 'newId': 4, 'recScale': 1.2} # 'smallId':0(国旗),'bigId':3(船只),wRation和hRation表示判断的阈值条件,newId--新目标的id # yyy = channel2_post_process([preds], pars) # 送入后处理函数 # # print('after :\n ', yyy)