text: 
  from PIL import Image
  import cv2
  import time
  import mediapipe as mp
  
  class SelfieSegMP:
      def __init__(self, width=320, height=240):
          self.width = width
          self.height = height
          self.mp_drawing = mp.solutions.drawing_utils
          self.mp_selfie_segmentation = mp.solutions.selfie_segmentation
          self.selfie_segmentation = self.mp_selfie_segmentation.SelfieSegmentation(model_selection=1)
  
      def seg(self, frame):
          image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  
          image.flags.writeable = False
          results = self.selfie_segmentation.process(image)
  
          mask = cv2.resize(results.segmentation_mask, (self.width, self.height), interpolation=cv2.INTER_CUBIC)
          mask = (255 * mask).astype("uint8")
          _, mask = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY)
  
          return mask
  
  if __name__ == "__main__":
      #"""
      width = 320
      height = 240
      seg = SelfieSegMP(width, height)
  
      # Capture video from camera
      cap = cv2.VideoCapture(0)
      cap.set(3, width)
      cap.set(4, height)
  
      # Load and resize the background image
      bgd = cv2.imread('./images/background.jpeg')
      bgd = cv2.resize(bgd, (width, height))
  
      elapsedTime = 0
      count = 0
  
      while cv2.waitKey(1) < 0:
          t1 = time.time()
  
          # Read input frames
          success, frame = cap.read()
          if not success:
             cap.release()
             break
  
          # Get segmentation mask
          mask = seg.seg(frame)
  
          # Merge with background
          fg = cv2.bitwise_or(frame, frame, mask=mask)
          bg = cv2.bitwise_or(bgd, bgd, mask=~mask)
          out = cv2.bitwise_or(fg, bg)
  
          elapsedTime += (time.time() - t1)
          count += 1
          fps = "{:.1f} FPS".format(count / elapsedTime)
  
          # Show output in window
          cv2.putText(out, fps, (10, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (38, 255, 38), 1, cv2.LINE_AA)
          cv2.imshow('Selfie Segmentation', out)
  
      cv2.destroyAllWindows()
      cap.release()
  