当前位置:   article > 正文

python通过迭代器读取视频帧_self.cap.read()

self.cap.read()

代码:

  1. class VideoIter(object):
  2. def __init__(self, video_name):
  3. self.cap = cv2.VideoCapture(video_name)
  4. self.size = [int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
  5. int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))]
  6. self.fps = self.cap.get(cv2.CAP_PROP_FPS)
  7. assert self.cap.isOpened(), FileNotFoundError
  8. self.num_frame = self.cap.get(cv2.CAP_PROP_FRAME_COUNT)
  9. self.index = -1
  10. def __iter__(self):
  11. return self
  12. def __next__(self):
  13. # while True:
  14. if self.index < self.num_frame:
  15. ret, frame = self.cap.read()
  16. if ret:
  17. self.index += 1
  18. return frame
  19. else:
  20. raise ValueError
  21. else:
  22. raise StopIteration
  23. video_iter = VideoIter(os.path.join(video_path))
  24. size = video_i.size
  25. num_frame = video_iter.num_frame
  26. fps = video_iter.fps
  27. start_frame, end_frame = scene[0], scene[1]
  28. frame_data = []
  29. while video_iter.index < end_frame and video_iter.index < num_frame:
  30. frame = next(video_iter)
  31. frame_data.append(frame)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/775907
推荐阅读
相关标签
  

闽ICP备14008679号