Update app.py
Browse files
app.py
CHANGED
@@ -1,131 +1,146 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
import
|
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 |
-
center_x
|
39 |
-
cv2.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
st.
|
74 |
-
|
75 |
-
"
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
import zipfile
|
7 |
+
|
8 |
+
def detect_nose_and_realistically_thicken(image):
|
9 |
+
# Load pre-trained classifiers for face and nose detection
|
10 |
+
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
11 |
+
nose_cascade = cv2.CascadeClassifier('haarcascade_mcs_nose.xml')
|
12 |
+
|
13 |
+
# Convert the image to grayscale for better detection
|
14 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
15 |
+
|
16 |
+
# Detect faces in the image
|
17 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
18 |
+
|
19 |
+
for (x, y, w, h) in faces:
|
20 |
+
# For each face, detect the nose
|
21 |
+
face_region = gray[y:y+h, x:x+w]
|
22 |
+
noses = nose_cascade.detectMultiScale(face_region, 1.3, 5)
|
23 |
+
|
24 |
+
for (nx, ny, nw, nh) in noses:
|
25 |
+
# Get the nose region from the original image
|
26 |
+
nose_region = image[y+ny:y+ny+nh, x+nx:x+nx+nw]
|
27 |
+
|
28 |
+
# Thicken the nose by increasing its width symmetrically
|
29 |
+
thicken_factor = 1.5 # Adjust this value to control thickness
|
30 |
+
new_width = int(nw * thicken_factor)
|
31 |
+
|
32 |
+
# Create a thickened nose by resizing
|
33 |
+
thickened_nose = cv2.resize(nose_region, (new_width, nh), interpolation=cv2.INTER_LINEAR)
|
34 |
+
|
35 |
+
# Feather the edges of the thickened nose to blend with the face
|
36 |
+
mask = np.zeros_like(thickened_nose, dtype=np.float32)
|
37 |
+
center_x = new_width // 2
|
38 |
+
cv2.circle(mask, (center_x, nh // 2), int(min(new_width, nh) * 0.6), (1, 1, 1), -1, cv2.LINE_AA)
|
39 |
+
mask = cv2.GaussianBlur(mask, (21, 21), 10) # Feathering
|
40 |
+
|
41 |
+
# Calculate the offsets to center the thickened nose
|
42 |
+
left_offset = (new_width - nw) // 2
|
43 |
+
|
44 |
+
# Ensure not to go out of bounds
|
45 |
+
start_x = max(x + nx - left_offset, 0)
|
46 |
+
end_x = min(start_x + new_width, image.shape[1])
|
47 |
+
start_y = y + ny
|
48 |
+
end_y = start_y + nh
|
49 |
+
|
50 |
+
# Blend the thickened nose smoothly into the face
|
51 |
+
alpha = 0.7 # Controls transparency of thickened nose
|
52 |
+
|
53 |
+
# Blending with the feathered mask to smooth edges
|
54 |
+
image[start_y:end_y, start_x:end_x] = (
|
55 |
+
image[start_y:end_y, start_x:end_x].astype(np.float32) * (1 - mask) +
|
56 |
+
thickened_nose[:, :end_x - start_x].astype(np.float32) * mask
|
57 |
+
).astype(np.uint8)
|
58 |
+
|
59 |
+
return image
|
60 |
+
|
61 |
+
def main():
|
62 |
+
st.title("Nose Thickening App")
|
63 |
+
|
64 |
+
# Modal Popup for Instructions
|
65 |
+
with st.expander("Application of OpenCV for Nose thickening", expanded=False):
|
66 |
+
st.write(
|
67 |
+
"The application of OpenCV for nose thickening showcases the power of computer vision in image processing. By leveraging pre-trained classifiers like Haar cascades, the program effectively detects facial features, particularly the nose, within images. The algorithm then applies a thicken effect by resizing the detected nose region, ensuring a realistic blend with the surrounding facial features. This involves techniques such as Gaussian blurring to feather the edges of the modified area, resulting in a seamless transition. The integration with Streamlit allows users to easily upload images, apply the effect, and download the modified results, making sophisticated image manipulation accessible to a broader audience without requiring extensive programming knowledge. This demonstrates OpenCV's versatility in creative applications, from enhancing personal photos to providing entertainment or social media enhancements."
|
68 |
+
)
|
69 |
+
|
70 |
+
# Instructions sidebar
|
71 |
+
with st.sidebar:
|
72 |
+
st.header("Instructions")
|
73 |
+
st.write(
|
74 |
+
"1) If you choose only one image to upload, the results will be shown on display, and you can click the 'Download Images' button to download the processed images in zip format.\n"
|
75 |
+
"2) If you choose more than one image, it will not display any image as results; instead, it will directly show you the 'Download Images' button, and you can click on it to download the processed images in zip format."
|
76 |
+
)
|
77 |
+
|
78 |
+
uploaded_files = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
79 |
+
|
80 |
+
if st.button("Thicken Nose"):
|
81 |
+
if len(uploaded_files) == 1:
|
82 |
+
image = Image.open(uploaded_files[0]).convert("RGB")
|
83 |
+
image_np = np.array(image)
|
84 |
+
|
85 |
+
# Create a progress spinner
|
86 |
+
with st.spinner("Processing..."):
|
87 |
+
# Apply the nose detection and thickening effect
|
88 |
+
output_image = detect_nose_and_realistically_thicken(image_np)
|
89 |
+
|
90 |
+
# Display the output image
|
91 |
+
st.image(output_image, caption="Output Image", use_column_width=True)
|
92 |
+
|
93 |
+
# Convert the output image to RGB for saving
|
94 |
+
output_image_rgb = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
95 |
+
|
96 |
+
# Save the output image in the correct color format
|
97 |
+
original_filename = os.path.splitext(uploaded_files[0].name)[0]
|
98 |
+
output_path = f"{original_filename}_thickened_nose.jpg"
|
99 |
+
cv2.imwrite(output_path, output_image_rgb) # Save as RGB
|
100 |
+
|
101 |
+
st.success("Nose thickening applied! You can download the image below.")
|
102 |
+
st.download_button("Download Image", data=open(output_path, "rb").read(), file_name=output_path, mime='image/jpeg')
|
103 |
+
|
104 |
+
elif len(uploaded_files) > 1:
|
105 |
+
st.warning("Multiple images uploaded. No output will be displayed. Please download the images directly.")
|
106 |
+
|
107 |
+
# Save all images in the output directory
|
108 |
+
output_dir = 'output_images'
|
109 |
+
os.makedirs(output_dir, exist_ok=True)
|
110 |
+
|
111 |
+
# Create a progress bar
|
112 |
+
progress_bar = st.progress(0)
|
113 |
+
for i, uploaded_file in enumerate(uploaded_files):
|
114 |
+
image = Image.open(uploaded_file).convert("RGB")
|
115 |
+
image_np = np.array(image)
|
116 |
+
|
117 |
+
with st.spinner(f"Processing image {i + 1} of {len(uploaded_files)}..."):
|
118 |
+
output_image = detect_nose_and_realistically_thicken(image_np)
|
119 |
+
|
120 |
+
# Convert the output image to RGB for saving
|
121 |
+
output_image_rgb = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
122 |
+
|
123 |
+
# Create a new filename based on the original
|
124 |
+
original_filename = os.path.splitext(uploaded_file.name)[0]
|
125 |
+
output_filename = f"{original_filename}_thickened_nose.jpg"
|
126 |
+
output_path = os.path.join(output_dir, output_filename)
|
127 |
+
|
128 |
+
cv2.imwrite(output_path, output_image_rgb) # Save as RGB
|
129 |
+
|
130 |
+
# Update progress bar
|
131 |
+
progress_bar.progress((i + 1) / len(uploaded_files))
|
132 |
+
|
133 |
+
st.success("Nose thickening applied to all images! You can download them below.")
|
134 |
+
|
135 |
+
# Zip the images for download
|
136 |
+
zip_file_path = 'output_images.zip'
|
137 |
+
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
|
138 |
+
for uploaded_file in uploaded_files:
|
139 |
+
original_filename = os.path.splitext(uploaded_file.name)[0]
|
140 |
+
output_filename = f"{original_filename}_thickened_nose.jpg"
|
141 |
+
zipf.write(os.path.join(output_dir, output_filename), arcname=output_filename)
|
142 |
+
|
143 |
+
st.download_button("Download Images", data=open(zip_file_path, "rb").read(), file_name='output_images.zip', mime='application/zip')
|
144 |
+
|
145 |
+
if __name__ == "__main__":
|
146 |
+
main()
|