background_subtr_bgslib.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import argparse
  2. import cv2
  3. import pybgs as bgs
  4. def get_bgslib_result(video_to_process):
  5. # create VideoCapture object for further video processing
  6. captured_video = cv2.VideoCapture(video_to_process)
  7. # check video capture status
  8. if not captured_video.isOpened:
  9. print("Unable to open: " + video_to_process)
  10. exit(0)
  11. # instantiate background subtraction
  12. background_subtr_method = bgs.SuBSENSE()
  13. while True:
  14. # read video frames
  15. retval, frame = captured_video.read()
  16. # check whether the frames have been grabbed
  17. if not retval:
  18. break
  19. # resize video frames
  20. frame = cv2.resize(frame, (640, 360))
  21. # pass the frame to the background subtractor
  22. foreground_mask = background_subtr_method.apply(frame)
  23. # obtain the background without foreground mask
  24. img_bgmodel = background_subtr_method.getBackgroundModel()
  25. # show the current frame, foreground mask, subtracted result
  26. cv2.imshow("Initial Frames", frame)
  27. cv2.imshow("Foreground Masks", foreground_mask)
  28. cv2.imshow("Subtraction result", img_bgmodel)
  29. keyboard = cv2.waitKey(10)
  30. if keyboard == 27:
  31. break
  32. if __name__ == "__main__":
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument(
  35. "--input_video",
  36. type=str,
  37. help="Define the full input video path",
  38. default="space_traffic.mp4",
  39. )
  40. # parse script arguments
  41. args = parser.parse_args()
  42. # start BS-pipeline
  43. get_bgslib_result(args.input_video)