Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from rembg import remove
|
3 |
+
from PIL import Image, ImageOps
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Set page config first
|
7 |
+
st.set_page_config(page_title="Background Removal App", layout="wide")
|
8 |
+
|
9 |
+
# Dark mod için CSS stilleri
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
/* Genel stil */
|
13 |
+
body {
|
14 |
+
background-color: black;
|
15 |
+
color: white;
|
16 |
+
font-family: 'Arial', sans-serif;
|
17 |
+
}
|
18 |
+
|
19 |
+
/* Buton stilleri */
|
20 |
+
.stDownloadButton button {
|
21 |
+
background-color: #4F8BF9;
|
22 |
+
color: white;
|
23 |
+
border: none;
|
24 |
+
padding: 12px 24px;
|
25 |
+
border-radius: 8px;
|
26 |
+
transition: background-color 0.3s ease;
|
27 |
+
font-size: 16px;
|
28 |
+
font-weight: bold;
|
29 |
+
}
|
30 |
+
.stDownloadButton button:hover {
|
31 |
+
background-color: #3B6BB0;
|
32 |
+
}
|
33 |
+
|
34 |
+
/* Footer stilleri */
|
35 |
+
.footer {
|
36 |
+
text-align: center;
|
37 |
+
padding: 10px;
|
38 |
+
background-color: #333;
|
39 |
+
border-radius: 8px;
|
40 |
+
margin-top: 20px;
|
41 |
+
}
|
42 |
+
|
43 |
+
/* Tooltip stilleri */
|
44 |
+
.tooltip {
|
45 |
+
position: relative;
|
46 |
+
display: inline-block;
|
47 |
+
}
|
48 |
+
.tooltip .tooltiptext {
|
49 |
+
visibility: hidden;
|
50 |
+
width: 120px;
|
51 |
+
background-color: #555;
|
52 |
+
color: #fff;
|
53 |
+
text-align: center;
|
54 |
+
border-radius: 6px;
|
55 |
+
padding: 5px;
|
56 |
+
position: absolute;
|
57 |
+
z-index: 1;
|
58 |
+
bottom: 125%; /* Butonun üstünde */
|
59 |
+
left: 50%;
|
60 |
+
margin-left: -60px;
|
61 |
+
opacity: 0;
|
62 |
+
transition: opacity 0.3s;
|
63 |
+
}
|
64 |
+
.tooltip:hover .tooltiptext {
|
65 |
+
visibility: visible;
|
66 |
+
opacity: 1;
|
67 |
+
}
|
68 |
+
</style>
|
69 |
+
""", unsafe_allow_html=True)
|
70 |
+
|
71 |
+
# App title and description
|
72 |
+
st.title("🎨 Modern Background Removal App")
|
73 |
+
st.write("""
|
74 |
+
📸 **Upload an image or use your camera to see the background removed results!**
|
75 |
+
This app is developed using the `rembg` library.
|
76 |
+
""")
|
77 |
+
|
78 |
+
# Yükleme ekranını daha iyi bir yere taşı
|
79 |
+
with st.expander("📁 Upload Image or Use Camera", expanded=True):
|
80 |
+
option = st.radio("Choose Input Method:", ("Upload Image", "Use Camera"))
|
81 |
+
|
82 |
+
if option == "Upload Image":
|
83 |
+
uploaded_file = st.file_uploader("Select an image...", type=["jpg", "jpeg", "png"])
|
84 |
+
else:
|
85 |
+
uploaded_file = st.camera_input("Take a photo")
|
86 |
+
|
87 |
+
if uploaded_file is not None:
|
88 |
+
# Open and display the original image
|
89 |
+
image = Image.open(uploaded_file)
|
90 |
+
st.subheader("🖼️ Original Image")
|
91 |
+
st.image(image, use_container_width=True, caption="Uploaded Image")
|
92 |
+
|
93 |
+
# Image manipulation options
|
94 |
+
st.subheader("🛠️ Image Adjustment")
|
95 |
+
rotate_angle = st.slider("Rotate Image (Degrees)", -180, 180, 0, help="Rotate the image by the specified angle.")
|
96 |
+
resize_width = st.slider("Resize Width", 100, 1000, image.width, help="Resize the image width while maintaining aspect ratio.")
|
97 |
+
|
98 |
+
# Apply image transformations
|
99 |
+
if rotate_angle != 0:
|
100 |
+
image = image.rotate(rotate_angle, resample=Image.Resampling.BICUBIC)
|
101 |
+
if resize_width != image.width:
|
102 |
+
ratio = resize_width / image.width
|
103 |
+
height = int(image.height * ratio)
|
104 |
+
image = image.resize((resize_width, height), resample=Image.Resampling.LANCZOS)
|
105 |
+
|
106 |
+
# Display the adjusted image
|
107 |
+
st.image(image, use_container_width=True, caption="Adjusted Image")
|
108 |
+
|
109 |
+
# Process image to remove background
|
110 |
+
with st.spinner("⏳ Removing background... Please wait."):
|
111 |
+
output_image = remove(image)
|
112 |
+
|
113 |
+
# Display background removed image
|
114 |
+
st.subheader("✅ Background Removed Image")
|
115 |
+
st.image(output_image, use_container_width=True, caption="Background Removed Image")
|
116 |
+
|
117 |
+
# Create silhouette
|
118 |
+
silhouette = np.array(output_image)
|
119 |
+
silhouette[silhouette[..., -1] > 0] = [255, 255, 255, 255] # Foreground white
|
120 |
+
silhouette[silhouette[..., -1] == 0] = [0, 0, 0, 255] # Background black
|
121 |
+
|
122 |
+
# Display silhouette
|
123 |
+
st.subheader("⚫ Silhouette")
|
124 |
+
st.image(silhouette, use_container_width=True, caption="Silhouette Image")
|
125 |
+
|
126 |
+
# Download buttons for results
|
127 |
+
st.markdown("### 📥 Download Options")
|
128 |
+
|
129 |
+
# Download background removed image
|
130 |
+
st.download_button(
|
131 |
+
label="⬇️ Download Background Removed Image",
|
132 |
+
data=output_image.tobytes(),
|
133 |
+
file_name="foreground_image.png",
|
134 |
+
mime="image/png",
|
135 |
+
key="download_foreground",
|
136 |
+
help="Download the image with the background removed."
|
137 |
+
)
|
138 |
+
|
139 |
+
# Download silhouette
|
140 |
+
silhouette_img = Image.fromarray(silhouette)
|
141 |
+
st.download_button(
|
142 |
+
label="⬇️ Download Silhouette",
|
143 |
+
data=silhouette_img.tobytes(),
|
144 |
+
file_name="silhouette_image.png",
|
145 |
+
mime="image/png",
|
146 |
+
key="download_silhouette",
|
147 |
+
help="Download the silhouette image."
|
148 |
+
)
|
149 |
+
|
150 |
+
# Footer
|
151 |
+
st.markdown("---")
|
152 |
+
st.markdown("""
|
153 |
+
<div class="footer">
|
154 |
+
🚀 **Developed by Metin Haşimi.**<br>
|
155 |
+
🌟 Connect with me on <a href="https://www.linkedin.com/in/metin-hasimi-33aa82269" target="_blank">LinkedIn</a> or check out my <a href="https://github.com/metinhasimi22" target="_blank">GitHub</a>.
|
156 |
+
</div>
|
157 |
+
""", unsafe_allow_html=True)
|