Skip to content

Commit fa0f20c

Browse files
authored
new project remove background for webcam with readme (#1)
* add remove background for webcam * add readme for remove background
1 parent 55e4359 commit fa0f20c

File tree

7 files changed

+52
-0
lines changed

7 files changed

+52
-0
lines changed

webcam_remove_background/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# WebCam Remove Background With openCV
2+
use pretrained model segmentation
3+
4+
## ![image1](src/1.jpg)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+

webcam_remove_background/images/1.jpg

251 KB
Loading

webcam_remove_background/images/2.jpg

361 KB
Loading

webcam_remove_background/images/3.jpg

271 KB
Loading

webcam_remove_background/images/4.jpg

2.64 MB
Loading

webcam_remove_background/src/1.png

2.52 MB
Loading

0 commit comments

Comments
 (0)