Skip to content

Commit 37038f7

Browse files
committed
Add video module and WEBCAM class
1 parent 7932b34 commit 37038f7

File tree

11 files changed

+201
-1
lines changed

11 files changed

+201
-1
lines changed
39 Bytes
Binary file not shown.

eggdriver/resources/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from eggdriver.resources.utils import *
1212
from eggdriver.resources.web import *
1313
from eggdriver.resources.math import *
14+
from eggdriver.resources.video import *
1415

1516
author="eanorambuena"
1617
author_email="[email protected]"
Binary file not shown.

eggdriver/resources/video/__init__.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import cv2
2+
import numpy as np
3+
4+
def solidBackground(shape = (480, 640, 3), color = (0, 255, 0)):
5+
bg_image = np.ones(shape, dtype = np.uint8)
6+
bg_image[:] = color
7+
return bg_image
8+
9+
def imageBackground(source):
10+
return cv2.imread(source)

eggdriver/resources/video/effects.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cv2
2+
import numpy as np
3+
import mediapipe as mp
4+
5+
def removeBG(image):
6+
mp_selfie_segmentation = mp.solutions.selfie_segmentation
7+
selfie_segmentation = mp_selfie_segmentation.SelfieSegmentation(model_selection = 1)
8+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
9+
results = selfie_segmentation.process(image)
10+
_, th = cv2.threshold(results.segmentation_mask, 0.75, 255, cv2.THRESH_BINARY)
11+
th = th.astype(np.uint8)
12+
th_inv = cv2.bitwise_not(th)
13+
return th, th_inv
14+
15+
def maskImage(image, mask):
16+
return cv2.bitwise_and(image, image, mask = mask)
17+
18+
def blur(image):
19+
return cv2.GaussianBlur(image, (15, 15), 0)

test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
build() ##
77
###############################
88

9-
from eggdriver import Matrix, Vector, build
9+
from eggdriver import Matrix, Vector, WEBCAM, build, changeBackground, blur, solidBackground
1010

1111
c = Matrix("""
1212
| 1 1 2 3 4 |
@@ -20,3 +20,5 @@
2020
a = Vector("[ 1 2 3 4 5 6 30 0 9]")
2121
a.display()
2222

23+
w = WEBCAM("Emmanuel")
24+
w.default(background_effects= [(blur, [])], effects = [(changeBackground, [solidBackground()])])

0 commit comments

Comments
 (0)