datacollector.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class Result(object):
  17. def __init__(self):
  18. self.res_dict = {
  19. 'det': dict(),
  20. 'mot': dict(),
  21. 'attr': dict(),
  22. 'kpt': dict(),
  23. 'action': dict(),
  24. 'reid': dict()
  25. }
  26. def update(self, res, name):
  27. self.res_dict[name].update(res)
  28. def get(self, name):
  29. if name in self.res_dict and len(self.res_dict[name]) > 0:
  30. return self.res_dict[name]
  31. return None
  32. def clear(self, name):
  33. self.res_dict[name].clear()
  34. class DataCollector(object):
  35. """
  36. DataCollector of pphuman Pipeline, collect results in every frames and assign it to each track ids.
  37. mainly used in mtmct.
  38. data struct:
  39. collector:
  40. - [id1]: (all results of N frames)
  41. - frames(list of int): Nx[int]
  42. - rects(list of rect): Nx[rect(conf, xmin, ymin, xmax, ymax)]
  43. - features(list of array(256,)): Nx[array(256,)]
  44. - qualities(list of float): Nx[float]
  45. - attrs(list of attr): refer to attrs for details
  46. - kpts(list of kpts): refer to kpts for details
  47. - actions(list of actions): refer to actions for details
  48. ...
  49. - [idN]
  50. """
  51. def __init__(self):
  52. #id, frame, rect, score, label, attrs, kpts, actions
  53. self.mots = {
  54. "frames": [],
  55. "rects": [],
  56. "attrs": [],
  57. "kpts": [],
  58. "features": [],
  59. "qualities": [],
  60. "actions": []
  61. }
  62. self.collector = {}
  63. def append(self, frameid, Result):
  64. mot_res = Result.get('mot')
  65. attr_res = Result.get('attr')
  66. kpt_res = Result.get('kpt')
  67. action_res = Result.get('action')
  68. reid_res = Result.get('reid')
  69. rects = reid_res['rects'] if reid_res is not None else mot_res['boxes']
  70. for idx, mot_item in enumerate(rects):
  71. ids = int(mot_item[0])
  72. if ids not in self.collector:
  73. self.collector[ids] = copy.deepcopy(self.mots)
  74. self.collector[ids]["frames"].append(frameid)
  75. self.collector[ids]["rects"].append([mot_item[2:]])
  76. if attr_res:
  77. self.collector[ids]["attrs"].append(attr_res['output'][idx])
  78. if kpt_res:
  79. self.collector[ids]["kpts"].append(
  80. [kpt_res['keypoint'][0][idx], kpt_res['keypoint'][1][idx]])
  81. if action_res and (idx + 1) in action_res:
  82. self.collector[ids]["actions"].append(action_res[idx + 1])
  83. else:
  84. # action model generate result per X frames, Not available every frames
  85. self.collector[ids]["actions"].append(None)
  86. if reid_res:
  87. self.collector[ids]["features"].append(reid_res['features'][
  88. idx])
  89. self.collector[ids]["qualities"].append(reid_res['qualities'][
  90. idx])
  91. def get_res(self):
  92. return self.collector