vehicle_pressing.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import numpy as np
  16. import math
  17. class VehiclePressingRecognizer(object):
  18. def __init__(self, cfg):
  19. self.cfg = cfg
  20. def judge(self, Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
  21. if (max(Ax1,Ax2)>=min(Bx1,Bx2) and min(Ax1,Ax2)<=max(Bx1,Bx2)) and \
  22. (max(Ay1,Ay2)>=min(By1,By2) and min(Ay1,Ay2)<=max(By1,By2)):
  23. if ((Bx1-Ax1)*(Ay2-Ay1)-(By1-Ay1)*(Ax2-Ax1)) * ((Bx2-Ax1)*(Ay2-Ay1)-(By2-Ay1)*(Ax2-Ax1))<=0 \
  24. and ((Ax1-Bx1)*(By2-By1)-(Ay1-By1)*(Bx2-Bx1)) * ((Ax2-Bx1)*(By2-By1)-(Ay2-By1)*(Bx2-Bx1)) <=0:
  25. return True
  26. else:
  27. return False
  28. else:
  29. return False
  30. def is_intersect(self, line, bbox):
  31. Ax1, Ay1, Ax2, Ay2 = line
  32. xmin, ymin, xmax, ymax = bbox
  33. bottom = self.judge(Ax1, Ay1, Ax2, Ay2, xmin, ymax, xmax, ymax)
  34. return bottom
  35. def run(self, lanes, det_res):
  36. intersect_bbox_list = []
  37. start_idx, boxes_num_i = 0, 0
  38. for i in range(len(lanes)):
  39. lane = lanes[i]
  40. if det_res is not None:
  41. det_res_i = {}
  42. boxes_num_i = det_res['boxes_num'][i]
  43. det_res_i['boxes'] = det_res['boxes'][start_idx:start_idx +
  44. boxes_num_i, :]
  45. intersect_bbox = []
  46. for line in lane:
  47. for bbox in det_res_i['boxes']:
  48. if self.is_intersect(line, bbox[2:]):
  49. intersect_bbox.append(bbox)
  50. intersect_bbox_list.append(intersect_bbox)
  51. start_idx += boxes_num_i
  52. return intersect_bbox_list
  53. def mot_run(self, lanes, det_res):
  54. intersect_bbox_list = []
  55. if det_res is None:
  56. return intersect_bbox_list
  57. lanes_res = lanes['output']
  58. for i in range(len(lanes_res)):
  59. lane = lanes_res[i]
  60. for line in lane:
  61. for bbox in det_res:
  62. if self.is_intersect(line, bbox[3:]):
  63. intersect_bbox_list.append(bbox)
  64. return intersect_bbox_list