• 基于 MediaPipe 的图像去背


    环境

    • windows 10 64bit

    • mediapipe 0.8.11

    简介

    本篇介绍另一个图片、视频和摄像头图像背景去除的开源项目,该项目基于 mediapipe 机器学习框架,主要封装了 FaceDetectionSelfieSegmentation,除此之外,还提供了像人脸检测和图像素描化的实例,很有参考价值。

    安装

    第一步去拉取源码

    1. git clone https://github.com/pythonlessons/background_removal.git
    2. cd background_removal

    安装所有依赖

    pip install -r requirements.txt

    如果机器中有 nvidiagpu,就安装 onnxruntime-gpu 来代替 onnxruntime

    测试

    三个主要功能,我们一一来看

    去背换背

    准备一张图片,来看看去除背景、替换背景的功能

    08b28ad0818696275f8946b07502901d.jpeg

    1. from utils import FPSmetric
    2. from selfieSegmentation import MPSegmentation
    3. from engine import Engine
    4. if __name__ == '__main__':
    5.     fpsMetric = FPSmetric()
    6.     segmentationModule = MPSegmentation(threshold=0.3, bg_images_path='', bg_blur_ratio=(4545))
    7.     selfieSegmentation = Engine(image_path='data/lijiaxin.jpg', show=True, custom_objects=[segmentationModule,])
    8.     selfieSegmentation.run()

    c54aff9031ccf438346376374be55c20.png

    其中,MPSegmentation 中的参数 bg_blur_ratio 对应的是 cv2.GaussianBlur 中的 ksize,值越大,图像越模糊,下图是 bg_blur_ratio 为 (89,89) 的效果,这个值必须是奇数

    de17333cc154b7866b8f4c5acc3b8206.png

    如果需要指定背景的话,可以在参数 bg_images_path 中指定背景所在的文件夹

    e025a53be5e6eaba1729b3ccd4e9b233.png

    38430f16a3e0084971c0652a686505c2.png

    通过参数 video_path 来指定待处理的视频文件

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

    通过参数 webcam_id 来使用特定的摄像头,多个摄像头通过不同的 id 来区分

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

    作者封装了一个 fps 相关的类 FPSmetric,需要使用的话,在 custom_objects 列表中加入

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

    6603bd20d472854e88de577738dd64d5.png

    人脸检测

    MPFaceDetection 封装的是 mediapipe 中的 FaceDetection,使用它可以进行人脸检测

    1. from utils import FPSmetric
    2. from faceDetection import MPFaceDetection
    3. from engine import Engine
    4. if __name__ == '__main__':
    5.     fpsMetric = FPSmetric()
    6.     mpFaceDetector = MPFaceDetection()
    7.     selfieSegmentation = Engine(video_path='test.mp4', show=True, custom_objects=[mpFaceDetector, fpsMetric])
    8.     selfieSegmentation.run()

    821d93f871de9ad9e15dd8d85caeaa72.png

    图像素描化

    作者利用 opencv 实现了一个简单的图像素描化的效果,对应的类是 PencilSketch,其大致的工作流程如下

    • 灰度化图像

    • 颜色倒置,即 255 - 灰度值

    • 使用 cv2.GaussianBlur 进行模糊化处理

    • 在模糊化和灰度图上应用颜色减淡混合模式(Colour Dodge blending mode)

    PencilSketch 类的使用跟上面提到的 FPSmetric 类似

    1. from pencilSketch import PencilSketch
    2. from engine import Engine
    3. if __name__ == '__main__':
    4.     pencilSketch = PencilSketch(blur_simga=5)
    5.     selfieSegmentation = Engine(image_path='data/lijiaxin.jpg', show=True, custom_objects=[pencilSketch])
    6.     selfieSegmentation.run()

    713c4c9ae2e9596b7289a96815611f21.jpeg

    视频文件和摄像头数据的处理跟上面去背示例的用法是一样,就不再赘述了。

    参考资料

    • backgroundremover去背

    • rembg去背

    • BackgroundMattingV2去背

  • 相关阅读:
    Pycharm 安装配置 pyQt5 图文操作(全)
    机器学习笔记之隐马尔可夫模型(五)学习问题——EM算法
    requests爬虫IP连接初始化问题及解决方案
    最短路径——通过Dynamo批量创建行进路线
    BERT之后,NLP主要预训练模型演变梳理
    MySQL安装
    揭秘如今市场上最火爆的三大商业模式,点进来看看有没有什么收获
    java计算机毕业设计web家教管理系统源码+mysql数据库+系统+lw文档+部署
    智领未来,安全无忧:智能网联汽车监控大屏的守护之旅
    SpringBoot系列之动态定时程序改进版
  • 原文地址:https://blog.csdn.net/djstavaV/article/details/127385215