predictor.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (c) 2024 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. from typing import Union
  15. from ....modules.video_detection.model_list import MODELS
  16. from ....utils.func_register import FuncRegister
  17. from ...common.batch_sampler import VideoBatchSampler
  18. from ...common.reader import ReadVideo
  19. from ..base import BasePredictor
  20. from .processors import DetVideoPostProcess, Image2Array, NormalizeVideo, ResizeVideo
  21. from .result import DetVideoResult
  22. class VideoDetPredictor(BasePredictor):
  23. entities = MODELS
  24. _FUNC_MAP = {}
  25. register = FuncRegister(_FUNC_MAP)
  26. def __init__(
  27. self,
  28. nms_thresh: Union[float, None] = None,
  29. score_thresh: Union[float, None] = None,
  30. *args,
  31. **kwargs
  32. ):
  33. super().__init__(*args, **kwargs)
  34. self.nms_thresh = nms_thresh
  35. self.score_thresh = score_thresh
  36. self.pre_tfs, self.infer, self.post_op = self._build()
  37. def _build_batch_sampler(self):
  38. return VideoBatchSampler()
  39. def _get_result_class(self):
  40. return DetVideoResult
  41. def _build(self):
  42. pre_tfs = {}
  43. for cfg in self.config["PreProcess"]["transform_ops"]:
  44. tf_key = list(cfg.keys())[0]
  45. assert tf_key in self._FUNC_MAP
  46. func = self._FUNC_MAP[tf_key]
  47. args = cfg.get(tf_key, {})
  48. name, op = func(self, **args) if args else func(self)
  49. if op:
  50. pre_tfs[name] = op
  51. infer = self.create_static_infer()
  52. post_op = {}
  53. for cfg in self.config["PostProcess"]["transform_ops"]:
  54. tf_key = list(cfg.keys())[0]
  55. assert tf_key in self._FUNC_MAP
  56. func = self._FUNC_MAP[tf_key]
  57. args = cfg.get(tf_key, {})
  58. if tf_key == "DetVideoPostProcess":
  59. args["label_list"] = self.config["label_list"]
  60. name, op = func(self, **args) if args else func(self)
  61. if op:
  62. post_op[name] = op
  63. return pre_tfs, infer, post_op
  64. def process(
  65. self,
  66. batch_data,
  67. nms_thresh: Union[float, None] = None,
  68. score_thresh: Union[float, None] = None,
  69. ):
  70. batch_raw_videos = self.pre_tfs["ReadVideo"](videos=batch_data)
  71. batch_videos = self.pre_tfs["ResizeVideo"](videos=batch_raw_videos)
  72. batch_videos = self.pre_tfs["Image2Array"](videos=batch_videos)
  73. x = self.pre_tfs["NormalizeVideo"](videos=batch_videos)
  74. num_seg = len(x[0])
  75. pred_seg = []
  76. for i in range(num_seg):
  77. batch_preds = self.infer(x=[x[0][i]])
  78. pred_seg.append(batch_preds)
  79. batch_bboxes = self.post_op["DetVideoPostProcess"](
  80. preds=[pred_seg],
  81. nms_thresh=nms_thresh or self.nms_thresh,
  82. score_thresh=score_thresh or self.score_thresh,
  83. )
  84. return {
  85. "input_path": batch_data,
  86. "result": batch_bboxes,
  87. }
  88. @register("ReadVideo")
  89. def build_readvideo(self, num_seg=8):
  90. return "ReadVideo", ReadVideo(backend="opencv", num_seg=num_seg)
  91. @register("ResizeVideo")
  92. def build_resize(self, target_size=224):
  93. return "ResizeVideo", ResizeVideo(
  94. target_size=target_size,
  95. )
  96. @register("Image2Array")
  97. def build_image2array(self, data_format="tchw"):
  98. return "Image2Array", Image2Array(data_format="tchw")
  99. @register("NormalizeVideo")
  100. def build_normalize(
  101. self,
  102. scale=255.0,
  103. ):
  104. return "NormalizeVideo", NormalizeVideo(scale=scale)
  105. @register("DetVideoPostProcess")
  106. def build_postprocess(self, nms_thresh, score_thresh, label_list=[]):
  107. if not self.nms_thresh:
  108. self.nms_thresh = nms_thresh
  109. if not self.score_thresh:
  110. self.score_thresh = score_thresh
  111. return "DetVideoPostProcess", DetVideoPostProcess(label_list=label_list)