Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import cv2
- import os
- def process_videos(args):
- # getting the filename without .extension
- basename = os.path.basename(args.video_path)
- filename = os.path.splitext(basename)[0]
- # setting file-type of output image
- ext = '.jpg'
- out_path = os.path.abspath(args.out_dir)
- if os.path.isdir(out_path):
- file_path = os.path.join(out_path, filename + ext)
- else:
- file_path = args.out_dir
- # getting background image
- print(out_path)
- print(file_path)
- cap = cv2.VideoCapture(args.video_path)
- background = subtract_foreground(cap, args.show)
- cap.release()
- cv2.imwrite(file_path, background)
- def subtract_foreground(cap, show=False):
- fgbg = cv2.createBackgroundSubtractorMOG2()
- bgimg = None
- try:
- while 1:
- ret, frame = cap.read()
- if ret:
- fgbg.apply(frame)
- bgimg = fgbg.getBackgroundImage()
- if show:
- cv2.namedWindow('img', cv2.WINDOW_NORMAL)
- cv2.resizeWindow('img', 800, 600)
- cv2.imshow('img', bgimg)
- k = cv2.waitKey(1) & 0xff
- if k == 27:
- break
- else:
- break
- except KeyboardInterrupt:
- pass
- return bgimg
- def main():
- parser = argparse.ArgumentParser(
- prog='BeesBook foreground subtractor.',
- description='This will remove the moving objects of an video and'
- 'return an image of the background.'
- )
- parser.add_argument('video_path', help='Path of the video.', type=str)
- parser.add_argument('out_dir', help='Path of the output dir.', type=str)
- parser.add_argument("--show", help="Shows the current background.", action="store_true")
- args = parser.parse_args()
- process_videos(args)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement