Jainam Jain
commited on
Commit
•
d372282
1
Parent(s):
8a046b1
Updated Module
Browse files- app.py +6 -3
- images.py +7 -1
- media/ThugLife.mp3 +0 -0
- media/glasses.png +0 -0
- modules/ThugLife/__pycache__/main.cpython-310.pyc +0 -0
- modules/ThugLife/haarcascade_frontalface_default.xml +0 -0
- modules/ThugLife/main.py +50 -0
- requiremments.txt → requirements.txt +1 -0
app.py
CHANGED
@@ -4,7 +4,7 @@ import numpy as np
|
|
4 |
import streamlit as st
|
5 |
from webcam import webcam
|
6 |
from images import image_transformations
|
7 |
-
|
8 |
title = st.empty()
|
9 |
title.header("Image Filter")
|
10 |
img_extensions = ["jpg","png","jpeg"]
|
@@ -25,13 +25,16 @@ if selected_option == "Image Filters":
|
|
25 |
if img_operation_mode == "Local Storage":
|
26 |
uploaded_file = st.file_uploader("Upload images from local storage here",type = img_extensions)
|
27 |
if uploaded_file is not None:
|
|
|
|
|
|
|
28 |
img_bytes = uploaded_file.read()
|
29 |
decoded_img = cv2.imdecode(np.frombuffer(img_bytes, np.uint8), -1)
|
30 |
result = decoded_img
|
31 |
-
|
32 |
filter = st.selectbox("Choose an Image filter: ",["Basic Image Editing","Thug Life","Green Screen","Moustaches",\
|
33 |
"Devil-ie","Heart Eyes","John Cena XD","Cartoonie","Face Blur"],0)
|
34 |
-
image_transformations(result,filter,uploaded_file.name.split("."))
|
35 |
|
36 |
elif img_operation_mode == "Take a snap from Webcam":
|
37 |
result = webcam()
|
|
|
4 |
import streamlit as st
|
5 |
from webcam import webcam
|
6 |
from images import image_transformations
|
7 |
+
from PIL import Image
|
8 |
title = st.empty()
|
9 |
title.header("Image Filter")
|
10 |
img_extensions = ["jpg","png","jpeg"]
|
|
|
25 |
if img_operation_mode == "Local Storage":
|
26 |
uploaded_file = st.file_uploader("Upload images from local storage here",type = img_extensions)
|
27 |
if uploaded_file is not None:
|
28 |
+
|
29 |
+
image_byte = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
30 |
+
uploaded_file.seek(0)
|
31 |
img_bytes = uploaded_file.read()
|
32 |
decoded_img = cv2.imdecode(np.frombuffer(img_bytes, np.uint8), -1)
|
33 |
result = decoded_img
|
34 |
+
inp_img = cv2.imdecode(image_byte, 1)
|
35 |
filter = st.selectbox("Choose an Image filter: ",["Basic Image Editing","Thug Life","Green Screen","Moustaches",\
|
36 |
"Devil-ie","Heart Eyes","John Cena XD","Cartoonie","Face Blur"],0)
|
37 |
+
image_transformations(result,inp_img,filter,uploaded_file.name.split("."))
|
38 |
|
39 |
elif img_operation_mode == "Take a snap from Webcam":
|
40 |
result = webcam()
|
images.py
CHANGED
@@ -5,6 +5,7 @@ import numpy as np
|
|
5 |
from PIL import ImageFont, ImageDraw, Image
|
6 |
import streamlit as st
|
7 |
from modules.Cartoon.main import cartoon
|
|
|
8 |
# from helper.utils import file_checker
|
9 |
# from helper.descriptor import file_info
|
10 |
# from helper.rcnn_utils import generate_rcnn_mask
|
@@ -18,7 +19,7 @@ def get_binary_file_downloader_html(bin_file,file_label='File'):
|
|
18 |
href = f'<h3><a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a></h3>'
|
19 |
return href
|
20 |
|
21 |
-
def image_transformations(result,filter,img_extension = "jpg"):
|
22 |
result = result[:,:,:3]
|
23 |
# filter_info(filter)
|
24 |
if filter == "Basic Image Editing":
|
@@ -53,6 +54,11 @@ def image_transformations(result,filter,img_extension = "jpg"):
|
|
53 |
blurVal = st.slider("Blurr effect",3,101,7,2)
|
54 |
totalCols = st.slider("Total Color in images",2,100,11,1)
|
55 |
result = cartoon(result,[line_size,blurVal,totalCols])
|
|
|
|
|
|
|
|
|
|
|
56 |
if np.all(result != None):
|
57 |
st.image(result, use_column_width=True, clamp=True, channels="BGR")
|
58 |
filename = img_extension[0] + "." + img_extension[-1]
|
|
|
5 |
from PIL import ImageFont, ImageDraw, Image
|
6 |
import streamlit as st
|
7 |
from modules.Cartoon.main import cartoon
|
8 |
+
from modules.ThugLife.main import thug_life
|
9 |
# from helper.utils import file_checker
|
10 |
# from helper.descriptor import file_info
|
11 |
# from helper.rcnn_utils import generate_rcnn_mask
|
|
|
19 |
href = f'<h3><a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a></h3>'
|
20 |
return href
|
21 |
|
22 |
+
def image_transformations(result,inp_img,filter,img_extension = "jpg"):
|
23 |
result = result[:,:,:3]
|
24 |
# filter_info(filter)
|
25 |
if filter == "Basic Image Editing":
|
|
|
54 |
blurVal = st.slider("Blurr effect",3,101,7,2)
|
55 |
totalCols = st.slider("Total Color in images",2,100,11,1)
|
56 |
result = cartoon(result,[line_size,blurVal,totalCols])
|
57 |
+
elif filter == "Thug Life":
|
58 |
+
angle = st.slider("Angle",-360,360,0,1)
|
59 |
+
x = st.slider("Shift X",-1.0,1.0,0.0,0.1)
|
60 |
+
y = st.slider("Shift Y", -1.0, 1.0, 0.0, 0.1)
|
61 |
+
result = thug_life(inp_img,angle,x,y)
|
62 |
if np.all(result != None):
|
63 |
st.image(result, use_column_width=True, clamp=True, channels="BGR")
|
64 |
filename = img_extension[0] + "." + img_extension[-1]
|
media/ThugLife.mp3
ADDED
Binary file (161 kB). View file
|
|
media/glasses.png
ADDED
modules/ThugLife/__pycache__/main.cpython-310.pyc
ADDED
Binary file (1.8 kB). View file
|
|
modules/ThugLife/haarcascade_frontalface_default.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
modules/ThugLife/main.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import imutils
|
4 |
+
import streamlit as st
|
5 |
+
import base64
|
6 |
+
def detect_face(input_img):
|
7 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
8 |
+
gray_image = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
|
9 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
|
10 |
+
|
11 |
+
if len(faces) == 1:
|
12 |
+
return faces[0]
|
13 |
+
else:
|
14 |
+
return None
|
15 |
+
|
16 |
+
def thug_life(input_img,angle,x,y):
|
17 |
+
face_coords = detect_face(input_img)
|
18 |
+
|
19 |
+
if face_coords is None:
|
20 |
+
return input_img
|
21 |
+
|
22 |
+
sunglass_img = cv2.imread('media/glasses.png', cv2.IMREAD_UNCHANGED)
|
23 |
+
sunglass_img = imutils.rotate(sunglass_img, angle=angle)
|
24 |
+
face_width, face_height = face_coords[2], face_coords[3]
|
25 |
+
sunglass_img_resized = cv2.resize(sunglass_img, (face_width, face_height))
|
26 |
+
|
27 |
+
# Check if the image has an alpha channel
|
28 |
+
if sunglass_img_resized.shape[2] == 4:
|
29 |
+
alpha_channel_resized = cv2.resize(sunglass_img_resized[:, :, 3], (face_width, face_height))
|
30 |
+
else:
|
31 |
+
# If no alpha channel, create a default one
|
32 |
+
alpha_channel_resized = np.ones((face_height, face_width), dtype=np.uint8) * 255
|
33 |
+
|
34 |
+
thug_glass = alpha_channel_resized / 255.0
|
35 |
+
thug_glass = np.stack([thug_glass] * 3, axis=-1)
|
36 |
+
offset_y = int(face_height * y) # Adjust the offset based on your preference
|
37 |
+
start_y = max(0, face_coords[1] - offset_y)
|
38 |
+
offset_x = int(face_width * x) # Adjust the offset based on your preference
|
39 |
+
start_x = max(0, face_coords[1] - offset_x)
|
40 |
+
roi = input_img[start_y: start_y + face_height, start_x:start_x + face_width]
|
41 |
+
|
42 |
+
glass_bgr = sunglass_img_resized[:, :, :3]
|
43 |
+
overlay = (1 - thug_glass) * roi + thug_glass * glass_bgr
|
44 |
+
input_img[start_y: start_y + face_height, start_x:start_x + face_width] = overlay
|
45 |
+
music_file = 'media/ThugLife.mp3'
|
46 |
+
audio_bytes = open(music_file,'rb').read()
|
47 |
+
audio_bytes = base64.b64encode(audio_bytes).decode()
|
48 |
+
st.markdown(f'<audio autoplay controls><source src="data:audio/mp3;base64,{audio_bytes}" type="audio/mp3"></audio>', unsafe_allow_html=True)
|
49 |
+
|
50 |
+
return input_img
|
requiremments.txt → requirements.txt
RENAMED
@@ -59,3 +59,4 @@ validators==0.22.0
|
|
59 |
watchdog==3.0.0
|
60 |
webcam==1.34
|
61 |
zipp==3.17.0
|
|
|
|
59 |
watchdog==3.0.0
|
60 |
webcam==1.34
|
61 |
zipp==3.17.0
|
62 |
+
|