app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import cv2 import mediapipe as mp import numpy as np # Initialize MediaPipe Selfie Segmentation mp_selfie_segmentation = mp.solutions.selfie_segmentation segment = mp_selfie_segmentation.SelfieSegmentation(model_selection=1) # Load image image = cv2.imread("bg.jpg") rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = segment.process(rgb) # Create binary mask mask = results.segmentation_mask > 0.5 # Blur the entire image blurred = cv2.GaussianBlur(image, (101, 101), 0) # Combine blurred background with original subject output_image = np.where(mask[:, :, None], image, blurred) # Save result cv2.imwrite("blurred_background.jpg", output_image) print("Image saved with Instagram-style blurred background.") |