video_reader.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 ...utils.benchmark import benchmark
  15. from ...utils.io import VideoReader
  16. @benchmark.timeit_with_options(name=None, is_read_operation=True)
  17. class ReadVideo:
  18. """Load video from the file."""
  19. def __init__(self, backend="opencv", num_seg=8, seg_len=1, sample_type=None):
  20. super().__init__()
  21. self._video_reader = VideoReader(
  22. backend=backend, num_seg=num_seg, seg_len=seg_len, sample_type=sample_type
  23. )
  24. def __call__(self, videos):
  25. """apply"""
  26. return [self._read(video) for video in videos]
  27. def _read(self, file_path):
  28. return self._read_video(file_path)
  29. def _read_video(self, video_path):
  30. blob = list(self._video_reader.read(video_path))
  31. if blob is None:
  32. raise Exception("Video read Error")
  33. return blob