This code demonstrates the use of the SelfiSegmentation module from the cvzone library to remove the background from a video stream in real-time.
- Python 3
- OpenCV
- cvzone
- Clone the repository and navigate to the directory containing the code.
- Install the required libraries using pip install -r requirements.txt.
- Place the background images you want to use in the images/ directory. The images should be in .jpg format.
- Run the script using python background_removal.py.
- Press 'n' to switch to the next background image and 'b' to switch to the previous one.
- Press 'q' to exit the program.
- The cv2.VideoCapture() function is used to create a video capture object that captures frames from the default camera.
- The SelfiSegmentation class is used to create an instance of the SelfiSegmentation module, which is used to remove the background from the input frames.
- The cvzone.FPS() class is used to create an instance of the FPS module, which is used to measure the frames per second of the output video.
- The background images are loaded from the images/ directory using the os library and stored in a list.
- A while loop is used to capture frames from the video stream, process them using the SelfiSegmentation module, and display the output.
- The cvzone.stackImages() function is used to stack the input and output frames vertically, and the cvzone.FPS().update() function is used to update the FPS counter.
- The program responds to the user pressing 'n' or 'b' to switch between background images and 'q' to exit the program.
- Finally, the video capture object is released and the windows are closed.