Welcome folks today in this blog post we will be seeing on how basically we can remove the background of the live webcam video in python using opencv ,cvzone and medipipe library. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries as shown below
pip install opencv-python
pip install cvzone
pip install mediapipe
After installing these libraries make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 |
import cv2 import cvzone from cvzone.SelfiSegmentationModule import SelfiSegmentation video = cv2.VideoCapture(0) video.set(3,640) video.set(4,480) loopsvar = True segmen = SelfiSegmentation() |
First of all in the above code we are importing all the libraries that we installed and then we are taking the user webcam using opencv and storing it inside the video variable. And then we are setting the width and the height of the webcam to be 640x480
. And then we are storing the segmentation constructor in segmen variable.
1 2 3 4 5 6 7 8 9 10 11 12 |
while loopsvar: _,frame = video.read() cv2.imshow("video",frame) key=cv2.waitKey(1) if key== ord('c'): loopsvar = False elif key == ord('q'): imagechange('firstimage') elif key == ord('w'): imagechange('secondimage') elif key == ord('e'): imagechange('thirdimage') |
Now guys we are running a simple while loop to simply display the live webcam video of the user. We are reading the frames from the video webcam using the opencv read() method and then we are checking which key the user has pressed. If the user has pressed the c
key we are quitting the application by making the loopvar
to false. And then if user has pressed q,w and e keys we are toggling the background of the webcam to custom images that we have stored as shown below
Make sure you resize all the images that is stored to 640x480
which is the size of the webcam video. And now basically we can write the function which will actually change the background of the webcam to a custom image as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def imagechange(image): global loopsvar if image == "firstimage": bgimage = cv2.imread("1.png") elif image == "secondimage": bgimage = cv2.imread("2.png") elif image == "thirdimage": bgimage = cv2.imread('3.png') while loopsvar: check,frame = video.read() videoremovebg = segmen.removeBG(frame,bgimage,threshold=0.8) cv2.imshow('video',videoremovebg) key=cv2.waitKey(1) if key== ord('c'): loopsvar=False elif key == ord('q'): imagechange('firstimage') elif key == ord('w'): imagechange('secondimage') elif key == ord('e'): imagechange('thirdimage') |
Here we are using the removeBg()
method of the cvzone
module to replace the custom image as the background. And then we are using the same logic to check which key is pressed by the user and accordingly we are showing the respective three images. Now if you run the python script you will see the below output
python app.py
Full Source Code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import cv2 import cvzone from cvzone.SelfiSegmentationModule import SelfiSegmentation video = cv2.VideoCapture(0) video.set(3,640) video.set(4,480) loopsvar = True segmen = SelfiSegmentation() def imagechange(image): global loopsvar if image == "firstimage": bgimage = cv2.imread("1.png") elif image == "secondimage": bgimage = cv2.imread("2.png") elif image == "thirdimage": bgimage = cv2.imread('3.png') while loopsvar: check,frame = video.read() videoremovebg = segmen.removeBG(frame,bgimage,threshold=0.8) cv2.imshow('video',videoremovebg) key=cv2.waitKey(1) if key== ord('c'): loopsvar=False elif key == ord('q'): imagechange('firstimage') elif key == ord('w'): imagechange('secondimage') elif key == ord('e'): imagechange('thirdimage') while loopsvar: _,frame = video.read() cv2.imshow("video",frame) key=cv2.waitKey(1) if key== ord('c'): loopsvar = False elif key == ord('q'): imagechange('firstimage') elif key == ord('w'): imagechange('secondimage') elif key == ord('e'): imagechange('thirdimage') |