action_utils.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. class KeyPointSequence(object):
  15. def __init__(self, max_size=100):
  16. self.frames = 0
  17. self.kpts = []
  18. self.bboxes = []
  19. self.max_size = max_size
  20. def save(self, kpt, bbox):
  21. self.kpts.append(kpt)
  22. self.bboxes.append(bbox)
  23. self.frames += 1
  24. if self.frames == self.max_size:
  25. return True
  26. return False
  27. class KeyPointBuff(object):
  28. def __init__(self, max_size=100):
  29. self.flag_track_interrupt = False
  30. self.keypoint_saver = dict()
  31. self.max_size = max_size
  32. self.id_to_pop = set()
  33. self.flag_to_pop = False
  34. def get_state(self):
  35. return self.flag_to_pop
  36. def update(self, kpt_res, mot_res):
  37. kpts = kpt_res.get('keypoint')[0]
  38. bboxes = kpt_res.get('bbox')
  39. mot_bboxes = mot_res.get('boxes')
  40. updated_id = set()
  41. for idx in range(len(kpts)):
  42. tracker_id = mot_bboxes[idx, 0]
  43. updated_id.add(tracker_id)
  44. kpt_seq = self.keypoint_saver.get(tracker_id,
  45. KeyPointSequence(self.max_size))
  46. is_full = kpt_seq.save(kpts[idx], bboxes[idx])
  47. self.keypoint_saver[tracker_id] = kpt_seq
  48. #Scene1: result should be popped when frames meet max size
  49. if is_full:
  50. self.id_to_pop.add(tracker_id)
  51. self.flag_to_pop = True
  52. #Scene2: result of a lost tracker should be popped
  53. interrupted_id = set(self.keypoint_saver.keys()) - updated_id
  54. if len(interrupted_id) > 0:
  55. self.flag_to_pop = True
  56. self.id_to_pop.update(interrupted_id)
  57. def get_collected_keypoint(self):
  58. """
  59. Output (List): List of keypoint results for Skeletonbased Recognition task, where
  60. the format of each element is [tracker_id, KeyPointSequence of tracker_id]
  61. """
  62. output = []
  63. for tracker_id in self.id_to_pop:
  64. output.append([tracker_id, self.keypoint_saver[tracker_id]])
  65. del (self.keypoint_saver[tracker_id])
  66. self.flag_to_pop = False
  67. self.id_to_pop.clear()
  68. return output
  69. class ActionVisualHelper(object):
  70. def __init__(self, frame_life=20):
  71. self.frame_life = frame_life
  72. self.action_history = {}
  73. def get_visualize_ids(self):
  74. id_detected = self.check_detected()
  75. return id_detected
  76. def check_detected(self):
  77. id_detected = set()
  78. deperate_id = []
  79. for mot_id in self.action_history:
  80. self.action_history[mot_id]["life_remain"] -= 1
  81. if int(self.action_history[mot_id]["class"]) == 0:
  82. id_detected.add(mot_id)
  83. if self.action_history[mot_id]["life_remain"] == 0:
  84. deperate_id.append(mot_id)
  85. for mot_id in deperate_id:
  86. del (self.action_history[mot_id])
  87. return id_detected
  88. def update(self, action_res_list):
  89. for mot_id, action_res in action_res_list:
  90. if mot_id in self.action_history:
  91. if int(action_res["class"]) != 0 and int(self.action_history[
  92. mot_id]["class"]) == 0:
  93. continue
  94. action_info = self.action_history.get(mot_id, {})
  95. action_info["class"] = action_res["class"]
  96. action_info["life_remain"] = self.frame_life
  97. self.action_history[mot_id] = action_info