|
- # 设定开关,将最小外接矩形中心点间的距离作为vehicle之间的距离
- import numpy as np
- import math, cv2, time
-
-
- def get_ms(time2, time1):
- return (time2 - time1) * 1000.0
-
-
- def two_points_distance(x1, y1, x2, y2):
- distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
- return distance
-
-
- # 保存正常vehicle和非正常vehicle的信息(当contours顶点数小于6时,无法拟合最小外接矩形,定义为非正常vehicle)
- def saveVehicle1(traffic_dict, contours, normVehicleBD, normVehicle, count, i, unnormVehicle, normVehicleCOOR):
- if len(contours) >= 6:
- normVehicleBD.append(contours)
- normVehicle.append(traffic_dict['det'][count])
- rect = cv2.minAreaRect(contours)
- normVehicleCOOR.append(rect[0])
- else:
- traffic_dict['det'][int(i / 2)] = traffic_dict['det'][int(i / 2)] + [0, 0.3, 999, -1, 3]
- unnormVehicle.append(traffic_dict['det'][int(i / 2)])
- return normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR
-
-
- # saveVehicle2和saveVehicle1有区别
- def saveVehicle2(traffic_dict, contours, normVehicleBD, normVehicle, count, i, unnormVehicle, normVehicleCOOR, centerCOOR):
- if len(contours) >= 6:
- normVehicleBD.append(contours)
- normVehicle.append(traffic_dict['det'][count])
- normVehicleCOOR.append(centerCOOR)
- else:
- traffic_dict['det'][int(i / 2)] = traffic_dict['det'][int(i / 2)] + [0, 0.3, 999, -1, 3]
- unnormVehicle.append(traffic_dict['det'][int(i / 2)])
- return normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR
-
-
- # 对于不在道路上的vehicle,将输出信息补全
- def supplementInformation(traffic_dict, i, roundness, y_min, y_max, imgVehicle, rect):
- score = -1
- traffic_dict['det'][i] = traffic_dict['det'][i] + [0, roundness, 999, [-1, -1, -1], 666]
- if y_min > 0 and y_max < imgVehicle.shape[0] and roundness > traffic_dict['roundness']: # 过滤掉上下方被speedRoad的边界截断的vehicle
- score = (min(rect[1]) - max(rect[1]) * traffic_dict['roundness']) / (max(rect[1]) * (1 - traffic_dict['roundness']))
- return score
-
-
- # 判断交通事故类型
- def judgeAccidentType(traffic_dict, b):
- if max(traffic_dict['det'][b][9]) == traffic_dict['det'][b][9][0] and traffic_dict['det'][b][9][0] != -1:
- return 0
- elif max(traffic_dict['det'][b][9]) == traffic_dict['det'][b][9][1] and traffic_dict['det'][b][9][1] != -1:
- return 1
- elif max(traffic_dict['det'][b][9]) == traffic_dict['det'][b][9][2] and traffic_dict['det'][b][9][2] != -1:
- return 2
- else:
- return 3
-
-
- # 计算距离得分
- def distanceScore(vehicleWH, index1, index2, smallestDistance, traffic_dict):
- d1 = (min(vehicleWH[index1]) + min(vehicleWH[index2])) / 2
- d2 = min(min(vehicleWH[index1]), min(vehicleWH[index2])) + max(min(vehicleWH[index1]), min(vehicleWH[index2])) / 2
- if smallestDistance == d1:
- score1 = 1
- traffic_dict['det'][index2][9][2] = score1
- traffic_dict['det'][index2][10] = judgeAccidentType(traffic_dict, index2)
- elif smallestDistance < d2:
- score1 = 1 - (smallestDistance - d1) / (d2 - d1)
- if 0 < score1 < 1:
- traffic_dict['det'][index2][9][2] = score1
- traffic_dict['det'][index2][10] = judgeAccidentType(traffic_dict, index2)
- else:
- traffic_dict['det'][index2][10] = judgeAccidentType(traffic_dict, index2)
- else:
- traffic_dict['det'][index2][10] = judgeAccidentType(traffic_dict, index2)
- return traffic_dict['det']
-
-
- # 计算两个contours之间的最短距离
- def array_distance(arr1, arr2):
- '''
- 计算两个数组中,每任意两个点之间L2距离
- arr1和arr2都必须是numpy数组
- 且维度分别是mx2,nx2
- 输出数组维度为mxn
- '''
- m, _ = arr1.shape
- n, _ = arr2.shape
- arr1_power = np.power(arr1, 2)
- arr1_power_sum = arr1_power[:, 0] + arr1_power[:, 1] # 第1区域,x与y的平方和
- arr1_power_sum = np.tile(arr1_power_sum, (n, 1)) # 将arr1_power_sum沿着y轴复制n倍,沿着x轴复制1倍,这里用于与arr2进行计算。 n x m 维度
- arr1_power_sum = arr1_power_sum.T # 将arr1_power_sum进行转置
- arr2_power = np.power(arr2, 2)
- arr2_power_sum = arr2_power[:, 0] + arr2_power[:, 1] # 第2区域,x与y的平方和
- arr2_power_sum = np.tile(arr2_power_sum, (m, 1)) # 将arr1_power_sum沿着y轴复制m倍,沿着x轴复制1倍,这里用于与arr1进行计算。 m x n 维度
- dis = arr1_power_sum + arr2_power_sum - (2 * np.dot(arr1, arr2.T)) # np.dot(arr1, arr2.T)矩阵相乘,得到xy的值。
- dis = np.sqrt(dis)
- return dis
-
-
- # 存储所有道路的信息
- def storageRoad(contours, allRoadContent, traffic_dict):
- speedRoadAngle = 0
- for cnt in contours: # 道路
- rect = cv2.minAreaRect(cnt)
- if rect[1][0] * rect[1][1] > traffic_dict['RoadArea']: # 过滤掉面积小于阈值的speedRoad
- if rect[1][0] <= rect[1][1]: # w <= h
- if rect[2] >= 0 and rect[2] < 90:
- speedRoadAngle = rect[2] + 90
- elif rect[2] == 90:
- speedRoadAngle = 0
- else:
- if rect[2] >= 0 and rect[2] <= 90:
- speedRoadAngle = rect[2]
- allRoadContent.append([cnt, speedRoadAngle, rect[1]])
- return allRoadContent
-
-
- # 存储所有vehicle的信息,方法1
- def storageVehicle1(traffic_dict, normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR, imgVehicle):
- count = 0
- for i in range(0, len(traffic_dict['vehicleCOOR']), 2):
- mask = np.zeros(imgVehicle.shape[:2], dtype="uint8")
- x0 = int(traffic_dict['vehicleCOOR'][i][0] * traffic_dict['ZoomFactor']['x'])
- y0 = int(traffic_dict['vehicleCOOR'][i][1] * traffic_dict['ZoomFactor']['y'])
- x1 = int(traffic_dict['vehicleCOOR'][i + 1][0] * traffic_dict['ZoomFactor']['x'])
- y1 = int(traffic_dict['vehicleCOOR'][i + 1][1] * traffic_dict['ZoomFactor']['y'])
- cv2.rectangle(mask, (x0, y0), (x1, y1), 255, -1, lineType=cv2.LINE_AA)
- imgVehicle_masked = cv2.bitwise_and(imgVehicle, imgVehicle, mask=mask)
- img2 = cv2.cvtColor(imgVehicle_masked, cv2.COLOR_BGR2GRAY)
- contours2, hierarchy2 = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- if len(contours2) != 0:
- if len(contours2) > 1: # 这里我通过比较同一检测框内各个contours对应的最小外接矩形的面积,来剔除那些存在干扰的contours,最终只保留一个contours
- vehicleArea = [] # 存储vehicle的最小外接矩形的面积
- for j in range(len(contours2)):
- rect = cv2.minAreaRect(contours2[j])
- vehicleArea.append(rect[1][0] * rect[1][1])
- maxAreaIndex = vehicleArea.index(max(vehicleArea))
- maxAreaContours = contours2[maxAreaIndex]
- normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR = saveVehicle1(traffic_dict,maxAreaContours,normVehicleBD,normVehicle,count,i,unnormVehicle, normVehicleCOOR)
- elif len(contours2) == 1:
- normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR = saveVehicle1(traffic_dict,contours2[0],normVehicleBD,normVehicle,count,i,unnormVehicle, normVehicleCOOR)
- else:
- traffic_dict['det'][int(i / 2)] = traffic_dict['det'][int(i / 2)] + [0, 0.3, 999, -1, 3]
- unnormVehicle.append(traffic_dict['det'][int(i / 2)])
- count += 1
- traffic_dict['det'] = normVehicle
- return traffic_dict['det'], normVehicleBD, unnormVehicle, normVehicleCOOR
-
-
- # 存储所有vehicle的信息,方法2
- def storageVehicle2(traffic_dict, normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR, imgVehicle):
- img = cv2.cvtColor(imgVehicle, cv2.COLOR_BGR2GRAY)
- count = 0
- for i in range(0, len(traffic_dict['vehicleCOOR']), 2):
- row1 = int(traffic_dict['vehicleCOOR'][i][1] * traffic_dict['ZoomFactor']['y'])
- row2 = int(traffic_dict['vehicleCOOR'][i + 1][1] * traffic_dict['ZoomFactor']['y'])
- col1 = int(traffic_dict['vehicleCOOR'][i][0] * traffic_dict['ZoomFactor']['x'])
- col2 = int(traffic_dict['vehicleCOOR'][i + 1][0] * traffic_dict['ZoomFactor']['x'])
- if row1 >= 2:
- row1 = row1 - 2
- if row2 <= (traffic_dict['modelSize'][1] - 2):
- row2 = row2 + 2
- if col1 >= 2:
- col1 = col1 - 2
- if col2 <= (traffic_dict['modelSize'][0] - 2):
- col2 = col2 + 2
- centerCOOR = (int((col1 + col2) / 2), int((row1 + row2) / 2))
- img1 = img[row1:row2, col1:col2]
- up = np.zeros((20, (col2 - col1)), dtype='uint8')
- left = np.zeros(((40 + row2 - row1), 20), dtype='uint8')
- img1 = np.concatenate((up, img1), axis=0)
- img1 = np.concatenate((img1, up), axis=0)
- img1 = np.concatenate((left, img1), axis=1)
- img2 = np.concatenate((img1, left), axis=1)
- contours2, hierarchy = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- if len(contours2) != 0:
- if len(contours2) > 1:
- vehicleArea = [] # 存储vehicle的最小外接矩形的面积
- for j in range(len(contours2)):
- rect = cv2.minAreaRect(contours2[j])
- vehicleArea.append(rect[1][0] * rect[1][1])
- maxAreaIndex = vehicleArea.index(max(vehicleArea))
- maxAreaContours = contours2[maxAreaIndex]
- normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR = saveVehicle2(traffic_dict,maxAreaContours,normVehicleBD,normVehicle,count,i,unnormVehicle,normVehicleCOOR,centerCOOR)
- elif len(contours2) == 1:
- normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR = saveVehicle2(traffic_dict,contours2[0],normVehicleBD,normVehicle,count,i,unnormVehicle,normVehicleCOOR,centerCOOR)
- else:
- traffic_dict['det'][int(i / 2)] = traffic_dict['det'][int(i / 2)] + [0, 0.3, 999, -1, 3]
- unnormVehicle.append(traffic_dict['det'][int(i / 2)])
- count += 1
- traffic_dict['det'] = normVehicle
- return traffic_dict['det'], normVehicleBD, unnormVehicle, normVehicleCOOR
-
-
- # 计算角度和长宽比得分
- def angleRoundness(normVehicleBD, vehicleBox, vehicleWH, allRoadContent, traffic_dict, normVehicleCOOR, imgVehicle):
- for i in range(len(normVehicleBD)):
- ellipse = cv2.fitEllipse(normVehicleBD[i])
- # print("line202", ellipse)
- vehicleAngle = 0
- if ellipse[2] >= 0 and ellipse[2] < 90:
- vehicleAngle = 90 + ellipse[2]
- elif ellipse[2] >= 90 and ellipse[2] < 180:
- vehicleAngle = ellipse[2] - 90
- elif ellipse[2] == 180:
- vehicleAngle = 90
- rect = cv2.minAreaRect(normVehicleBD[i])
- box = cv2.boxPoints(rect).astype(np.int32)
- center = normVehicleCOOR[i]
- vehicleBox.append(box)
- vehicleWH.append(rect[1])
- roundness = min(rect[1]) / max(rect[1])
- y_min = np.min(box[:, 1])
- y_max = np.max(box[:, 1])
- if len(allRoadContent) != 0:
- for j in range(len(allRoadContent)):
- flag = cv2.pointPolygonTest(allRoadContent[j][0], center, False)
- if flag >= 0:
- roadVehicleAngle = abs(vehicleAngle - allRoadContent[j][1])
- traffic_dict['det'][i] = traffic_dict['det'][i] + [roadVehicleAngle, roundness, 999, [-1, -1, -1], 666]
- if y_min > 0 and y_max < imgVehicle.shape[0]: # 过滤掉上下方被speedRoad的边界截断的vehicle
- if roadVehicleAngle >= traffic_dict['roadVehicleAngle']: # 当道路同水平方向的夹角与车辆同水平方向的夹角的差值在15°和75°之间时,需要将车辆框出来
- # print("line225 车辆角度,道路角度", vehicleAngle, allRoadContent[j][1])
- if roadVehicleAngle > 90:
- score1 = float((180 - roadVehicleAngle) / 90)
- else:
- score1 = float(roadVehicleAngle / 90)
- traffic_dict['det'][i][9][0] = score1
- if roundness > traffic_dict['roundness']:
- score2 = (min(rect[1]) - max(rect[1]) * traffic_dict['roundness']) / (max(rect[1]) * (1 - traffic_dict['roundness']))
- traffic_dict['det'][i][9][1] = score2
- break
- else:
- j += 1
- if len(traffic_dict['det'][i]) == 6:
- traffic_dict['det'][i][9][1] = supplementInformation(traffic_dict, i, roundness, y_min, y_max, imgVehicle, rect)
- else:
- traffic_dict['det'][i][9][1] = supplementInformation(traffic_dict, i, roundness, y_min, y_max, imgVehicle, rect)
- i += 1
- return vehicleBox, vehicleWH, traffic_dict['det']
-
-
- # 对于某一vehicle,以该vehicle的最小外接矩形的中心点为圆心O1,划定半径范围,求O1与半径范围内的其他vehicle的中心点之间的距离
- def vehicleDistance1(normVehicleCOOR, normVehicleBD, traffic_dict, vehicleWH):
- if len(normVehicleCOOR) > 1:
- for b in range(len(normVehicleCOOR)):
- contoursMinDistance = [] # 存储contours之间的最短距离
- tmp = normVehicleCOOR[b]
- normVehicleCOOR[b] = normVehicleCOOR[0]
- normVehicleCOOR[0] = tmp
- targetContours = [] # 存储目标vehicle和中心点同目标车辆中心点之间的距离小于traffic_dict['radius']的vehicle的box
- for c in range(1, len(normVehicleCOOR)):
- if two_points_distance(normVehicleCOOR[0][0], normVehicleCOOR[0][1], normVehicleCOOR[c][0], normVehicleCOOR[c][1]) <= traffic_dict['radius']:
- if normVehicleBD[b] not in targetContours:
- targetContours.append(normVehicleBD[b])
- if c == b:
- targetContours.append(normVehicleBD[0])
- else:
- targetContours.append(normVehicleBD[c])
- if len(targetContours) != 0:
- goalVehicleContour = np.squeeze(targetContours[0], 1)
- for d in range(1, len(targetContours)):
- elseVehicleContour = np.squeeze(targetContours[d], 1)
- dist_arr = array_distance(goalVehicleContour, elseVehicleContour)
- min_dist = dist_arr[dist_arr > 0].min()
- contoursMinDistance.append(min_dist)
- traffic_dict['det'][b][8] = min(contoursMinDistance)
- if traffic_dict['det'][b][8] < min(vehicleWH[b]) * traffic_dict['vehicleFactor']:
- score1 = 1 - traffic_dict['det'][b][8] / (min(vehicleWH[b]) * traffic_dict['vehicleFactor'])
- traffic_dict['det'][b][9][2] = score1
- traffic_dict['det'][b][10] = judgeAccidentType(traffic_dict, b)
- else:
- traffic_dict['det'][b][8] = 999
- traffic_dict['det'][b][10] = judgeAccidentType(traffic_dict, b)
- tmp = normVehicleCOOR[b]
- normVehicleCOOR[b] = normVehicleCOOR[0]
- normVehicleCOOR[0] = tmp
- else: # 路上只有一辆车
- if max(traffic_dict['det'][0][9]) == traffic_dict['det'][0][9][0] and traffic_dict['det'][0][9][0] != -1:
- traffic_dict['det'][0][10] = 0
- elif max(traffic_dict['det'][0][9]) == traffic_dict['det'][0][9][1] and traffic_dict['det'][0][9][1] != -1:
- traffic_dict['det'][0][10] = 1
- else:
- traffic_dict['det'][0][10] = 3
- return traffic_dict['det']
-
-
- # 计算vehicle的最小外接矩形中心点之间的距离和距离得分
- def vehicleDistance2(normVehicleCOOR, traffic_dict, vehicleWH):
- if len(normVehicleCOOR) > 1: # 有多辆车
- for b in range(len(normVehicleCOOR)):
- centerDistance = [] # 存储contours之间的最短距离
- tmp = normVehicleCOOR[b]
- normVehicleCOOR[b] = normVehicleCOOR[0]
- normVehicleCOOR[0] = tmp
- for c in range(1, len(normVehicleCOOR)):
- centerDistance.append(two_points_distance(normVehicleCOOR[0][0], normVehicleCOOR[0][1], normVehicleCOOR[c][0], normVehicleCOOR[c][1]))
- smallestDistance = min(centerDistance)
- index = centerDistance.index(smallestDistance)
- traffic_dict['det'][b][8] = smallestDistance
- if index == b - 1: # 序号0和b对应的vehicle
- traffic_dict['det'] = distanceScore(vehicleWH, 0, b, smallestDistance, traffic_dict)
- else:
- traffic_dict['det'] = distanceScore(vehicleWH, index+1, b, smallestDistance, traffic_dict)
- tmp = normVehicleCOOR[b]
- normVehicleCOOR[b] = normVehicleCOOR[0]
- normVehicleCOOR[0] = tmp
- else: # 路上只有一辆车
- if max(traffic_dict['det'][0][9]) == traffic_dict['det'][0][9][0] and traffic_dict['det'][0][9][0] != -1:
- traffic_dict['det'][0][10] = 0
- elif max(traffic_dict['det'][0][9]) == traffic_dict['det'][0][9][1] and traffic_dict['det'][0][9][1] != -1:
- traffic_dict['det'][0][10] = 1
- else:
- traffic_dict['det'][0][10] = 3
- return traffic_dict['det']
-
-
- def PostProcessing(mask, testImageArray, traffic_dict, file):
- """
- 对于字典traffic_dict中的各个键,说明如下:
- RoadArea:speedRoad的最小外接矩形的面积
- roadVehicleAngle:判定发生交通事故的speedRoad与vehicle间的最小夹角
- vehicleCOOR:是一个列表,用于存储被检测出的vehicle的坐标(vehicle检测模型)
- roundness:长宽比 ,vehicle的长与宽的比率,设置为0.7,若宽与长的比值大于0.7,则判定该vehicle发生交通事故
- ZoomFactor:存储的是图像在H和W方向上的缩放因子,其值小于1
- 'cls':类别号
- 'vehicleFactor':两辆车之间的安全距离被定义为:min(车辆1的宽,车辆2的宽) * vehicleFactor
- 'radius':半径,以某一vehicle的最小外接矩形的中点为圆心,以radius为半径,划定范围,过滤车辆
- 'distanceFlag':开关。计算vehicle之间的距离时,可选择不同的函数
- 'vehicleFlag':开关。存储vehicle的信息时,可选择不同的函数
- 未发生交通事故时,得分为-1,”事故类型“为3
- 最终输出格式:[[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 交通事故类别], ...]
- 交通事故类别:0表示交通事故是由角度原因判定的,1表示交通事故是由长宽比原因判定的,2表示交通事故是由最短距离原因判定的,3表示未发生交通事故。
- """
- det_cors = []
- for bb in traffic_dict['det']:
- det_cors.append((int(bb[1]), int(bb[2])))
- det_cors.append((int(bb[3]), int(bb[4])))
- traffic_dict['vehicleCOOR'] = det_cors
-
- testImageArray = testImageArray[:, :, 0]
- H, W = testImageArray.shape[0:2] # sourceImage的分辨率为1080x1920
- scaleH = traffic_dict['modelSize'][1] / H # 自适应调整缩放比例 # 360
- scaleW = traffic_dict['modelSize'][0] / W # 640
- # print("line354", scaleW, scaleH)
- traffic_dict['ZoomFactor'] = {'x': scaleW, 'y': scaleH}
- new_hw = [int(H * scaleH), int(W * scaleW)]
- mask = cv2.resize(mask, (new_hw[1], new_hw[0]))
- if len(mask.shape) == 3:
- mask = mask[:, :, 0]
-
- t1 = time.time()
- normVehicleBD = [] # 存储一副图像中合格vehicle的contours,合格vehicle,即:contours中的顶点数大于等于6
- imgRoad = mask.copy()
- imgVehicle = mask.copy()
- imgRoad[imgRoad == 2] = 0 # 将vehicle过滤掉,只包含背景和speedRoad
- imgVehicle[imgVehicle == 1] = 0 # 将speedRoad过滤掉,只包含背景和vehicle
- imgRoad = cv2.cvtColor(np.uint8(imgRoad), cv2.COLOR_RGB2BGR) # 道路
- imgVehicle = cv2.cvtColor(np.uint8(imgVehicle), cv2.COLOR_RGB2BGR) # 车辆
-
- # cv2.imwrite("./demo/" + file[:-4] + "_mask.png", mask * 50)
- # cv2.imwrite("./demo/" + file[:-4] + "_vehicle.png", imgVehicle*50)
- # cv2.imwrite("./demo/", + + file[:-4] + "_road.png", imgRoad*255)
-
- t2 = time.time()
- img1 = cv2.cvtColor(imgRoad, cv2.COLOR_BGR2GRAY)
- contours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- t3 = time.time()
-
- allRoadContent = [] # 存放所有的speedRoad信息,单个speedRoad的信息为:[cnt, speedRoadAngle, rect[1]]
- vehicleBox = [] # 存储合格vehicle的box参数,合格vehicle,即:contours顶点个数大于等于6
- vehicleWH = [] # 存储合格vehicle的宽高
- normVehicle = [] # 存储合格vehicle的信息
- unnormVehicle = [] # 存储不合格vehicle的信息,不合格vehicle,即:contours顶点个数小于6
- normVehicleCOOR = [] # 存储合格vehicle的中心点坐标
- allRoadContent = storageRoad(contours, allRoadContent, traffic_dict)
- t4 = time.time()
-
- # 开关。存储vehicle的信息时,可选择不同的函数
- if traffic_dict['vehicleFlag'] == True:
- # normVehicleCOOR存储车辆分割区域的最小外接矩形的中心点坐标;提取车辆分割区域时,使用bitwise_and(),速度较慢.
- traffic_dict['det'], normVehicleBD, unnormVehicle, normVehicleCOOR = storageVehicle1(traffic_dict, normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR, imgVehicle)
- else:
- # normVehicleCOOR存储的是yolo检测框的中心点坐标;提取车辆分割区域时,根据x和y的坐标直接从分割图像中抠图,然后再填充边界,和bitwise_and()相比,速度较快
- traffic_dict['det'], normVehicleBD, unnormVehicle, normVehicleCOOR = storageVehicle2(traffic_dict, normVehicleBD, normVehicle, unnormVehicle, normVehicleCOOR, imgVehicle)
- t5 = time.time()
- if len(normVehicleBD) != 0:
- t6 = time.time()
- vehicleBox, vehicleWH, traffic_dict['det'] = angleRoundness(normVehicleBD, vehicleBox, vehicleWH, allRoadContent, traffic_dict, normVehicleCOOR, imgVehicle)
- t7 = time.time()
- # 开关。计算vehicle之间的距离时,可选择不同的函数
- if traffic_dict['distanceFlag'] == True:
- traffic_dict['det'] = vehicleDistance1(normVehicleCOOR, normVehicleBD, traffic_dict, vehicleWH)
- else:
- traffic_dict['det'] = vehicleDistance2(normVehicleCOOR, traffic_dict, vehicleWH)
- t8 = time.time()
- targetList = traffic_dict['det']
- # print("line393", targetList)
- for i in range(len(targetList)):
- targetList[i][9] = max(targetList[i][9])
- if len(unnormVehicle) != 0:
- targetList = targetList + unnormVehicle
- t9 = time.time()
- # print("line462", targetList) # 目标对象list, [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 类别], ...]
- else:
- targetList = unnormVehicle
- t10 = time.time()
- time_infos = 'postTime:%.2f (分割时间:%.2f, findContours:%.2f, ruleJudge:%.2f, storageRoad:%.2f, storageVehicle:%.2f, angleRoundScore:%.2f, vehicleDistance:%.2f, mergeList:%.2f)' % (
- get_ms(t10, t1), get_ms(t2, t1), get_ms(t3, t2), get_ms(t10, t3), get_ms(t4, t3), get_ms(t5, t4), get_ms(t7, t6), get_ms(t8, t7), get_ms(t9, t8))
-
- # time_infos = 'postTime:%.2f (分割时间:%.2f, findContours:%.2f, ruleJudge:%.2f, storageRoad:%.2f, storageVehicle:%.2f)' % (
- # get_ms(t10, t1), get_ms(t2, t1), get_ms(t3, t2), get_ms(t10, t3), get_ms(t4, t3), get_ms(t5, t4))
-
- return targetList, time_infos
|