-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselfieSegmenterModule.py
62 lines (48 loc) · 2.15 KB
/
selfieSegmenterModule.py
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
61
import cv2 as cv
import numpy as np
import mediapipe as mp
class Selfie_segmentation():
def __init__(self,model = 1): # model represents the technique General(0) & Landscape(1)
self.model = model
self.mpDraw = mp.solutions.drawing_utils
# Calling the selfie_segmentation Ml model
self.mp_selfie_Segmentation = mp.solutions.selfie_segmentation
# Setting parameters Ml model
self.Selfie_Segmentation = self.mp_selfie_Segmentation.SelfieSegmentation(self.model)
""""
Frame = (Input Frame)
BGimg = (Background-Frame) Default = (WHITE)
threshold = (Ml-model parameter)
"""
def Bg_Remover(self, frame, BGimg = (255,255,255), threshold = 0.1, blur=(7,7), invisible = False, frametemp = None):
'Convert color and flip frame'
frame = cv.cvtColor( cv.flip(frame,1), cv.COLOR_BGR2RGB)
'Improve performance: frame as not writeable'
frame.flags.writeable = False
'Pass through Ml-model'
results = self.Selfie_Segmentation.process(frame)
'frame as Writeable'
frame.flags.writeable = True
frame = cv.cvtColor(frame, cv.COLOR_RGB2BGR)
condition = np.stack((results.segmentation_mask, ) * 3, axis=-1) > threshold
'BGimg passed as color(tuple) => (255,255,255)'
if isinstance(BGimg, tuple):
bg_frame = np.zeros(frame.shape, dtype=np.uint8)
bg_frame[:] = BGimg
output_frame = np.where(condition, frame, bg_frame)
return output_frame
# Make yourself invisible
elif invisible == True:
try:
frametemp = cv.GaussianBlur(cv.flip(frametemp,1),blur,0)
BGimg = frame
output_frame = np.where(condition, frametemp, BGimg)
except:
print("No Background Frame Given\n"
"pass a still backgrounds i.e first frame, \n "
"your body will be replaced with this frame")
# BGimg as a image
else:
BGimg = cv.GaussianBlur(BGimg,blur,0)
output_frame = np.where(condition, frame, BGimg)
return output_frame