environment
windows 10 64bit
mediapipe 0.8.11
Introduction
This article introduces another open source project for background removal of pictures, videos and camera images. This project is based on mediapipe
the machine learning framework and mainly encapsulates FaceDetection
and SelfieSegmentation
. In addition, it also provides examples such as face detection and image sketching, which are very useful. reference value.
Install
The first step is to pull the source code
git clone https://github.com/pythonlessons/background_removal.git
cd background_removal
Install all dependencies
pip install -r requirements.txt
If there nvidia
is gpu
, install onnxruntime-gpu
insteadonnxruntime
test
Three main functions, let's look at them one by one
change back
Prepare a picture to see the function of removing background and replacing background
from utils import FPSmetric
from selfieSegmentation import MPSegmentation
from engine import Engine
if __name__ == '__main__':
fpsMetric = FPSmetric()
segmentationModule = MPSegmentation(threshold=0.3, bg_images_path='', bg_blur_ratio=(45, 45))
selfieSegmentation = Engine(image_path='data/lijiaxin.jpg', show=True, custom_objects=[segmentationModule,])
selfieSegmentation.run()
Among them, MPSegmentation
the parameter bg_blur_ratio
in corresponds that cv2.GaussianBlur
in ksize
, the larger the value, the more blurred the image, the picture below is the effect bg_blur_ratio
of (89,89), this value must be an odd number
If you need to specify the background, you can specify the folder where the background is located bg_images_path
in
video_path
Specify the video file to be processed by parameter
selfieSegmentation = Engine(video_path='data/test.mp4', show=True, custom_objects=[segmentationModule,])
selfieSegmentation.run()
webcam_id
Use a specific camera through the parameter , and multiple cameras id
are distinguished by different
selfieSegmentation = Engine(webcam_id=0, show=True, custom_objects=[segmentationModule,])
selfieSegmentation.run()
The author encapsulates a fps
related class FPSmetric
, if you need to use it, add it to custom_objects
the list
fpsMetric = FPSmetric()
selfieSegmentation = Engine(video_path='test.mp4', show=True, custom_objects=[segmentationModule, fpsMetric])
Face Detection
MPFaceDetection
The package is mediapipe
in FaceDetection
, which can be used for face detection
from utils import FPSmetric
from faceDetection import MPFaceDetection
from engine import Engine
if __name__ == '__main__':
fpsMetric = FPSmetric()
mpFaceDetector = MPFaceDetection()
selfieSegmentation = Engine(video_path='test.mp4', show=True, custom_objects=[mpFaceDetector, fpsMetric])
selfieSegmentation.run()
Image sketching
The author uses opencv
to achieve a simple image sketch effect, the corresponding class is PencilSketch
, and its general workflow is as follows
grayscale image
Colors are inverted, i.e. 255 - grayscale value
cv2.GaussianBlur
Obfuscate usingColour Dodge blending mode
Apply a Color Dodge blending mode ( ) on Blur and Grayscale
PencilSketch
The use of the class FPSmetric
is similar
from pencilSketch import PencilSketch
from engine import Engine
if __name__ == '__main__':
pencilSketch = PencilSketch(blur_simga=5)
selfieSegmentation = Engine(image_path='data/lijiaxin.jpg', show=True, custom_objects=[pencilSketch])
selfieSegmentation.run()
The processing of video files and camera data is the same as the usage of the above example, so I won’t repeat them here.
References
backgroundremover to back
rembg go back
BackgroundMattingV2 to back