Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import cv2
  2. import os
  3.  
  4.  
  5. def extract_frames_from_single_vid(input_vid, output_dir):
  6.  
  7. if not os.path.isdir(output_dir):
  8. os.makedirs(output_dir)
  9.  
  10. vidcap = cv2.VideoCapture(input_vid)
  11.  
  12. success = True
  13.  
  14. count = 0
  15.  
  16. while success:
  17. success, image = vidcap.read()
  18. #print('Read a new frame: ', success)
  19. cv2.imwrite(os.path.join(output_dir, '{:0>5}.jpg'.format(count)), image) # save frame as JPEG file
  20. count += 10
  21.  
  22. print('Converted {} frames successfully'.format(count))
  23.  
  24.  
  25. def extract_frames_from_batch(base_in_dir, extension):
  26.  
  27. for file in os.listdir(base_in_dir):
  28. if file.endswith(".mp4"):
  29. input_vid_file_path = os.path.join(base_in_dir, file)
  30. print("extracting frames for : " + file)
  31.  
  32. filename, file_extension = os.path.splitext(file)
  33. output_dir = os.path.join(base_in_dir, "output/" + filename)
  34.  
  35. extract_frames_from_single_vid(input_vid_file_path, output_dir)
  36.  
  37.  
  38.  
  39.  
  40. if __name__ == '__main__':
  41.  
  42. #####################################################
  43. # Use this to extract frames from single video file #
  44. #####################################################
  45.  
  46. """
  47. base_dir = r'/home/omer/Work/Ford-Scenarios/BM1/Images+BBs/Train-Data/Car_Turning_Right_Train/'
  48. input_vid = os.path.join(base_dir, 'Boston_Massachusetts_USA_15_37-15_41.mp4')
  49. output_dir = os.path.join(base_dir, 'Boston_Massachusetts_USA_15_37-15_41')
  50.  
  51. extract_frames_from_single_vid(input_vid, output_dir)
  52. """
  53.  
  54.  
  55. ##########################################################
  56. # Use this to extract frames from a batch of video files #
  57. ##########################################################
  58.  
  59. base_dir = r'/home/user/Desktop/video_test'
  60. extension = ".MP4"
  61.  
  62. extract_frames_from_batch(base_dir, extension)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement