├── Readme.md ├── img ├── 1.jpg └── 2.jpg ├── libraries.bat ├── main.py ├── requirements.txt └── result ├── result.gif └── result2.gif /Readme.md: -------------------------------------------------------------------------------- 1 | # Background removal 2 | In this project I am going to learn how to change background using OpenCV Python. 3 | 4 | ## Features 5 | * Can change background in real-time 6 | * Can be used in Zoom, Teams and other 7 | 8 | ## How to install 9 | 1. Clone this repository on your computer 10 | `https://github.com/paveldat/background_removal.git` 11 | 2. Install all the requirements 12 | `run libraries.bat` or 13 | `pip install -r requirements.txt` 14 | 3. Run the program 15 | `python main.py` 16 | 17 | ## Help 18 | You might face issue with webcam not showing and you get errors. 19 | To solve it just change the value in this line (for example to `1`). 20 | `cap = cv2.VideoCapture(0)` 21 | Increment this number until you see your webcam. 22 | 23 | ## Background 24 | All background images could be found in img folder. 25 | Each image must be 640*480 pixels. 26 | 27 | You can change this parametres here: 28 | ``` 29 | cap.set(3, 640) 30 | cap.set(4, 480) 31 | ``` 32 | And after that you can upload images for the background of other sizes indicated in these two lines. 33 | 34 | ## Change background images 35 | You can change background pressing "a" or "d". For exiting the program just press "q". 36 | 37 | ## Result 38 | ![Alt Text](https://github.com/paveldat/background_removal/blob/main/result/result.gif) 39 | 40 | ## Deleting the main video 41 | You can also remove the original video by changing the following lines: 42 | ``` 43 | #imgStacked = cvzone.stackImages([img, imgOut], 2, 1) 44 | #_, imgStacked = fpsReader.update(imgStacked, color=(255, 0, 255)) 45 | ``` 46 | ``` 47 | cv2.imshow("Image", imgOut) 48 | ``` 49 | 50 | ## Result 51 | ![Alt Text](https://github.com/paveldat/background_removal/blob/main/result/result2.gif) -------------------------------------------------------------------------------- /img/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/background_removal/6f5a4634cef85576991adcd509fafb7174a201a1/img/1.jpg -------------------------------------------------------------------------------- /img/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paveldat/background_removal/6f5a4634cef85576991adcd509fafb7174a201a1/img/2.jpg -------------------------------------------------------------------------------- /libraries.bat: -------------------------------------------------------------------------------- 1 | pip install cvzone 2 | pip install opencv-python -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import cvzone 3 | from cvzone.SelfiSegmentationModule import SelfiSegmentation 4 | import os 5 | 6 | cap = cv2.VideoCapture(0) 7 | cap.set(3, 640) 8 | cap.set(4, 480) 9 | cap.set(cv2.CAP_PROP_FPS, 60) 10 | segmentor = SelfiSegmentation() 11 | fpsReader = cvzone.FPS() 12 | 13 | listImg = os.listdir("img") 14 | 15 | imgList = [] 16 | for imgPath in listImg: 17 | img = cv2.imread(f'img/{imgPath}') 18 | imgList.append(img) 19 | 20 | indexImg = 0 21 | 22 | while True: 23 | success, img = cap.read() 24 | img = cv2.flip(img, 1) 25 | 26 | imgOut = segmentor.removeBG(img, imgList[indexImg], threshold=0.85) 27 | 28 | imgStacked = cvzone.stackImages([img, imgOut], 2, 1) 29 | _, imgStacked = fpsReader.update(imgStacked, color=(255, 0, 255)) 30 | 31 | cv2.imshow("Image", imgStacked) 32 | key = cv2.waitKey(1) 33 | if key == ord('a'): 34 | if indexImg>0: 35 | indexImg -= 1 36 | elif key == ord('d'): 37 | if indexImg