Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import argparse
  2. import cv2
  3. import os
  4.  
  5. def process_videos(args):
  6. # getting the filename without .extension
  7. basename = os.path.basename(args.video_path)
  8. filename = os.path.splitext(basename)[0]
  9.  
  10. # setting file-type of output image
  11. ext = '.jpg'
  12.  
  13. out_path = os.path.abspath(args.out_dir)
  14. if os.path.isdir(out_path):
  15. file_path = os.path.join(out_path, filename + ext)
  16. else:
  17. file_path = args.out_dir
  18.  
  19. # getting background image
  20. print(out_path)
  21. print(file_path)
  22. cap = cv2.VideoCapture(args.video_path)
  23. background = subtract_foreground(cap, args.show)
  24. cap.release()
  25.  
  26. cv2.imwrite(file_path, background)
  27.  
  28. def subtract_foreground(cap, show=False):
  29. fgbg = cv2.createBackgroundSubtractorMOG2()
  30. bgimg = None
  31. try:
  32.  
  33. while 1:
  34. ret, frame = cap.read()
  35.  
  36. if ret:
  37. fgbg.apply(frame)
  38. bgimg = fgbg.getBackgroundImage()
  39.  
  40. if show:
  41. cv2.namedWindow('img', cv2.WINDOW_NORMAL)
  42. cv2.resizeWindow('img', 800, 600)
  43. cv2.imshow('img', bgimg)
  44. k = cv2.waitKey(1) & 0xff
  45. if k == 27:
  46. break
  47. else:
  48. break
  49. except KeyboardInterrupt:
  50. pass
  51. return bgimg
  52.  
  53.  
  54.  
  55. def main():
  56. parser = argparse.ArgumentParser(
  57. prog='BeesBook foreground subtractor.',
  58. description='This will remove the moving objects of an video and'
  59. 'return an image of the background.'
  60. )
  61.  
  62. parser.add_argument('video_path', help='Path of the video.', type=str)
  63. parser.add_argument('out_dir', help='Path of the output dir.', type=str)
  64. parser.add_argument("--show", help="Shows the current background.", action="store_true")
  65.  
  66. args = parser.parse_args()
  67. process_videos(args)
  68.  
  69. if __name__ == '__main__':
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement