Image removal based on MediaPipe

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 mediapipethe machine learning framework and mainly encapsulates FaceDetectionand 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 nvidiais gpu, install onnxruntime-gpuinsteadonnxruntime

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

08b28ad0818696275f8946b07502901d.jpeg

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()

c54aff9031ccf438346376374be55c20.png

Among them, MPSegmentationthe parameter bg_blur_ratioin corresponds that cv2.GaussianBlurin ksize, the larger the value, the more blurred the image, the picture below is the effect bg_blur_ratioof (89,89), this value must be an odd number

de17333cc154b7866b8f4c5acc3b8206.png

If you need to specify the background, you can specify the folder where the background is located bg_images_pathin

e025a53be5e6eaba1729b3ccd4e9b233.png

38430f16a3e0084971c0652a686505c2.png

video_pathSpecify the video file to be processed by parameter

selfieSegmentation = Engine(video_path='data/test.mp4', show=True, custom_objects=[segmentationModule,])
    selfieSegmentation.run()

webcam_idUse a specific camera through the parameter , and multiple cameras idare distinguished by different

selfieSegmentation = Engine(webcam_id=0, show=True, custom_objects=[segmentationModule,])
    selfieSegmentation.run()

The author encapsulates a fpsrelated class FPSmetric, if you need to use it, add it to custom_objectsthe list

fpsMetric = FPSmetric()
selfieSegmentation = Engine(video_path='test.mp4', show=True, custom_objects=[segmentationModule, fpsMetric])

6603bd20d472854e88de577738dd64d5.png

Face Detection

MPFaceDetectionThe package is mediapipein 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()

821d93f871de9ad9e15dd8d85caeaa72.png

Image sketching

The author uses opencvto 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.GaussianBlurObfuscate using

  • Colour Dodge blending modeApply a Color Dodge blending mode ( ) on Blur and Grayscale

PencilSketchThe use of the class FPSmetricis 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()

713c4c9ae2e9596b7289a96815611f21.jpeg

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

Guess you like

Origin blog.csdn.net/djstavaV/article/details/127385215