video_reader.py 1.4 KB

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