You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
6.1KB

  1. import math
  2. import numpy as np
  3. import torch
  4. import time
  5. def dmpr_yolo( yolo_det, dmpr_det,pars):
  6. #if len(yolo_det)==0 or len(dmpr_det)==0:
  7. #print('line11:\n',yolo_det, dmpr_det,pars)
  8. time1=time.time()
  9. if len(yolo_det)==0:
  10. return yolo_det,' No yolo detections'
  11. img_shape = (pars['imgSize'][1],pars['imgSize'][0])
  12. cls = pars['carCls']; scaleRatio = pars['scaleRatio']
  13. illParkCls = pars['illCls'];border = pars['border']
  14. yolo_det = np.array(yolo_det)
  15. yolo_det_0 = yolo_det.copy()
  16. #print('-'*10,'line17:',yolo_det_0)
  17. # 过滤在图像边界的box(防止出现类似一小半车辆的情况)
  18. x_c = (yolo_det[:, 0] + yolo_det[:, 2]) / 2
  19. y_c = (yolo_det[:, 1] + yolo_det[:, 3]) / 2
  20. tmp = (x_c >= border) & (x_c <= (img_shape[1] - border)) & (y_c >= border) & (y_c <= (img_shape[0] - border))
  21. yolo_det = yolo_det[tmp]
  22. # 创建yolo_det_clone内容为x1, y1, x2, y2, conf, cls, unlabel (unlabel代表该类是否需要忽略,0:不忽略 其他:忽略)
  23. yolo_det_clone = yolo_det.copy()
  24. tmp_0_tensor = np.zeros([len(yolo_det), 1])
  25. yolo_det_clone = np.concatenate([yolo_det_clone, tmp_0_tensor], axis=1)
  26. # cls为需要计算的类别
  27. yolo_det = yolo_det[yolo_det[:, -1] == cls]
  28. # new_yolo_det为膨胀后数据,内容为x1, y1, x2, y2, flag (flag代表膨胀后车位内是否包含角点 且 与角点方向差值小于90度, 其值为第一个满足条件的角点索引)
  29. new_yolo_det = np.zeros([len(yolo_det), 7])
  30. # yolo框膨胀,长的边两边各膨胀0.4倍总长,短的边两边各膨胀0.2倍总长
  31. x_length = yolo_det[:, 2] - yolo_det[:, 0] #x2-x1
  32. y_length = yolo_det[:, 3] - yolo_det[:, 1] #y2-y1
  33. # x, y哪个方向差值大哪个方向膨胀的多
  34. x_dilate_coefficient = ((x_length > y_length) + 1)*scaleRatio
  35. y_dilate_coefficient = ((~(x_length > y_length)) + 1)*scaleRatio
  36. # 原始框中心点x_c, y_c
  37. new_yolo_det[:, 5] = (yolo_det[:, 0] + yolo_det[:, 2]) / 2
  38. new_yolo_det[:, 6] = (yolo_det[:, 1] + yolo_det[:, 3]) / 2
  39. # 膨胀
  40. new_yolo_det[:, 0] = np.round(yolo_det[:, 0] - x_dilate_coefficient * x_length).clip(0, img_shape[1]) #x1 膨胀
  41. new_yolo_det[:, 1] = np.round(yolo_det[:, 1] - y_dilate_coefficient * y_length).clip(0, img_shape[0]) #y1 膨胀
  42. new_yolo_det[:, 2] = np.round(yolo_det[:, 2] + x_dilate_coefficient * x_length).clip(0, img_shape[1]) #x2 膨胀
  43. new_yolo_det[:, 3] = np.round(yolo_det[:, 3] + y_dilate_coefficient * y_length).clip(0, img_shape[0]) #y2 膨胀
  44. m, n = new_yolo_det.size, dmpr_det.size
  45. if not m or not n:
  46. #print('##line47 original yolo_det_clone:',yolo_det_clone)
  47. yolo_det_clone[np.logical_and( yolo_det_clone[:,-1]==0,yolo_det_clone[:,-2]==cls),-2] = illParkCls
  48. #yolo_det_clone[yolo_det_clone[:, -1] == 0 & yolo_det_clone[:, -2==cls] , -2] = illParkCls
  49. return yolo_det_clone[:,0:6], ' no cars or T/L corners'
  50. new_yolo = new_yolo_det[:, np.newaxis, :].repeat(dmpr_det.shape[0], 1) # 扩展为 (m , n, 5)
  51. dmpr_det = dmpr_det[np.newaxis, ...].repeat(new_yolo_det.shape[0], 0)
  52. yolo_dmpr = np.concatenate((new_yolo, dmpr_det), axis=2) # (m, n, 10)
  53. x_p, y_p = yolo_dmpr[..., 8], yolo_dmpr[..., 9]
  54. x1, y1, x2, y2 = yolo_dmpr[..., 0], yolo_dmpr[..., 1], yolo_dmpr[..., 2], yolo_dmpr[..., 3]
  55. x_c, y_c = yolo_dmpr[..., 5], yolo_dmpr[..., 6]
  56. direction1 = np.arctan2(y_c - y_p, x_c - x_p) / math.pi * 180
  57. direction2 = yolo_dmpr[..., 10] / math.pi * 180
  58. direction3 = direction2 + 90 # L形角点另外一个方向
  59. direction3[direction3 > 180] -= 360
  60. ang_diff = direction1 - direction2
  61. ang_diff2 = direction1 - direction3
  62. # 判断膨胀后yolo框包含角点关系 && 包含角点的时候计算水平框中心点与角点的角度关系
  63. # direction ∈ (-180, 180) 若角差大于180,需算补角
  64. # T形角点比较一个方向,L形角点比较两个方向
  65. mask = (x_p >= x1) & (x_p <= x2) & (y_p >= y1) & (y_p <= y2) & \
  66. (((yolo_dmpr[..., 11] <= 0.5) & # T形角点情况
  67. (((ang_diff >= -90) & (ang_diff <= 90)) | ((ang_diff > 180) & ((360 - ang_diff) <= 90)) |
  68. (((ang_diff) < -180) & ((360 + ang_diff) <= 90)))) |
  69. ((yolo_dmpr[..., 11] > 0.5) & # L形角点情况
  70. (((ang_diff >= -90) & (ang_diff <= 90)) | ((ang_diff > 180) & ((360 - ang_diff) <= 90)) |
  71. (((ang_diff) < -180) & ((360 + ang_diff) <= 90))) &
  72. (((ang_diff2 >= -90) & (ang_diff2 <= 90)) | ((ang_diff2 > 180) & ((360 - ang_diff2) <= 90)) |
  73. (((ang_diff2) < -180) & ((360 + ang_diff2) <= 90)))))
  74. res = np.sum(mask, axis=1)
  75. yolo_det_clone[yolo_det_clone[:, -2] == cls, -1] = res
  76. #print('##line69 original yolo_det_clone:',yolo_det_clone)
  77. #yolo_det_clone[yolo_det_clone[:, -1] == 0, -2] = illParkCls
  78. #print('-'*20,'--line78',yolo_det_clone)
  79. yolo_det_clone[ np.logical_and( yolo_det_clone[:,-1]==0,yolo_det_clone[:,-2]==cls) ,-2 ] = illParkCls
  80. #print('-'*20,'--line80:',yolo_det_clone)
  81. yolo_det_clone = yolo_det_clone[:,0:6]
  82. time2=time.time()
  83. return np.array(yolo_det_clone), 'dmpr_yolo:%.1f'%( (time2-time1)*1000 )
  84. def stdc_yolo(stdc_det, yolo_det):
  85. im = np.uint8(stdc_det)
  86. x_c = ((yolo_det[:, 0] + yolo_det[:, 2]) // 2).astype(int)
  87. y_c = ((yolo_det[:, 1] + yolo_det[:, 3]) // 2).astype(int)
  88. yolo_filted = yolo_det[im[y_c, x_c] == 0]
  89. return yolo_filted
  90. def dmpr_yolo_stdc(predsList,pars):
  91. if len(predsList)==2:
  92. yolo_det, dmpr_det = predsList[0:2]
  93. else:
  94. yolo_det, dmpr_det,stdc_det = predsList[0:3]
  95. if len(yolo_det)==0:
  96. return yolo_det,' No yolo detections'
  97. if isinstance(yolo_det,list):
  98. yolo_det = np.array(yolo_det)
  99. if len(predsList)>2:
  100. yolo_det = stdc_yolo(stdc_det, yolo_det)
  101. return dmpr_yolo(yolo_det, dmpr_det,pars)