mat8854

video opencv frame by frame

Apr 30th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import time
  2. import cv2 as cv
  3.  
  4. # video taken from here: https://www.youtube.com/watch?v=uUMRB829j-I
  5. # downloaded with: https://youtube-mp4.download/de/free-converter
  6.  
  7. if __name__ == '__main__':
  8.     print('started')
  9.  
  10.     video_path = 'D:\scenic_drive.mp4'
  11.     cap = cv.VideoCapture(video_path)
  12.     frame_counter = 0
  13.  
  14.     while (True):
  15.         # get frame
  16.         ret, frame = cap.read()
  17.  
  18.         # check for valid frame
  19.         if ret:
  20.             # resize and display
  21.             frame = cv.resize(frame, (0, 0), None, .3, .3)
  22.             cv.imshow('Drive', frame)
  23.             # counter logic
  24.             print(frame_counter)
  25.             frame_counter = frame_counter + 1
  26.         else:
  27.             cap.release()
  28.             cv.destroyAllWindows()
  29.             print('finished')
  30.  
  31.         # key press logic
  32.         wait = True
  33.         while(wait):
  34.             key = (cv.waitKey(1) & 0xFF)
  35.             if key ==  ord('p'):
  36.                 wait = False
  37.             if key == ord('q'):
  38.                 cap.release()
  39.                 cv.destroyAllWindows()
  40.                 print('finished')
Add Comment
Please, Sign In to add comment