Skip to content

Webcambackground #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions webcam_remove_background/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# WebCam Remove Background With openCV
use pretrained model segmentation

## ![image1](src/1.jpg)
48 changes: 48 additions & 0 deletions webcam_remove_background/Webcam_remove_background.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# @OWNER2PLUSAI

import os
import cv2 as cv
import numpy as np
import mediapipe as mp

# find images
image_path = "images"
images = os.listdir(image_path)
image_index = 0
bg_image = cv.imread(image_path +"/" +images[image_index])

# pretrained model for human detection
mp_selfie_segmentation = mp.solutions.selfie_segmentation
selfie_segmentation = mp_selfie_segmentation.SelfieSegmentation(model_selection=1) # use segmentation

cap = cv.VideoCapture("Video.mp4") # for ur webcam use 0 , 1
while cap.isOpened():
_ ,frame = cap.read()
frame = cv.flip(frame,1)
height ,width ,channle = frame.shape
RGB = cv.cvtColor(frame ,cv.COLOR_BGR2RGB)
results = selfie_segmentation.process(RGB)
mask = results.segmentation_mask

condition = np.stack(
(results.segmentation_mask,) *3 ,axis=-1) > 0.5

bg_image = cv.resize(bg_image, (width,height))
output_image = np.where(condition,frame ,bg_image)

cv.imshow("Webcam",output_image)
cv.imshow("real",frame)

key = cv.waitKey(15) # slow framerate
if key == ord("q"): # press q for exit
break
elif key == ord("d"): # press d for change background
if image_index != len(images)-1:
image_index += 1
else:
image_index = 0

bg_image = cv.imread(image_path +"/" +images[image_index])



Binary file added webcam_remove_background/images/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webcam_remove_background/images/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webcam_remove_background/images/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webcam_remove_background/images/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webcam_remove_background/src/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.