-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_removal.py
More file actions
63 lines (35 loc) · 1.19 KB
/
background_removal.py
File metadata and controls
63 lines (35 loc) · 1.19 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import cv2
import numpy as np
import mediapipe as mp
mp_selfie_segmentation = mp.solutions.selfie_segmentation
selfie_segmentation = mp_selfie_segmentation.SelfieSegmentation(model_selection=1)
image_path = 'images'
images = os.listdir(image_path)
image_index= 0
bg_image = cv2.imread(image_path+'/'+images[image_index])
cap = cv2.VideoCapture(0)
while cap.isOpened():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
height , width, channel = frame.shape
RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = selfie_segmentation.process(RGB)
mask = results.segmentation_mask
condition = np.stack(
(results.segmentation_mask,) * 3, axis=-1) > 0.6
bg_image = cv2.resize(bg_image, (width, height))
output_image = np.where(condition, frame, bg_image)
cv2.imshow("Output", output_image)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
elif key == ord('d'):
if image_index != len(images)-1:
image_index += 1
else:
image_index = 0
bg_image = cv2.imread(image_path+'/'+images[image_index])
cap.release()
cv2.destroyAllWindows()