Pārlūkot izejas kodu

debug trafficAccidentV7

master
wangjin0928 pirms 1 gada
vecāks
revīzija
01d11e0b39
4 mainītis faili ar 345 papildinājumiem un 94 dzēšanām
  1. +0
    -1
      AI.py
  2. Binārs
      __pycache__/AI.cpython-38.pyc
  3. Binārs
      segutils/__pycache__/trafficUtils.cpython-38.pyc
  4. +345
    -93
      segutils/trafficUtils.py

+ 0
- 1
AI.py Parādīt failu

@@ -117,7 +117,6 @@ def AI_process(im0s,model,segmodel,names,label_arraylist,rainbows,objectPar={ 'h
postPar['label_info']=label_info
tlist = list(postPar.keys()); tlist.sort()
if len(det_coords)> 0:
list8, image,time_infos = trafficPostProcessingV2(postPar)
Accident_results = np.array(list8,dtype=object)
acc_det=[]

Binārs
__pycache__/AI.cpython-38.pyc Parādīt failu


Binārs
segutils/__pycache__/trafficUtils.cpython-38.pyc Parādīt failu


+ 345
- 93
segutils/trafficUtils.py Parādīt failu

@@ -651,7 +651,7 @@ def trafficPostProcessingV2_1(traffic_dict):
t10 = time.time()
time_infos = 'postTime:%.2f ( predMapBinaryTime:%.2f , findContours:%.2f ruleJudge:%.2f coorsResize:%.2f )' %(get_ms(t10,t3), get_ms(t4,t3),get_ms(t6,t5),get_ms(t8,t7),get_ms(t10,t9) )
return list8, list11, image,time_infos
def trafficPostProcessingV2(traffic_dict, debug=False):
def trafficPostProcessingV6(traffic_dict):
"""
对于字典traffic_dict中的各个键,说明如下:
speedRoadArea:speedRoad的最小外接矩形的面积
@@ -667,6 +667,245 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
最终输出格式:[[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['vehicleCoordinate'] = det_cors
t3 = time.time()
list17 = []
list21 = [] # 存储一副图像中vehicles的contours
image_speedRoad = traffic_dict['mask'].copy()
image_vehicle = traffic_dict['mask'].copy()
image_speedRoad[image_speedRoad == 2] = 0 # 将vehicle过滤掉,只包含背景和speedRoad
image_vehicle[image_vehicle == 1] = 0 # 将speedRoad过滤掉,只包含背景和vehicle
for key in traffic_dict['label_info']:
list17.append(traffic_dict['label_info'][key])
colour_codes = np.array(list17) # [[0 0 0],[128 0 0],[0 128 0]] ndarray类型
preds_squeeze_predict_speedRoad = colour_codes[image_speedRoad]
preds_squeeze_predict_vehicle = colour_codes[image_vehicle]
preds_squeeze_predict = colour_codes[traffic_dict['mask']]
image = cv2.cvtColor(np.uint8(preds_squeeze_predict), cv2.COLOR_RGB2BGR)
image_speedRoad = cv2.cvtColor(np.uint8(preds_squeeze_predict_speedRoad), cv2.COLOR_RGB2BGR) # 道路
image_vehicle = cv2.cvtColor(np.uint8(preds_squeeze_predict_vehicle), cv2.COLOR_RGB2BGR) # 车辆
t4 = time.time()
img1 = cv2.cvtColor(image_speedRoad, cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
t5 = time.time()
list1 = [] # 过渡使用
list2 = [] # 存放道路坐标(Xmin,Xmax,Ymin,Ymax)
list3 = [] # # 存储vehicle最小外接矩形的最短边
list4 = [] # list4存储车辆的box参数
list7 = [] # 存储vehicle的宽高
for cnt in contours: # 道路
rect = cv2.minAreaRect(cnt)
if rect[1][0] * rect[1][1] > traffic_dict['speedRoadArea']: # 过滤掉面积小于阈值的speedRoad
box = cv2.boxPoints(rect).astype(np.int32)
Xmin = min(box[0][0], box[1][0], box[2][0], box[3][0])
Xmax = max(box[0][0], box[1][0], box[2][0], box[3][0])
Ymin = min(box[0][1], box[1][1], box[2][1], box[3][1])
Ymax = max(box[0][1], box[1][1], box[2][1], box[3][1])
list1.append(Xmin) # 将道路矩形框四个顶点的Xmin,Xmax,Ymin,Ymax存储在list1中
list1.append(Xmax)
list1.append(Ymin)
list1.append(Ymax)
list1.append(rect[2]) # 将道路矩形框与水平方向的夹角存储在list1中
list1.append(rect[1]) # 将道路的宽高存储在list1中
list2.append(list1)
list1 = []
for i in range(0, len(traffic_dict['vehicleCoordinate']), 2):
mask = np.zeros(image_vehicle.shape[:2], dtype="uint8")
x0 = int(traffic_dict['vehicleCoordinate'][i][0] * traffic_dict['ZoomFactor']['x'])
y0 = int(traffic_dict['vehicleCoordinate'][i][1] * traffic_dict['ZoomFactor']['y'])
x1 = int(traffic_dict['vehicleCoordinate'][i + 1][0] * traffic_dict['ZoomFactor']['x'])
y1 = int(traffic_dict['vehicleCoordinate'][i + 1][1] * traffic_dict['ZoomFactor']['y'])
cv2.rectangle(mask, (x0, y0), (x1, y1), 255, -1, lineType=cv2.LINE_AA)
image_vehicle_masked = cv2.bitwise_and(image_vehicle, image_vehicle, mask=mask)
img2 = cv2.cvtColor(image_vehicle_masked, cv2.COLOR_BGR2GRAY)
contours2, hierarchy2 = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
list21.append(contours2)
for i in range(len(list21)):
for cnt in list21[i]:
flag = False
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect).astype(np.int32)
if rect[1][0] * rect[1][1] > traffic_dict['vehicleArea']: # 过滤掉面积小于阈值的vehicle
list3.append(min(rect[1]))
list4.append(box)
list7.append(rect[1])
for j in range(len(list2)):
if (rect[0][0] > list2[j][0] and rect[0][0] < list2[j][1]) and (rect[0][1] > list2[j][2] and rect[0][1] < list2[j][3]): # 判断车辆矩形框的中心点坐标是否在道路矩形框Xmin,Xmax,Ymin和Ymax的范围内;
box = cv2.boxPoints(rect).astype(np.int32) # 将Box2D结构作为输入并返回4个角点。
if (box[0][0] >= list2[j][0] and box[0][0] <= list2[j][1] and box[0][1] >= list2[j][2] and box[0][1] <= list2[j][3]) and (box[1][0] >= list2[j][0] and box[1][0] <= list2[j][1] and box[1][1] >= list2[j][2] and box[1][1] <= list2[j][3]) and (box[2][0] >= list2[j][0] and box[2][0] <= list2[j][1] and box[2][1] >= list2[j][2] and box[2][1] <= list2[j][3]) and (box[3][0] >= list2[j][0] and box[3][0] <= list2[j][1] and box[3][1] >= list2[j][2] and box[3][1] <= list2[j][3]): # 比较车辆矩形框四个顶点的坐标是否都在道路矩形框Xmin,Xmax,Ymin和Ymax的范围内,若都在,则说明该车辆在这条道路上。
y_min = min(box[0][1], box[1][1], box[2][1], box[3][1])
y_max = max(box[0][1], box[1][1], box[2][1], box[3][1])
angle = abs(rect[2] - list2[j][4])
roundness = min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1])
traffic_dict['det'][i].append(angle)
traffic_dict['det'][i].append(roundness)
traffic_dict['det'][i].append(999) # 给最小距离占位
traffic_dict['det'][i].append([-1, -1, -1])
traffic_dict['det'][i].append(666) # 给事故类别占位
if abs(rect[2] - list2[j][4]) >= traffic_dict['speedRoadVehicleAngleMin'] and abs(
rect[2] - list2[j][4]) <= traffic_dict[
'speedRoadVehicleAngleMax']: # 当道路同水平方向的夹角与车辆同水平方向的夹角的差值在15°和75°之间时,需要将车辆框出来
score1 = float(abs(rect[2] - list2[j][4]) / 90)
traffic_dict['det'][i][9][0] = score1
if min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1]) > traffic_dict['roundness']:
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) * traffic_dict[
'roundness']) / (max(rect[1][0], rect[1][1]) * (
1 - traffic_dict['roundness']))
traffic_dict['det'][i][9][1] = score2
elif list2[j][5][0] < list2[j][5][1]: # speedRoad的最小外接矩形的w<h,说明该speedRoad是左侧道路
if y_min > 0 and y_max < image.shape[0]: # 过滤掉上下方被speedRoad的边界截断的vehicle
if abs(rect[2] - list2[j][4]) >= 0 and abs(rect[2] - list2[j][4]) < traffic_dict[
'speedRoadVehicleAngleMin']:
if (rect[1][0] >= rect[1][1]) or (
min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1])) >= \
traffic_dict['roundness']:
if rect[2] == list2[j][4]:
score1 = 0.10
traffic_dict['det'][i][9][0] = score1
else:
score1 = float(abs(rect[2] - list2[j][4]) / 90)
traffic_dict['det'][i][9][0] = score1
if min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1]) > traffic_dict[
'roundness']:
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) *
traffic_dict['roundness']) / (
max(rect[1][0], rect[1][1]) * (
1 - traffic_dict['roundness']))
traffic_dict['det'][i][9][1] = score2
elif abs(rect[2] - list2[j][4]) > traffic_dict['speedRoadVehicleAngleMax'] and abs(
rect[2] - list2[j][4]) <= 90:
if rect[1][0] <= rect[1][1] or (
min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1])) >= \
traffic_dict['roundness']:
score1 = float(abs(rect[2] - list2[j][4]) / 90)
traffic_dict['det'][i][9][0] = score1
if min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1]) > traffic_dict[
'roundness']:
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) *
traffic_dict['roundness']) / (
max(rect[1][0], rect[1][1]) * (
1 - traffic_dict['roundness']))
traffic_dict['det'][i][9][1] = score2
elif list2[j][5][0] > list2[j][5][1]: # speedRoad的最小外接矩形的w>h,说明该speedRoad是右侧道路
if y_min > 0 and y_max < image.shape[0]:
if abs(rect[2] - list2[j][4]) >= 0 and abs(rect[2] - list2[j][4]) < traffic_dict[
'speedRoadVehicleAngleMin']:
if rect[1][0] <= rect[1][1] or (
min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1])) >= \
traffic_dict['roundness']:
if rect[2] == list2[j][4]:
score1 = 0.10
traffic_dict['det'][i][9][0] = score1
else:
score1 = float(abs(rect[2] - list2[j][4]) / 90)
traffic_dict['det'][i][9][0] = score1
if min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1]) > traffic_dict['roundness']:
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) *
traffic_dict['roundness']) / (
max(rect[1][0], rect[1][1]) * (
1 - traffic_dict['roundness']))
traffic_dict['det'][i][9][1] = score2
elif abs(rect[2] - list2[j][4]) > traffic_dict['speedRoadVehicleAngleMax'] and abs(
rect[2] - list2[j][4]) <= 90:
if rect[1][0] >= rect[1][1] or (
min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1])) >= \
traffic_dict['roundness']:
score1 = float(abs(rect[2] - list2[i][4]) / 90)
traffic_dict['det'][i][9][0] = score1
if min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1]) > traffic_dict[
'roundness']:
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) *
traffic_dict['roundness']) / (
max(rect[1][0], rect[1][1]) * (
1 - traffic_dict['roundness']))
traffic_dict['det'][i][9][1] = score2
break
else:
j += 1
flag = True
if flag == True:
break
i += 1
list22 = []
a = 0
for a in range(len(list4)):
tmp = list4[a]
list4[a] = list4[0]
list4[0] = tmp
for j in range(1, len(list4)):
for k in range(4):
point1 = [list4[0][k][0], list4[0][k][1]]
for w in range(3):
line1 = [list4[j][w][0], list4[j][w][1], list4[j][w + 1][0], list4[j][w + 1][1]]
list22.append(point_to_line_distance(point1, line1))
w += 1
line1 = [list4[j][3][0], list4[j][3][1], list4[j][0][0], list4[j][0][1]]
list22.append(point_to_line_distance(point1, line1))
k += 1
j += 1
traffic_dict['det'][a][8] = min(list22)
list22 = []
if traffic_dict['det'][a][8] <= list3[a] * traffic_dict['vehicleFactor']:
score1 = 1 - traffic_dict['det'][a][8] / (list3[a] * traffic_dict['vehicleFactor'])
traffic_dict['det'][a][9][2] = score1
if max(traffic_dict['det'][a][9]) == traffic_dict['det'][a][9][0] and traffic_dict['det'][a][9][0] != -1:
traffic_dict['det'][a][10] = 0
elif max(traffic_dict['det'][a][9]) == traffic_dict['det'][a][9][1] and traffic_dict['det'][a][9][1] != -1:
traffic_dict['det'][a][10] = 1
elif max(traffic_dict['det'][a][9]) == traffic_dict['det'][a][9][2] and traffic_dict['det'][a][9][2] != -1:
traffic_dict['det'][a][10] = 2
else:
traffic_dict['det'][a][10] = 3
a += 1
# print(traffic_dict['det']) # [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, [角度得分, 长宽比得分, 最小距离得分], 类别], ...]
list23 = traffic_dict['det']
traffic_dict['det'] = []
for i in range(len(list23)):
list23[i][9] = max(list23[i][9])
#print("line393", list23) # 目标对象, [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 类别], ...]
t6 = time.time()
time_infos = 'postTime:%.2f (分割时间:%.2f, findContours:%.2f ruleJudge:%.2f)' % (
get_ms(t6, t3), get_ms(t4, t3), get_ms(t5, t4), get_ms(t6, t5))
return list23, image, time_infos
def trafficPostProcessingV2(traffic_dict, debug=False):
"""
对于字典traffic_dict中的各个键,说明如下:
speedRoadArea:speedRoad的最小外接矩形的面积
speedRoadVehicleAngleMin:判定发生交通事故的speedRoad与vehicle间的最小夹角
vehicleCoordinate:是一个列表,用于存储被检测出的vehicle的坐标(vehicle检测模型)
roundness:圆度 ,vehicle的长与宽的比率,设置为0.7,若宽与长的比值大于0.7,则判定该vehicle发生交通事故
ZoomFactor:存储的是图像在H和W方向上的缩放因子,其值小于1
'cls':类别号
'vehicleFactor':两辆车之间的安全距离被定义为:min(车辆1的宽,车辆2的宽) * vehicleFactor
未发生交通事故时,得分为-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])))
@@ -674,18 +913,24 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
traffic_dict['vehicleCoordinate'] = det_cors
t3 = time.time()
list17 = []
list21 = [] # 存储一副图像中vehicles的contours
list8 = []
list10 = [] # 存储一副图像中vehicles的contours
image_speedRoad = traffic_dict['mask'].copy()
image_vehicle = traffic_dict['mask'].copy()
image_speedRoad[image_speedRoad == 2] = 0 # 将vehicle过滤掉,只包含背景和speedRoad
image_vehicle[image_vehicle == 1] = 0 # 将speedRoad过滤掉,只包含背景和vehicle
image_speedRoad = cv2.cvtColor(np.uint8(image_speedRoad), cv2.COLOR_RGB2BGR) # 道路
image_vehicle = cv2.cvtColor(np.uint8(image_vehicle), cv2.COLOR_RGB2BGR) # 车辆
# image_vehicle = cv2.resize(image_vehicle, (3840, 2160))
# cv2.imwrite('./demo/vehicle.png', image_vehicle)
# image_speedRoad = cv2.resize(image_speedRoad, (1920, 1080))
# cv2.imwrite('./demo/speedRoad.png', image_speedRoad)
if debug == False:
for key in traffic_dict['label_info']:
list17.append(traffic_dict['label_info'][key])
colour_codes = np.array(list17) # [[0 0 0],[128 0 0],[0 128 0]] ndarray类型
list8.append(traffic_dict['label_info'][key])
colour_codes = np.array(list8) # [[0 0 0],[128 0 0],[0 128 0]] ndarray类型
preds_squeeze_predict = colour_codes[traffic_dict['mask']]
image = cv2.cvtColor(np.uint8(preds_squeeze_predict), cv2.COLOR_RGB2BGR)
else:
@@ -693,13 +938,14 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
t4 = time.time()
img1 = cv2.cvtColor(image_speedRoad, cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# contours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv2.findContours(img1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
t5 = time.time()
list1 = [] # 过渡使用
list2 = [] # 存放道路坐标(Xmin,Xmax,Ymin,Ymax)
list3 = [] # # 存储vehicle最小外接矩形的最短边
list4 = [] # list4存储车辆的box参数
list7 = [] # 存储vehicle的宽高
list5 = [] # 存储vehicle的宽高
for cnt in contours: # 道路
rect = cv2.minAreaRect(cnt)
if rect[1][0] * rect[1][1] > traffic_dict['speedRoadArea']: # 过滤掉面积小于阈值的speedRoad
@@ -712,11 +958,21 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
list1.append(Xmax)
list1.append(Ymin)
list1.append(Ymax)
list1.append(rect[2]) # 将道路矩形框与水平方向的夹角存储在list1中
if rect[1][0] <= rect[1][1]:
if rect[2] >= 0 and rect[2] < 90:
speedRoadAngle = rect[2] + 90
elif rect[2] == 90:
speedRoadAngle = 0
elif rect[2] >= 0 and rect[2] <= 90:
speedRoadAngle = rect[2]
list1.append(speedRoadAngle) # 将道路矩形框与水平方向的夹角存储在list1中
list1.append(rect[1]) # 将道路的宽高存储在list1中
list2.append(list1)
list1 = []
# print("line203", list2)
# print("line213", list2)
list14 = []
list15 = []
count = 0
for i in range(0, len(traffic_dict['vehicleCoordinate']), 2):
mask = np.zeros(image_vehicle.shape[:2], dtype="uint8")
@@ -727,100 +983,100 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
cv2.rectangle(mask, (x0, y0), (x1, y1), 255, -1, lineType=cv2.LINE_AA)
image_vehicle_masked = cv2.bitwise_and(image_vehicle, image_vehicle, mask=mask)
# cv2.imwrite('./demo/vehicle_masked.png', image_vehicle_masked)
# cv2.imwrite('./demo/vehicle_masked_' + str(i) + '.png', cv2.resize(image_vehicle_masked, (3840, 2160)))
# cv2.imwrite('./demo/vehicle_masked_' + str(i) + '.png', cv2.resize(image_vehicle_masked, (1920, 1080)))
img2 = cv2.cvtColor(image_vehicle_masked, cv2.COLOR_BGR2GRAY)
contours2, hierarchy2 = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours2) != 0:
list21.append(contours2)
# contours2, hierarchy2 = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours2, hierarchy2 = cv2.findContours(img2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
list24 = []
if len(contours2) != 0:
list10.append(contours2)
list14.append(traffic_dict['det'][count])
else:
traffic_dict['det'][i].append(5)
traffic_dict['det'][i].append(0.5)
traffic_dict['det'][i].append(15)
traffic_dict['det'][i].append(0.01)
traffic_dict['det'][i].append(3)
list15.append(traffic_dict['det'][i])
count += 1
traffic_dict['det'] = list14
# list12 = []
# print("line222", len(list21))
if len(list21) != 0:
for i in range(len(list21)): # 选取落在检测框范围内的分割区域,存在一个问题,就是其他vehicle的一小部分分割区域可能也落在了检测框中,这时会产生多个contours,
if len(list21[i]) > 1: # 这里我通过比较同一检测框内各个contours对应的最小外接矩形的面积,来剔除那些存在干扰的contours,最终只保留一个contours
for j in range(len(list21[i])):
contours_j = list21[i][j]
if len(list10) != 0:
for i in range(len(list10)): # 选取落在检测框范围内的分割区域,存在一个问题,就是其他vehicle的一小部分分割区域可能也落在了检测框中,这时会产生多个contours,
list12 = []
if len(list10[i]) > 1: # 这里我通过比较同一检测框内各个contours对应的最小外接矩形的面积,来剔除那些存在干扰的contours,最终只保留一个contours
for j in range(len(list10[i])):
contours_j = list10[i][j]
rect = cv2.minAreaRect(contours_j)
list24.append(rect)
list25 = [list24[g][1][0] * list24[g][1][1] for g in range(len(list24))]
maxAreaIndex = list25.index(max(list25))
rect = list24[maxAreaIndex]
list12.append(rect)
# print("line257", rect)
# print("line258", len(list10[i]), len(list12))
list13 = [list12[g][1][0] * list12[g][1][1] for g in range(len(list12))]
maxAreaIndex = list13.index(max(list13))
rect = list12[maxAreaIndex]
box = cv2.boxPoints(rect).astype(np.int32)
elif len(list21[i]) == 1:
rect = cv2.minAreaRect(np.array(list21[i][0]))
# print("line261", list10[i])
# print("line262", list13)
# print("line263", maxAreaIndex)
maxAreacontours = list10[i][maxAreaIndex]
ellipse = cv2.fitEllipse(maxAreacontours) # (中心点坐标(x,y),(短轴长度,长轴长度),短轴与水平线的夹角)
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
elif len(list10[i]) == 1:
rect = cv2.minAreaRect(np.array(list10[i][0]))
box = cv2.boxPoints(rect).astype(np.int32)
maxAreacontours = list10[i][0]
ellipse = cv2.fitEllipse(maxAreacontours)
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
list3.append(min(rect[1]))
list4.append(box)
list7.append(rect[1])
list5.append(rect[1])
for j in range(len(list2)):
if (rect[0][0] > list2[j][0] and rect[0][0] < list2[j][1]) and (rect[0][1] > list2[j][2] and rect[0][1] < list2[j][3]): # 判断车辆矩形框的中心点坐标是否在道路矩形框Xmin,Xmax,Ymin和Ymax的范围内;
box = cv2.boxPoints(rect).astype(np.int32) # 将Box2D结构作为输入并返回4个角点。
if (box[0][0] >= list2[j][0] and box[0][0] <= list2[j][1] and box[0][1] >= list2[j][2] and box[0][1] <= list2[j][3]) and (box[1][0] >= list2[j][0] and box[1][0] <= list2[j][1] and box[1][1] >= list2[j][2] and box[1][1] <= list2[j][3]) and (box[2][0] >= list2[j][0] and box[2][0] <= list2[j][1] and box[2][1] >= list2[j][2] and box[2][1] <= list2[j][3]) and (box[3][0] >= list2[j][0] and box[3][0] <= list2[j][1] and box[3][1] >= list2[j][2] and box[3][1] <= list2[j][3]): # 比较车辆矩形框四个顶点的坐标是否都在道路矩形框Xmin,Xmax,Ymin和Ymax的范围内,若都在,则说明该车辆在这条道路上。
y_min = min(box[0][1], box[1][1], box[2][1], box[3][1])
y_max = max(box[0][1], box[1][1], box[2][1], box[3][1])
angle = abs(rect[2] - list2[j][4])
if list2[j][4] == 0:
list2[j][4] = 90
speedRoadVehicleAngle = abs(vehicleAngle - list2[j][4])
# print("line288", vehicleAngle, list2[j][4], speedRoadVehicleAngle)
roundness = min(rect[1][0], rect[1][1]) / max(rect[1][0], rect[1][1])
traffic_dict['det'][i].append(angle)
traffic_dict['det'][i].append(speedRoadVehicleAngle)
traffic_dict['det'][i].append(roundness)
traffic_dict['det'][i].append(999) # 给最小距离占位
traffic_dict['det'][i].append([-1, -1, -1])
traffic_dict['det'][i].append(666) # 给事故类别占位
score1 = float(abs(rect[2] - list2[j][4]) / 90)
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) * traffic_dict[
'roundness']) / (max(rect[1][0], rect[1][1]) * (
1 - traffic_dict['roundness']))
if abs(rect[2] - list2[j][4]) >= traffic_dict['speedRoadVehicleAngleMin'] and abs(
rect[2] - list2[j][4]) <= traffic_dict[
'speedRoadVehicleAngleMax']: # 当道路同水平方向的夹角与车辆同水平方向的夹角的差值在15°和75°之间时,需要将车辆框出来
traffic_dict['det'][i][9][0] = score1
if y_min > 0 and y_max < image_vehicle.shape[0]: # 过滤掉上下方被speedRoad的边界截断的vehicle
if speedRoadVehicleAngle >= traffic_dict['speedRoadVehicleAngleMin']: # 当道路同水平方向的夹角与车辆同水平方向的夹角的差值在15°和75°之间时,需要将车辆框出来
if speedRoadVehicleAngle > 90:
score1 = float((180 - speedRoadVehicleAngle) / 90)
else:
score1 = float(speedRoadVehicleAngle / 90)
traffic_dict['det'][i][9][0] = score1
if roundness > traffic_dict['roundness']:
score2 = (min(rect[1][0], rect[1][1]) - max(rect[1][0], rect[1][1]) * traffic_dict[
'roundness']) / (max(rect[1][0], rect[1][1]) * (1 - traffic_dict['roundness']))
traffic_dict['det'][i][9][1] = score2
elif list2[j][5][0] < list2[j][5][1]: # speedRoad的最小外接矩形的w<h,说明该speedRoad是左侧道路
if y_min > 0 and y_max < image_vehicle.shape[0]: # 过滤掉上下方被speedRoad的边界截断的vehicle
if abs(rect[2] - list2[j][4]) >= 0 and abs(rect[2] - list2[j][4]) < traffic_dict['speedRoadVehicleAngleMin']:
if rect[1][0] >= rect[1][1]:
if score1 != 0:
traffic_dict['det'][i][9][0] = score1
else:
traffic_dict['det'][i][9][0] = 1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
elif abs(rect[2] - list2[j][4]) > traffic_dict['speedRoadVehicleAngleMax'] and abs(
rect[2] - list2[j][4]) <= 90:
if rect[1][0] <= rect[1][1]:
if score1 != 0:
traffic_dict['det'][i][9][0] = score1
else:
traffic_dict['det'][i][9][0] = 1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
elif list2[j][5][0] > list2[j][5][1]: # speedRoad的最小外接矩形的w>h,说明该speedRoad是右侧道路
if y_min > 0 and y_max < image_vehicle.shape[0]:
if abs(rect[2] - list2[j][4]) >= 0 and abs(rect[2] - list2[j][4]) < traffic_dict['speedRoadVehicleAngleMin']:
if rect[1][0] <= rect[1][1]:
if score1 != 0:
traffic_dict['det'][i][9][0] = score1
else:
traffic_dict['det'][i][9][0] = 1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
elif abs(rect[2] - list2[j][4]) > traffic_dict['speedRoadVehicleAngleMax'] and abs(
rect[2] - list2[j][4]) <= 90:
if rect[1][0] >= rect[1][1]:
if score1 != 0:
traffic_dict['det'][i][9][0] = score1
else:
traffic_dict['det'][i][9][0] = 1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
break
else:
j += 1
i += 1
list22 = []
list11 = []
if len(list4) > 1:
for a in range(len(list4)):
tmp = list4[a]
@@ -831,19 +1087,19 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
point1 = [list4[0][k][0], list4[0][k][1]]
for w in range(3):
line1 = [list4[j][w][0], list4[j][w][1], list4[j][w + 1][0], list4[j][w + 1][1]]
list22.append(point_to_line_distance(point1, line1))
list11.append(point_to_line_distance(point1, line1))
w += 1
line1 = [list4[j][3][0], list4[j][3][1], list4[j][0][0], list4[j][0][1]]
list22.append(point_to_line_distance(point1, line1))
list11.append(point_to_line_distance(point1, line1))
k += 1
j += 1
traffic_dict['det'][a][8] = min(list22)
list22 = []
traffic_dict['det'][a][8] = min(list11)
list11 = []
if traffic_dict['det'][a][8] < list3[a] * traffic_dict['vehicleFactor']:
score1 = 1 - traffic_dict['det'][a][8] / (list3[a] * traffic_dict['vehicleFactor'])
traffic_dict['det'][a][9][2] = score1
# print("line334", traffic_dict['det']) # [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, [角度得分, 长宽比得分, 最小距离得分], 类别], ...]
if max(traffic_dict['det'][a][9]) == traffic_dict['det'][a][9][0] and traffic_dict['det'][a][9][0] != -1:
traffic_dict['det'][a][10] = 0
elif max(traffic_dict['det'][a][9]) == traffic_dict['det'][a][9][1] and traffic_dict['det'][a][9][1] != -1:
@@ -860,18 +1116,13 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
traffic_dict['det'][0][10] = 1
else:
traffic_dict['det'][0][10] = 3
# print("line347", traffic_dict['det']) # [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, [角度得分, 长宽比得分, 最小距离得分], 类别], ...]
# print("line351", traffic_dict['det']) # [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, [角度得分, 长宽比得分, 最小距离得分], 类别], ...]
list23 = traffic_dict['det']
for i in range(len(list23)):
if len(list23[i]) != 11:
list23[i].append(90)
list23[i].append(0.7)
list23[i].append(10)
list23[i].append(1)
list23[i].append(2)
else:
list23[i][9] = max(list23[i][9])
# print("line351", list23) # 目标对象, [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 类别], ...]
list23[i][9] = max(list23[i][9])
for i in range(len(list15)):
list23.append(list15[i])
# print("line357", list23) # 目标对象, [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 类别], ...]
else:
print("分割模型未检测到vehicle!")
list23 = traffic_dict['det']
@@ -879,4 +1130,5 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
t6 = time.time()
time_infos = 'postTime:%.2f (分割时间:%.2f, findContours:%.2f ruleJudge:%.2f)' % (
get_ms(t6, t3), get_ms(t4, t3), get_ms(t5, t4), get_ms(t6, t5))
return list23, image, time_infos
return list23, image, time_infos

Notiek ielāde…
Atcelt
Saglabāt