|
| 1 | +# @OWNER2PLUSAI |
| 2 | + |
| 3 | +import os |
| 4 | +import cv2 as cv |
| 5 | +import numpy as np |
| 6 | +import mediapipe as mp |
| 7 | + |
| 8 | +# find images |
| 9 | +image_path = "images" |
| 10 | +images = os.listdir(image_path) |
| 11 | +image_index = 0 |
| 12 | +bg_image = cv.imread(image_path +"/" +images[image_index]) |
| 13 | + |
| 14 | +# pretrained model for human detection |
| 15 | +mp_selfie_segmentation = mp.solutions.selfie_segmentation |
| 16 | +selfie_segmentation = mp_selfie_segmentation.SelfieSegmentation(model_selection=1) # use segmentation |
| 17 | + |
| 18 | +cap = cv.VideoCapture("Video.mp4") # for ur webcam use 0 , 1 |
| 19 | +while cap.isOpened(): |
| 20 | + _ ,frame = cap.read() |
| 21 | + frame = cv.flip(frame,1) |
| 22 | + height ,width ,channle = frame.shape |
| 23 | + RGB = cv.cvtColor(frame ,cv.COLOR_BGR2RGB) |
| 24 | + results = selfie_segmentation.process(RGB) |
| 25 | + mask = results.segmentation_mask |
| 26 | + |
| 27 | + condition = np.stack( |
| 28 | + (results.segmentation_mask,) *3 ,axis=-1) > 0.5 |
| 29 | + |
| 30 | + bg_image = cv.resize(bg_image, (width,height)) |
| 31 | + output_image = np.where(condition,frame ,bg_image) |
| 32 | + |
| 33 | + cv.imshow("Webcam",output_image) |
| 34 | + cv.imshow("real",frame) |
| 35 | + |
| 36 | + key = cv.waitKey(15) # slow framerate |
| 37 | + if key == ord("q"): # press q for exit |
| 38 | + break |
| 39 | + elif key == ord("d"): # press d for change background |
| 40 | + if image_index != len(images)-1: |
| 41 | + image_index += 1 |
| 42 | + else: |
| 43 | + image_index = 0 |
| 44 | + |
| 45 | + bg_image = cv.imread(image_path +"/" +images[image_index]) |
| 46 | + |
| 47 | + |
| 48 | + |
0 commit comments