Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. import moviepy.editor as mpy
  4. import argparse
  5. import logging
  6. from PIL import Image
  7. import pdb
  8.  
  9. import cv2
  10. def capture_opencv():
  11. VIDEO_PREFIX = "/path/to/video/video{}.mp4"
  12. for vid in range(0, 1000):
  13. vid_dir = "TrainValFrame/video{}".format(vid)
  14. if not os.path.exists(vid_dir):
  15. os.mkdir(vid_dir)
  16.  
  17. cap = cv2.VideoCapture(VIDEO_PREFIX.format(vid))
  18. ret = True
  19. frame_num = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
  20. for frame_id in range(int(frame_num)):
  21. ret, frame = cap.read(frame_id)
  22. cv2.imwrite("TrainValFrame/video{}/frame{}.jpg".format(vid, frame_id), frame)
  23. if ret == False:
  24. break
  25.  
  26.  
  27. def sample_capture(stat_file, video_path, frame_path, start_id, end_id, frames_per_video):
  28. """
  29. Capture frames from video.
  30.  
  31. frames_per_video: The amount of frames captured from one video.
  32. """
  33. frame_amount_stat = open(stat_file, 'a')
  34.  
  35. for video_id in range(start_id, end_id):
  36. video_filename = 'video{}.mp4'.format(video_id)
  37. clip = mpy.VideoFileClip(os.path.join(video_path, video_filename)
  38.  
  39. frame_amount = int(clip.fps * clip.duration)
  40. frame_amount_stat.write(video_filename + ',{},{},{}\n'.format(frame_amount, clip.w, clip.h))
  41.  
  42. frame_dir = os.path.join(frame_path, 'video{:04d}/'.format(video_id))
  43. if not os.path.exists(frame_dir):
  44. os.mkdir(frame_dir) # video7010
  45.  
  46. gap = clip.duration / frames_per_video
  47. for i in range(frames_per_video):
  48. # t is time in second.
  49. clip.save_frame(frame_dir + '{}.jpg'.format(i), t=(i * gap))
  50. print('Video{}: Done!'.format(video_id))
  51.  
  52. # frame_amount_stat.close()
  53.  
  54. def show_statistic_info(split_name, stat_file):
  55. with open(stat_file) as f:
  56. lines = f.readlines()
  57.  
  58. # Get the minimum and maximum frame amount, width and height
  59. video_amount = 0
  60. total_frames = 0
  61. total_width = 0
  62. total_height = 0
  63. min_frames, max_frames = 10000, 0
  64. min_w, max_w = 10000, 0
  65. min_h, max_h = 10000, 0
  66.  
  67. for line in lines[1:]:
  68. video_amount += 1
  69. cur_frames = int(line.split(',')[1])
  70. total_frames += cur_frames
  71. cur_w = int(line.split(',')[2])
  72. total_width += cur_w
  73. cur_h = int(line.split(',')[3])
  74. total_height += cur_h
  75.  
  76. if cur_frames < min_frames:
  77. min_frames = cur_frames
  78. if cur_frames > max_frames:
  79. max_frames = cur_frames
  80.  
  81. if cur_w < min_w:
  82. min_w = cur_w
  83. if cur_w > max_w:
  84. max_w = cur_w
  85.  
  86. if cur_h < min_h:
  87. min_h = cur_h
  88. if cur_h > max_h:
  89. max_h = cur_h
  90.  
  91. print("Minimum frames of {} set is: {}".format(split_name, min_frames))
  92. print("Maximum frames of {} set is: {}".format(split_name, max_frames))
  93. print("Average frames amount of {} set is: {}".format(split_name, total_frames/ video_amount))
  94.  
  95. print("Minimum width of {} set is: {}".format(split_name, min_w))
  96. print("Maximum width of {} set is: {}".format(split_name, max_w))
  97. print("Average width of {} set is: {}".format(split_name, total_width / video_amount))
  98.  
  99. print("Minimum height of {} set is: {}".format(split_name, min_h))
  100. print("Maximum height of {} set is: {}".format(split_name, max_h))
  101. print("Average height of {} set is: {}".format(split_name, total_height / video_amount))
  102.  
  103.  
  104. def capture(video_path, start_id, end_id, frame_path):
  105. for video_id in range(start_id, end_id):
  106. video_filename = 'video{}.mp4'.format(video_id)
  107. clip = mpy.VideoFileClip(os.path.join(video_path, video_filename))
  108.  
  109. frame_dir = os.path.join(frame_path, 'video{:04d}/'.format(video_id))
  110. if not os.path.exists(frame_dir):
  111. os.mkdir(frame_dir) # example: video7010
  112.  
  113. i = 0
  114. for frame in clip.iter_frames(dtype="uint8"):
  115. frame = Image.fromarray(frame)
  116. frame.save("{}{:04d}.jpg".format(frame_dir, i))
  117. i += 1
  118.  
  119. if video_id % 100 == 0:
  120. print("Current Video: ", video_id)
  121.  
  122.  
  123. if __name__ == "__main__":
  124. parser = argparse.ArgumentParser()
  125.  
  126. parser.add_argument("--action", type=str)
  127. parser.add_argument("--video_root", type=str)
  128. parser.add_argument("--frame_root", type=str)
  129. parser.add_argument("--start_video", type=int)
  130. parser.add_argument("--end_video", type=int)
  131.  
  132. parser.add_argument("--stat_file", type=str, help="Path to the statistic file.")
  133. parser.add_argument("--frames_per_video", type=int, help="If sample, the amount of frames for one video.")
  134.  
  135. args = parser.parse_args()
  136.  
  137. if args.action == "sample_capture":
  138. sample_capture(stat_file=args.stat_file,
  139. video_path=args.video_root,
  140. frame_path=args.frame_root,
  141. start_id=args.start_video,
  142. end_id=args.end_video,
  143. frames_per_video=args.frames_per_video)
  144. if args.action == "show_statistic_info":
  145. show_statistic_info('train', 'statistic_train.txt')
Add Comment
Please, Sign In to add comment