Mediapipe portrait segmentation produced by Google, you can change the background of pictures and videos at will

In some video chat software, we can see that the background of many people is particularly beautiful, and we even wonder if we have really come to a certain place. In this issue, we will introduce Mediapipe portrait segmentation (RVM portrait segmentation)

MediaPipe Selfie Segmentation segments prominent characters in a scene. It runs in real time on smartphones and laptops.

Model

In this solution, MediaPipe provides two models: general model and landscape model. Both models are based on MobileNetV3 with modifications for efficiency. Generic models operate on 256x256x3 (HWC) tensors and output 256x256x1 tensors representing segmentation masks. The landscape model is similar to the general model, but operates on 144x256x3 (HWC) tensors. It has fewer FLOPs than the general model and thus runs faster. Note that MediaPipe Selfie Segmentation automatically resizes the input image to the desired tensor dimensions before feeding it into the ML model.

Code

import cv2
import mediapipe as mp
import numpy as np
mp_drawing = mp.solutions.drawing_utils
mp_selfie_segmentation = mp.solutions.selfie_segmentation
# 图片人物抠图:
IMAGE_FILES = []
BG_COLOR = (0, 255, 0) # 背景颜色也可以使用其他的照片,要求与原照片尺寸一致
#bg_image = cv2.imread('6.jpg')
MASK_COLOR = (255, 255, 255) # mask图片颜色
file = '1.jpg'
with mp_selfie_segmentation.SelfieSegmentation(model_selection=0) as selfi

Guess you like

Origin blog.csdn.net/weixin_44782294/article/details/131621315