1
+ import cv2
2
+ import mediapipe as mp
3
+ import numpy as np
4
+ from eggdriver .resources .video .effects import *
5
+ from eggdriver .resources .video .backgrounds import *
6
+
7
+ counterIterator = 0
8
+
9
+ def forever ():
10
+ return True
11
+
12
+ def stop ():
13
+ return input ()
14
+
15
+ def itself (var ):
16
+ return var
17
+
18
+ def count (value = 100 , step = 1 ):
19
+ global counterIterator
20
+ counterIterator += step
21
+ print (counterIterator )
22
+ result = counterIterator < value
23
+ return counterIterator < value
24
+
25
+ defaultEffect = [(itself , [])]
26
+ defaultCondition = (count , [100 ])
27
+ defaultUser = 'WEBCAM'
28
+
29
+ def changeBackground (image , bg_image , background_effects = defaultEffect ):
30
+ th , th_inv = removeBG (image )
31
+ bg = maskImage (bg_image , th_inv )
32
+ for ef in background_effects :
33
+ args = [bg ] + ef [1 ]
34
+ bg = ef [0 ](* args )
35
+ fg = maskImage (image , th )
36
+ final = cv2 .bitwise_or (bg , fg )
37
+ return final
38
+
39
+ def capture ():
40
+ print ("Turning on the WebCam..." )
41
+ return cv2 .VideoCapture (0 )
42
+
43
+ def applyEffects (image , effects , background_effects ):
44
+ for ef in effects :
45
+ args = [image ] + ef [1 ]
46
+ image = ef [0 ](* args )
47
+ return changeBackground (image , image , background_effects )
48
+
49
+ def webCam (user = defaultUser , effects = defaultEffect , background_effects = defaultEffect , number_of_windows = 1 , apply_effects_to = "all" , condition = defaultCondition ):
50
+ global counterIterator
51
+ counterIterator = 0
52
+ cap = capture ()
53
+ while (cap .isOpened ()):
54
+ ret , image = cap .read ()
55
+ if ret == True and condition [0 ](* condition [1 ]):
56
+ for i in range (number_of_windows ):
57
+ if apply_effects_to == "all" :
58
+ image = applyEffects (image , effects , background_effects )
59
+ else :
60
+ image = applyEffects (image , [effects [i ]], [background_effects [i ]])
61
+ cv2 .imshow (user , image )
62
+ if cv2 .waitKey (1 ) & 0xFF == ord ('s' ):
63
+ break
64
+ else : break
65
+ cap .release ()
66
+ cv2 .destroyAllWindows ()
67
+
68
+ def changeBackgroundWebCam (user = defaultUser , new_background = solidBackground (), effects = defaultEffect , condition = defaultCondition ):
69
+ webCam (user , [(changeBackground , [new_background ])] + effects , condition )
70
+
71
+ class WEBCAM ():
72
+ def __init__ (self , user , condition = count , * args ):
73
+ self .user = user
74
+ self .condition = (condition , args )
75
+ def default (self , effects = defaultEffect , background_effects = defaultEffect , number_of_windows = 1 , apply_effects_to = "all" ,):
76
+ webCam (user = self .user ,
77
+ effects = effects ,
78
+ background_effects = background_effects ,
79
+ number_of_windows = number_of_windows ,
80
+ apply_effects_to = apply_effects_to ,
81
+ condition = self .condition )
82
+ def changeBackground (self , new_background , effects = defaultEffect , background_effects = defaultEffect , number_of_windows = 1 , apply_effects_to = "all" ):
83
+ changeBackgroundWebCam (user = self .user ,
84
+ new_background = new_background ,
85
+ effects = effects ,
86
+ background_effects = background_effects ,
87
+ number_of_windows = number_of_windows ,
88
+ apply_effects_to = apply_effects_to ,
89
+ condition = self .condition )
90
+
91
+ class User ():
92
+ def __init__ (self , username = "video" ):
93
+ self .user = username
94
+ self .background = 0
95
+ self .ef = defaultEffect
96
+ def setBackground (self , background ):
97
+ self .background = background
98
+ def addEffect (self , * effects ):
99
+ self .ef = effects
100
+ def meeting (self , condition = forever , * args ):
101
+ if type (self .background ) == int :
102
+ webCam (self .user , condition , * args )
103
+ else :
104
+ changeBackgroundWebCam (self .user , self .background , condition , * args )
0 commit comments