datacollector.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 copy
  16. from collections import Counter
  17. class Result(object):
  18. def __init__(self):
  19. self.res_dict = {
  20. 'det': dict(),
  21. 'mot': dict(),
  22. 'attr': dict(),
  23. 'kpt': dict(),
  24. 'video_action': dict(),
  25. 'skeleton_action': dict(),
  26. 'reid': dict(),
  27. 'det_action': dict(),
  28. 'cls_action': dict(),
  29. 'vehicleplate': dict(),
  30. 'vehicle_attr': dict(),
  31. 'lanes': dict(),
  32. 'vehicle_press': dict(),
  33. 'vehicle_retrograde': dict()
  34. }
  35. def update(self, res, name):
  36. self.res_dict[name].update(res)
  37. def get(self, name):
  38. if name in self.res_dict and len(self.res_dict[name]) > 0:
  39. return self.res_dict[name]
  40. return None
  41. def clear(self, name):
  42. self.res_dict[name].clear()
  43. class DataCollector(object):
  44. """
  45. DataCollector of Pipeline, collect results in every frames and assign it to each track ids.
  46. mainly used in mtmct.
  47. data struct:
  48. collector:
  49. - [id1]: (all results of N frames)
  50. - frames(list of int): Nx[int]
  51. - rects(list of rect): Nx[rect(conf, xmin, ymin, xmax, ymax)]
  52. - features(list of array(256,)): Nx[array(256,)]
  53. - qualities(list of float): Nx[float]
  54. - attrs(list of attr): refer to attrs for details
  55. - kpts(list of kpts): refer to kpts for details
  56. - skeleton_action(list of skeleton_action): refer to skeleton_action for details
  57. ...
  58. - [idN]
  59. """
  60. def __init__(self):
  61. #id, frame, rect, score, label, attrs, kpts, skeleton_action
  62. self.mots = {
  63. "frames": [],
  64. "rects": [],
  65. "attrs": [],
  66. "kpts": [],
  67. "features": [],
  68. "qualities": [],
  69. "skeleton_action": [],
  70. "vehicleplate": []
  71. }
  72. self.collector = {}
  73. def append(self, frameid, Result):
  74. mot_res = Result.get('mot')
  75. attr_res = Result.get('attr')
  76. kpt_res = Result.get('kpt')
  77. skeleton_action_res = Result.get('skeleton_action')
  78. reid_res = Result.get('reid')
  79. vehicleplate_res = Result.get('vehicleplate')
  80. rects = []
  81. if reid_res is not None:
  82. rects = reid_res['rects']
  83. elif mot_res is not None:
  84. rects = mot_res['boxes']
  85. for idx, mot_item in enumerate(rects):
  86. ids = int(mot_item[0])
  87. if ids not in self.collector:
  88. self.collector[ids] = copy.deepcopy(self.mots)
  89. self.collector[ids]["frames"].append(frameid)
  90. self.collector[ids]["rects"].append([mot_item[2:]])
  91. if attr_res:
  92. self.collector[ids]["attrs"].append(attr_res['output'][idx])
  93. if kpt_res:
  94. self.collector[ids]["kpts"].append(
  95. [kpt_res['keypoint'][0][idx], kpt_res['keypoint'][1][idx]])
  96. if skeleton_action_res and (idx + 1) in skeleton_action_res:
  97. self.collector[ids]["skeleton_action"].append(
  98. skeleton_action_res[idx + 1])
  99. else:
  100. # action model generate result per X frames, Not available every frames
  101. self.collector[ids]["skeleton_action"].append(None)
  102. if reid_res:
  103. self.collector[ids]["features"].append(reid_res['features'][
  104. idx])
  105. self.collector[ids]["qualities"].append(reid_res['qualities'][
  106. idx])
  107. if vehicleplate_res and vehicleplate_res['plate'][idx] != "":
  108. self.collector[ids]["vehicleplate"].append(vehicleplate_res[
  109. 'plate'][idx])
  110. def get_res(self):
  111. return self.collector
  112. def get_carlp(self, trackid):
  113. lps = self.collector[trackid]["vehicleplate"]
  114. counter = Counter(lps)
  115. carlp = counter.most_common()
  116. if len(carlp) > 0:
  117. return carlp[0][0]
  118. else:
  119. return None