Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import cv2
  2. import numpy
  3.  
  4.  
  5. class VideoOperator(cv2.VideoCapture):
  6. def __init__(self, video_path):
  7. super().__init__(video_path)
  8. self.current_frame_index = 0
  9. self.last_frame_index = int(self.get(cv2.CAP_PROP_FRAME_COUNT) - 1)
  10.  
  11. def move_to_frame(self, frame_index):
  12. if frame_index > self.last_frame_index:
  13. self.set(cv2.CAP_PROP_POS_FRAMES, self.last_frame_index)
  14. self.current_frame_index = self.last_frame_index
  15. elif frame_index < 0:
  16. self.set(cv2.CAP_PROP_POS_FRAMES, 0)
  17. self.current_frame_index = 0
  18. else:
  19. print(type(self))
  20. self.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
  21. self.current_frame_index = frame_index
  22.  
  23. def set_frame_position(self, frame_index):
  24. self.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
  25. self.current_frame_index = frame_index
  26.  
  27. def move_x_frames(self, x):
  28. self.move_to_frame(self.current_frame_index + x)
  29.  
  30. def get_current_frame(self):
  31. _, image = self.read()
  32. self.move_to_frame(self.current_frame_index)
  33. return image
  34.  
  35.  
  36. if __name__ == "__main__":
  37. for i in range(5):
  38. vc = VideoOperator('vid1.mp4')
  39. vc.move_to_frame(vc.last_frame_index)
  40. vc.move_x_frames(60)
  41. # print(vc.current_frame_index)
  42. # vc.move_x_frames(20)
  43. # print(vc.current_frame_index)
  44. # success,image = vc.read()
  45. # vc.move_to_frame(0)
  46. # success2,image2 = vc.read()
  47. # print(numpy.array_equal(image2, image))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement