Spaces:
Runtime error
Runtime error
Upload 8 files
Browse files- Challenge - GOT Snapchat Filter.PNG +0 -0
- Code.py +44 -0
- app.py +38 -0
- app1.py +35 -0
- modelCamera.ipynb +111 -0
- modelPicture.ipynb +115 -0
- outputImg.csv +0 -0
- tempCodeRunnerFile.python +56 -0
Challenge - GOT Snapchat Filter.PNG
ADDED
Code.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %%
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
|
5 |
+
|
6 |
+
# %%
|
7 |
+
glasses=cv2.imread('Train/glasses.png',cv2.IMREAD_UNCHANGED)
|
8 |
+
mustache=cv2.imread('Train/mustache.png',cv2.IMREAD_UNCHANGED)
|
9 |
+
|
10 |
+
# %%
|
11 |
+
glassesCasc=cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')
|
12 |
+
noseCasc=cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')
|
13 |
+
|
14 |
+
def apply_effects(frame):
|
15 |
+
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
16 |
+
eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0)
|
17 |
+
for (x, y, w, h) in eyes:
|
18 |
+
glasses_resized = cv2.resize(glasses, (w, h))
|
19 |
+
alpha_channel = glasses_resized[:, :, 3] / 255.0
|
20 |
+
|
21 |
+
# Create a mask for the glasses
|
22 |
+
glasses_mask = np.zeros_like(glasses_resized[:, :, 3])
|
23 |
+
|
24 |
+
# Copy alpha channel to mask and apply threshold
|
25 |
+
glasses_mask[glasses_resized[:, :, 3] > 0] = 255
|
26 |
+
|
27 |
+
# Overlay the glasses using the mask
|
28 |
+
for c in range(0, 3):
|
29 |
+
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c]
|
30 |
+
|
31 |
+
nose=noseCasc.detectMultiScale(gray,1.3,5,0)
|
32 |
+
for (x, y, w, h) in nose:
|
33 |
+
mustache_resized = cv2.resize(mustache, (w, h))
|
34 |
+
alpha_channel = mustache_resized[:, :, 3] / 255.0
|
35 |
+
|
36 |
+
mustache_mask = np.zeros_like(mustache_resized[:, :, 3])
|
37 |
+
|
38 |
+
# Copy alpha channel to mask and apply threshold
|
39 |
+
mustache_mask[mustache_resized[:, :, 3] > 0] = 255
|
40 |
+
|
41 |
+
for c in range(0, 3):
|
42 |
+
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * mustache_resized[:, :, c]
|
43 |
+
|
44 |
+
return frame
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
from Code import apply_effects
|
5 |
+
|
6 |
+
# Load cascade classifiers and images
|
7 |
+
glassesCasc = cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')
|
8 |
+
noseCasc = cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')
|
9 |
+
glasses = cv2.imread('Train/glasses.png', cv2.IMREAD_UNCHANGED)
|
10 |
+
mustache = cv2.imread('Train/mustache.png', cv2.IMREAD_UNCHANGED)
|
11 |
+
|
12 |
+
def main():
|
13 |
+
st.title("Snapchat Filter App")
|
14 |
+
st.write("Upload an image or use your webcam to apply face effects!")
|
15 |
+
|
16 |
+
option = st.selectbox("Choose an option", ("Upload Image", "Use Webcam"))
|
17 |
+
|
18 |
+
if option == "Upload Image":
|
19 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
20 |
+
if uploaded_image is not None:
|
21 |
+
image = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), 1)
|
22 |
+
image_with_effects = apply_effects(image)
|
23 |
+
st.image(image_with_effects, channels="BGR", use_column_width=True)
|
24 |
+
|
25 |
+
else: # Use Webcam
|
26 |
+
cap = cv2.VideoCapture(0)
|
27 |
+
st.write("Webcam is active.")
|
28 |
+
frame_placeholder = st.empty()
|
29 |
+
|
30 |
+
while cap.isOpened():
|
31 |
+
ret, frame = cap.read()
|
32 |
+
if not ret:
|
33 |
+
break
|
34 |
+
image_with_effects = apply_effects(frame)
|
35 |
+
frame_placeholder.image(image_with_effects, channels="BGR", use_column_width=True)
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
main()
|
app1.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load the cascade classifiers and images
|
6 |
+
glassesCasc = cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')
|
7 |
+
noseCasc = cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')
|
8 |
+
glasses = cv2.imread('Train/glasses.png', cv2.IMREAD_UNCHANGED)
|
9 |
+
mustache = cv2.imread('Train/mustache.png', cv2.IMREAD_UNCHANGED)
|
10 |
+
|
11 |
+
def apply_effects(frame):
|
12 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
13 |
+
|
14 |
+
eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0)
|
15 |
+
for (x, y, w, h) in eyes:
|
16 |
+
glasses_resized = cv2.resize(glasses, (w, h))
|
17 |
+
alpha_channel = glasses_resized[:, :, 3] / 255.0
|
18 |
+
glasses_mask = np.zeros_like(glasses_resized[:, :, 3])
|
19 |
+
glasses_mask[glasses_resized[:, :, 3] > 0] = 255
|
20 |
+
for c in range(0, 3):
|
21 |
+
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c]
|
22 |
+
|
23 |
+
nose = noseCasc.detectMultiScale(gray, 1.3, 5, 0)
|
24 |
+
for (x, y, w, h) in nose:
|
25 |
+
mustache_resized = cv2.resize(mustache, (w, h))
|
26 |
+
alpha_channel = mustache_resized[:, :, 3] / 255.0
|
27 |
+
mustache_mask = np.zeros_like(mustache_resized[:, :, 3])
|
28 |
+
mustache_mask[mustache_resized[:, :, 3] > 0] = 255
|
29 |
+
for c in range(0, 3):
|
30 |
+
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * mustache_resized[:, :, c]
|
31 |
+
|
32 |
+
return frame
|
33 |
+
|
34 |
+
iface = gr.Interface(fn=apply_effects, inputs="webcam", outputs="image")
|
35 |
+
iface.launch()
|
modelCamera.ipynb
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 11,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import cv2\n",
|
10 |
+
"import numpy as np "
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": 12,
|
16 |
+
"metadata": {},
|
17 |
+
"outputs": [],
|
18 |
+
"source": [
|
19 |
+
"glasses=cv2.imread('Train/glasses.png',cv2.IMREAD_UNCHANGED)\n",
|
20 |
+
"mustache=cv2.imread('Train/mustache.png',cv2.IMREAD_UNCHANGED)"
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": 13,
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"glassesCasc=cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')\n",
|
30 |
+
"noseCasc=cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"cell_type": "code",
|
35 |
+
"execution_count": 14,
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"cap=cv2.VideoCapture(0)"
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"cell_type": "code",
|
44 |
+
"execution_count": 15,
|
45 |
+
"metadata": {},
|
46 |
+
"outputs": [],
|
47 |
+
"source": [
|
48 |
+
"while True:\n",
|
49 |
+
" ret,frame=cap.read()\n",
|
50 |
+
" gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n",
|
51 |
+
" eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0)\n",
|
52 |
+
" for (x, y, w, h) in eyes:\n",
|
53 |
+
" glasses_resized = cv2.resize(glasses, (w, h))\n",
|
54 |
+
" alpha_channel = glasses_resized[:, :, 3] / 255.0\n",
|
55 |
+
" \n",
|
56 |
+
" # Create a mask for the glasses\n",
|
57 |
+
" glasses_mask = np.zeros_like(glasses_resized[:, :, 3])\n",
|
58 |
+
" \n",
|
59 |
+
" # Copy alpha channel to mask and apply threshold\n",
|
60 |
+
" glasses_mask[glasses_resized[:, :, 3] > 0] = 255\n",
|
61 |
+
" \n",
|
62 |
+
" # Overlay the glasses using the mask\n",
|
63 |
+
" for c in range(0, 3):\n",
|
64 |
+
" frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c]\n",
|
65 |
+
"\n",
|
66 |
+
" nose=noseCasc.detectMultiScale(gray,1.3,5,0)\n",
|
67 |
+
" for (x, y, w, h) in nose:\n",
|
68 |
+
" mustache_resized = cv2.resize(mustache, (w, h))\n",
|
69 |
+
" alpha_channel = mustache_resized[:, :, 3] / 255.0\n",
|
70 |
+
" \n",
|
71 |
+
" mustache_mask = np.zeros_like(mustache_resized[:, :, 3])\n",
|
72 |
+
" \n",
|
73 |
+
" # Copy alpha channel to mask and apply threshold\n",
|
74 |
+
" mustache_mask[mustache_resized[:, :, 3] > 0] = 255\n",
|
75 |
+
"\n",
|
76 |
+
" for c in range(0, 3):\n",
|
77 |
+
" frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * mustache_resized[:, :, c]\n",
|
78 |
+
"\n",
|
79 |
+
" cv2.imshow('Webcam Feed', frame)\n",
|
80 |
+
"\n",
|
81 |
+
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
|
82 |
+
" break\n",
|
83 |
+
"\n",
|
84 |
+
"cap.release()\n",
|
85 |
+
"cv2.destroyAllWindows()"
|
86 |
+
]
|
87 |
+
}
|
88 |
+
],
|
89 |
+
"metadata": {
|
90 |
+
"kernelspec": {
|
91 |
+
"display_name": "base",
|
92 |
+
"language": "python",
|
93 |
+
"name": "python3"
|
94 |
+
},
|
95 |
+
"language_info": {
|
96 |
+
"codemirror_mode": {
|
97 |
+
"name": "ipython",
|
98 |
+
"version": 3
|
99 |
+
},
|
100 |
+
"file_extension": ".py",
|
101 |
+
"mimetype": "text/x-python",
|
102 |
+
"name": "python",
|
103 |
+
"nbconvert_exporter": "python",
|
104 |
+
"pygments_lexer": "ipython3",
|
105 |
+
"version": "3.10.9"
|
106 |
+
},
|
107 |
+
"orig_nbformat": 4
|
108 |
+
},
|
109 |
+
"nbformat": 4,
|
110 |
+
"nbformat_minor": 2
|
111 |
+
}
|
modelPicture.ipynb
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import cv2\n",
|
10 |
+
"import numpy as np "
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": 2,
|
16 |
+
"metadata": {},
|
17 |
+
"outputs": [],
|
18 |
+
"source": [
|
19 |
+
"glasses=cv2.imread('Train/glasses.png',cv2.IMREAD_UNCHANGED)\n",
|
20 |
+
"mustache=cv2.imread('Train/mustache.png',cv2.IMREAD_UNCHANGED)\n",
|
21 |
+
"\n",
|
22 |
+
"frame=cv2.imread('Test/Before.png')"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 3,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [],
|
30 |
+
"source": [
|
31 |
+
"glassesCasc=cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')\n",
|
32 |
+
"noseCasc=cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": 4,
|
38 |
+
"metadata": {},
|
39 |
+
"outputs": [],
|
40 |
+
"source": [
|
41 |
+
"gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n",
|
42 |
+
"eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0)\n",
|
43 |
+
"for (x, y, w, h) in eyes:\n",
|
44 |
+
" glasses_resized = cv2.resize(glasses, (w, h))\n",
|
45 |
+
" alpha_channel = glasses_resized[:, :, 3] / 255.0\n",
|
46 |
+
" \n",
|
47 |
+
" # Create a mask for the glasses\n",
|
48 |
+
" glasses_mask = np.zeros_like(glasses_resized[:, :, 3])\n",
|
49 |
+
" \n",
|
50 |
+
" # Copy alpha channel to mask and apply threshold\n",
|
51 |
+
" glasses_mask[glasses_resized[:, :, 3] > 0] = 255\n",
|
52 |
+
" \n",
|
53 |
+
" # Overlay the glasses using the mask\n",
|
54 |
+
" for c in range(0, 3):\n",
|
55 |
+
" frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c]\n",
|
56 |
+
"\n",
|
57 |
+
"nose=noseCasc.detectMultiScale(gray,1.1,5,0)\n",
|
58 |
+
"for (x, y, w, h) in nose:\n",
|
59 |
+
" mustache_resized = cv2.resize(mustache, (w, h))\n",
|
60 |
+
" alpha_channel = mustache_resized[:, :, 3] / 255.0\n",
|
61 |
+
" \n",
|
62 |
+
" mustache_mask = np.zeros_like(mustache_resized[:, :, 3])\n",
|
63 |
+
" \n",
|
64 |
+
" # Copy alpha channel to mask and apply threshold\n",
|
65 |
+
" mustache_mask[mustache_resized[:, :, 3] > 0] = 255\n",
|
66 |
+
"\n",
|
67 |
+
" for c in range(0, 3):\n",
|
68 |
+
" frame[y+20:y+h+20, x:x+w, c] = (1 - alpha_channel) * frame[y+20:y+h+20, x:x+w, c] + alpha_channel * mustache_resized[:, :, c]"
|
69 |
+
]
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"cell_type": "code",
|
73 |
+
"execution_count": 5,
|
74 |
+
"metadata": {},
|
75 |
+
"outputs": [],
|
76 |
+
"source": [
|
77 |
+
"flattened_image = frame.reshape(-1, 3)\n",
|
78 |
+
"column_names = ['Channel 1', 'Channel 2', 'Channel 3']\n",
|
79 |
+
"\n",
|
80 |
+
"data_with_headers = np.vstack([column_names, flattened_image])"
|
81 |
+
]
|
82 |
+
},
|
83 |
+
{
|
84 |
+
"cell_type": "code",
|
85 |
+
"execution_count": 6,
|
86 |
+
"metadata": {},
|
87 |
+
"outputs": [],
|
88 |
+
"source": [
|
89 |
+
"np.savetxt('outputImg.csv', data_with_headers, delimiter=',', fmt='%s')"
|
90 |
+
]
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"metadata": {
|
94 |
+
"kernelspec": {
|
95 |
+
"display_name": "base",
|
96 |
+
"language": "python",
|
97 |
+
"name": "python3"
|
98 |
+
},
|
99 |
+
"language_info": {
|
100 |
+
"codemirror_mode": {
|
101 |
+
"name": "ipython",
|
102 |
+
"version": 3
|
103 |
+
},
|
104 |
+
"file_extension": ".py",
|
105 |
+
"mimetype": "text/x-python",
|
106 |
+
"name": "python",
|
107 |
+
"nbconvert_exporter": "python",
|
108 |
+
"pygments_lexer": "ipython3",
|
109 |
+
"version": "3.10.9"
|
110 |
+
},
|
111 |
+
"orig_nbformat": 4
|
112 |
+
},
|
113 |
+
"nbformat": 4,
|
114 |
+
"nbformat_minor": 2
|
115 |
+
}
|
outputImg.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tempCodeRunnerFile.python
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %%
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# %%
|
6 |
+
glasses=cv2.imread('Train/glasses.png',cv2.IMREAD_UNCHANGED)
|
7 |
+
mustache=cv2.imread('Train/mustache.png',cv2.IMREAD_UNCHANGED)
|
8 |
+
|
9 |
+
# %%
|
10 |
+
glassesCasc=cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')
|
11 |
+
noseCasc=cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')
|
12 |
+
|
13 |
+
def camera():
|
14 |
+
cap=cv2.VideoCapture(0)
|
15 |
+
while True:
|
16 |
+
ret,frame=cap.read()
|
17 |
+
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
18 |
+
eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0)
|
19 |
+
for (x, y, w, h) in eyes:
|
20 |
+
glasses_resized = cv2.resize(glasses, (w, h))
|
21 |
+
alpha_channel = glasses_resized[:, :, 3] / 255.0
|
22 |
+
|
23 |
+
# Create a mask for the glasses
|
24 |
+
glasses_mask = np.zeros_like(glasses_resized[:, :, 3])
|
25 |
+
|
26 |
+
# Copy alpha channel to mask and apply threshold
|
27 |
+
glasses_mask[glasses_resized[:, :, 3] > 0] = 255
|
28 |
+
|
29 |
+
# Overlay the glasses using the mask
|
30 |
+
for c in range(0, 3):
|
31 |
+
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c]
|
32 |
+
|
33 |
+
nose=noseCasc.detectMultiScale(gray,1.1,5,0)
|
34 |
+
for (x, y, w, h) in nose:
|
35 |
+
mustache_resized = cv2.resize(mustache, (w, h))
|
36 |
+
alpha_channel = mustache_resized[:, :, 3] / 255.0
|
37 |
+
|
38 |
+
mustache_mask = np.zeros_like(mustache_resized[:, :, 3])
|
39 |
+
|
40 |
+
# Copy alpha channel to mask and apply threshold
|
41 |
+
mustache_mask[mustache_resized[:, :, 3] > 0] = 255
|
42 |
+
|
43 |
+
for c in range(0, 3):
|
44 |
+
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * mustache_resized[:, :, c]
|
45 |
+
|
46 |
+
cv2.imshow('Webcam Feed', frame)
|
47 |
+
|
48 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
49 |
+
break
|
50 |
+
|
51 |
+
cap.release()
|
52 |
+
cv2.destroyAllWindows()
|
53 |
+
|
54 |
+
camera()
|
55 |
+
|
56 |
+
|