Browse Source

debug trafficAccidentScore

master
wangjin0928 1 year ago
parent
commit
297eb4808e
2 changed files with 34 additions and 266 deletions
  1. BIN
      segutils/__pycache__/trafficUtils.cpython-38.pyc
  2. +34
    -266
      segutils/trafficUtils.py

BIN
segutils/__pycache__/trafficUtils.cpython-38.pyc View File


+ 34
- 266
segutils/trafficUtils.py View File

@@ -651,249 +651,6 @@ 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 trafficPostProcessingV5(traffic_dict):
"""
对于字典traffic_dict中的各个键,说明如下:
speedRoadArea:speedRoad的最小外接矩形的面积
vehicleArea:vehicle的最小外接矩形的面积
speedRoadVehicleAngleMin:判定发生交通事故的speedRoad与vehicle间的最小夹角
speedRoadVehicleAngleMax:判定发生交通事故的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])) )
det_cors.append( (int(bb[3]), int(bb[4])) )
traffic_dict['vehicleCoordinate'] = det_cors
t3 = time.time()
list17 = []
list21 = [] # 存储一副图像中vehicles的contours
t3_1=time.time()
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类型
t3_2 = time.time()
preds_squeeze_predict_speedRoad = colour_codes[image_speedRoad]
preds_squeeze_predict_vehicle = colour_codes[image_vehicle]
preds_squeeze_predict = colour_codes[traffic_dict['mask']]
t3_3 = time.time()
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()
print('#'*20, ' mask==2,1: %.1f color_code:%.1f cvtcolor:%.1f' %(get_ms(t3_2,t3_1 ),get_ms(t3_3,t3_2),get_ms(t4,t3_3) ))
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中的各个键,说明如下:
@@ -959,7 +716,7 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
list1.append(rect[1]) # 将道路的宽高存储在list1中
list2.append(list1)
list1 = []
# print("line206", list2)
# print("line203", list2)
for i in range(0, len(traffic_dict['vehicleCoordinate']), 2):
mask = np.zeros(image_vehicle.shape[:2], dtype="uint8")
@@ -978,6 +735,7 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
list24 = []
# print("line222", len(list21))
if len(list21) != 0:
for i in range(len(list21)): # 选取落在检测框范围内的分割区域,存在一个问题,就是其他vehicle的一小部分分割区域可能也落在了检测框中,这时会产生多个contours,
if len(list21[i]) > 1: # 这里我通过比较同一检测框内各个contours对应的最小外接矩形的面积,来剔除那些存在干扰的contours,最终只保留一个contours
@@ -1022,29 +780,41 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
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] or roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][0] = score1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
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] or roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][0] = score1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
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] or roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][0] = score1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
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] or roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][0] = score1
if roundness > traffic_dict['roundness']:
traffic_dict['det'][i][9][1] = score2
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
@@ -1070,7 +840,7 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
traffic_dict['det'][a][8] = min(list22)
list22 = []
if traffic_dict['det'][a][8] <= list3[a] * traffic_dict['vehicleFactor']:
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
@@ -1090,11 +860,11 @@ def trafficPostProcessingV2(traffic_dict, debug=False):
traffic_dict['det'][0][10] = 1
else:
traffic_dict['det'][0][10] = 3
# print("line337", traffic_dict['det']) # [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, [角度得分, 长宽比得分, 最小距离得分], 类别], ...]
# print("line347", traffic_dict['det']) # [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, [角度得分, 长宽比得分, 最小距离得分], 类别], ...]
list23 = traffic_dict['det']
for i in range(len(list23)):
list23[i][9] = max(list23[i][9])
# print("line341", list23) # 目标对象, [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 类别], ...]
# print("line351", list23) # 目标对象, [[cls, x0, y0, x1, y1, score, 角度, 长宽比, 最小距离, max([角度得分, 长宽比得分, 最小距离得分]), 类别], ...]
else:
print("分割模型未检测到vehicle!")
list23 = traffic_dict['det']
@@ -1102,6 +872,4 @@ 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

Loading…
Cancel
Save