Guest User

Untitled

a guest
Jan 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. import cv2
  4. import os
  5. import sys
  6.  
  7. # Get the Current Dir
  8. root = os.getcwd()
  9.  
  10. # Defaults Params
  11. videoFile = None
  12. image_size = None
  13.  
  14. frame_step = 10
  15. destination_dir = 'result'
  16. destination_format = 'jpg'
  17.  
  18.  
  19. # Extract the Data
  20. ####################
  21.  
  22. def readFrames():
  23. global videoFile, image_size, destination_dir, frame_step, destination_format
  24.  
  25. directory = os.path.join(root, destination_dir)
  26. if not os.path.exists(directory):
  27. os.makedirs(directory)
  28.  
  29. image_counter = 0
  30. read_counter = 0
  31.  
  32. print('Read file: {}'.format(videoFile))
  33. cap = cv2.VideoCapture(videoFile) # says we capture an image from a webcam
  34.  
  35. while(cap.isOpened()):
  36. ret,cv2_im = cap.read()
  37. if ret and read_counter % frame_step == 0:
  38.  
  39. converted = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
  40.  
  41. pil_im = Image.fromarray(converted)
  42.  
  43. if image_size:
  44. pil_im_resize = pil_im.resize(image_size)
  45. else:
  46. pil_im_resize = pil_im
  47.  
  48. pil_im_resize.save(os.path.join(root, destination_dir, str(image_counter) + '.' + destination_format))
  49. image_counter += 1
  50. elif not ret:
  51. break
  52. read_counter += 1
  53.  
  54. cap.release()
  55.  
  56. def parseArgs(args):
  57. global videoFile, image_size, destination_dir, frame_step, destination_format
  58. i = 1
  59. while i < len(args):
  60. if args[i] == '-f':
  61. # Input file Name
  62. videoFile = args[i+1]
  63. i += 2
  64. elif args[i] == '-s':
  65. # Image Output Size
  66. # Per default the original one
  67. image_size = (args[i+1], args[i+2])
  68. i += 3
  69. elif args[i] == '-o':
  70. # Output dir
  71. destination_dir = args[i+1]
  72. i += 2
  73. elif args[i] == '-steps':
  74. # Number of frames to take into account
  75. # 1 = every frame
  76. # 10 = every 10 frames
  77. frame_step = args[i+1]
  78. i += 2
  79. elif args[i] == '-image_format':
  80. # Basically PNG or JPG
  81. destination_format = args[i+1]
  82. i += 2
  83.  
  84.  
  85.  
  86. def main(argv):
  87. parseArgs(argv)
  88. readFrames()
  89. pass
  90.  
  91. if __name__ == "__main__":
  92. main(sys.argv)
Add Comment
Please, Sign In to add comment