predictor.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 Any, Union, Dict, List, Tuple
  15. from ....utils.func_register import FuncRegister
  16. from ....modules.video_detection.model_list import MODELS
  17. from ...common.batch_sampler import VideoBatchSampler
  18. from ...common.reader import ReadVideo
  19. from ..common import (
  20. ToBatch,
  21. StaticInfer,
  22. )
  23. from ..base import BasicPredictor
  24. from .processors import ResizeVideo, Image2Array, NormalizeVideo, DetVideoPostProcess
  25. from .result import DetVideoResult
  26. class VideoDetPredictor(BasicPredictor):
  27. entities = MODELS
  28. _FUNC_MAP = {}
  29. register = FuncRegister(_FUNC_MAP)
  30. def __init__(self, topk: Union[int, None] = None, *args, **kwargs):
  31. super().__init__(*args, **kwargs)
  32. self.pre_tfs, self.infer, self.post_op = self._build()
  33. def _build_batch_sampler(self):
  34. return VideoBatchSampler()
  35. def _get_result_class(self):
  36. return DetVideoResult
  37. def _build(self):
  38. pre_tfs = {}
  39. for cfg in self.config["PreProcess"]["transform_ops"]:
  40. tf_key = list(cfg.keys())[0]
  41. assert tf_key in self._FUNC_MAP
  42. func = self._FUNC_MAP[tf_key]
  43. args = cfg.get(tf_key, {})
  44. name, op = func(self, **args) if args else func(self)
  45. if op:
  46. pre_tfs[name] = op
  47. infer = StaticInfer(
  48. model_dir=self.model_dir,
  49. model_prefix=self.MODEL_FILE_PREFIX,
  50. option=self.pp_option,
  51. )
  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(self, batch_data):
  65. batch_raw_videos = self.pre_tfs["ReadVideo"](videos=batch_data)
  66. batch_videos = self.pre_tfs["ResizeVideo"](videos=batch_raw_videos)
  67. batch_videos = self.pre_tfs["Image2Array"](videos=batch_videos)
  68. x = self.pre_tfs["NormalizeVideo"](videos=batch_videos)
  69. num_seg = len(x[0])
  70. pred_seg = []
  71. for i in range(num_seg):
  72. batch_preds = self.infer(x=[x[0][i]])
  73. pred_seg.append(batch_preds)
  74. batch_bboxes = self.post_op["DetVideoPostProcess"](preds=[pred_seg])
  75. return {
  76. "input_path": batch_data,
  77. "result": batch_bboxes,
  78. }
  79. @register("ReadVideo")
  80. def build_readvideo(self, num_seg=8):
  81. return "ReadVideo", ReadVideo(backend="opencv", num_seg=num_seg)
  82. @register("ResizeVideo")
  83. def build_resize(self, target_size=224):
  84. return "ResizeVideo", ResizeVideo(
  85. target_size=target_size,
  86. )
  87. @register("Image2Array")
  88. def build_image2array(self, data_format="tchw"):
  89. return "Image2Array", Image2Array(data_format="tchw")
  90. @register("NormalizeVideo")
  91. def build_normalize(
  92. self,
  93. scale=255.0,
  94. ):
  95. return "NormalizeVideo", NormalizeVideo(scale=scale)
  96. @register("DetVideoPostProcess")
  97. def build_postprocess(self, nms_thresh=0.5, score_thresh=0.4, label_list=[]):
  98. return "DetVideoPostProcess", DetVideoPostProcess(
  99. nms_thresh=nms_thresh, score_thresh=score_thresh, label_list=label_list
  100. )