自拍分割(SelfieSegmentation)
import cv2
from mediapipe.python.solutions.selfie_segmentation import SelfieSegmentation
import numpy as np
with SelfieSegmentation(
model_selection=1
) as selfie_segmentation:
bg_img = cv2.imread(
'/Users/dx/Documents/Python/PaddleXDemo/opencv/mediapipe/images/bg.jpeg')
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
if success:
img = cv2.cvtColor(cv2.flip(img, 1), cv2.COLOR_BGR2RGB)
img.flags.writeable = False
results = selfie_segmentation.process(img)
img.flags.writeable = True
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
condition = np.stack(
(results.segmentation_mask,) * 3, axis=-1) > 0.1
if bg_img.shape[:1] != img.shape[:1]:
bg_img = cv2.resize(bg_img, (img.shape[1], img.shape[0]))
output_image = np.where(condition, img, bg_img)
cv2.imshow('自拍抠图', output_image)
if cv2.waitKey(1) > 0:
break
cap.release()
功能