Final update
Browse files
app.py
CHANGED
|
@@ -1,76 +1,100 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from gradio_imageslider import ImageSlider
|
| 3 |
from rembg import remove
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
-
.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
margin-bottom: 20px;
|
| 24 |
}
|
| 25 |
"""
|
| 26 |
|
| 27 |
def remove_background(input_image):
|
| 28 |
-
|
| 29 |
image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
| 30 |
-
return remove(image)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
original = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
| 35 |
-
processed = remove_background(input_image)
|
| 36 |
-
return (original, processed)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"examples/
|
|
|
|
|
|
|
|
|
|
| 44 |
"examples/5.jpg"
|
| 45 |
]
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
</div>
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
with gr.Row():
|
| 61 |
-
with gr.Column():
|
| 62 |
-
|
| 63 |
gr.Examples(
|
| 64 |
-
examples=
|
| 65 |
-
inputs=
|
| 66 |
label="Try example images ↓"
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from rembg import remove
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
+
custom_css = """
|
| 7 |
+
.button-gradient {
|
| 8 |
+
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
|
| 9 |
+
background-size: 400% 400%;
|
| 10 |
+
border: none;
|
| 11 |
+
padding: 14px 28px;
|
| 12 |
+
font-size: 16px;
|
| 13 |
+
font-weight: bold;
|
| 14 |
+
color: white;
|
| 15 |
+
border-radius: 10px;
|
| 16 |
+
cursor: pointer;
|
| 17 |
+
transition: 0.3s ease-in-out;
|
| 18 |
+
animation: gradientAnimation 2s infinite linear;
|
| 19 |
+
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
|
| 20 |
}
|
| 21 |
+
@keyframes gradientAnimation {
|
| 22 |
+
0% { background-position: 0% 50%; }
|
| 23 |
+
25% { background-position: 50% 100%; }
|
| 24 |
+
50% { background-position: 100% 50%; }
|
| 25 |
+
75% { background-position: 50% 0%; }
|
| 26 |
+
100% { background-position: 0% 50%; }
|
| 27 |
}
|
| 28 |
+
.button-gradient:hover {
|
| 29 |
+
transform: scale(1.05);
|
| 30 |
+
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
|
|
|
|
| 31 |
}
|
| 32 |
"""
|
| 33 |
|
| 34 |
def remove_background(input_image):
|
| 35 |
+
# Конвертируем изображение из Gradio в PIL
|
| 36 |
image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
|
|
|
| 37 |
|
| 38 |
+
# Удаляем фон с помощью U²-Net
|
| 39 |
+
output_image = remove(image)
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Конвертируем результат обратно в numpy array для Gradio
|
| 42 |
+
return np.array(output_image)
|
| 43 |
+
|
| 44 |
+
# Define examples as a list of lists: [source, target, enhance]
|
| 45 |
+
example_files = [
|
| 46 |
+
"examples/1.png",
|
| 47 |
+
"examples/2.png",
|
| 48 |
+
"examples/3.png",
|
| 49 |
+
"examples/4.jpg",
|
| 50 |
"examples/5.jpg"
|
| 51 |
]
|
| 52 |
|
| 53 |
+
# APP Interface
|
| 54 |
+
with gr.Blocks(css=custom_css) as app:
|
| 55 |
+
gr.Markdown(
|
| 56 |
+
"""
|
| 57 |
+
<a href="https://miniai.live" style="display: flex; align-items: center;">
|
| 58 |
+
<img src="https://miniai.live/wp-content/uploads/2024/02/logo_name-1-768x426-1.png" style="width: 18%; margin-right: 15px;"/>
|
| 59 |
+
<div>
|
| 60 |
+
<p style="font-size: 40px; font-weight: bold; margin-right: 20px;">Background Removal Pro</p>
|
| 61 |
+
</div>
|
| 62 |
+
</a>
|
| 63 |
+
<div style="display: flex; justify-content: center; align-items: center;">
|
| 64 |
+
<table style="text-align: center;">
|
| 65 |
+
<tr>
|
| 66 |
+
<td style="text-align: center; vertical-align: middle;"><a href="https://github.com/MiniAiLive"><img src="https://miniai.live/wp-content/uploads/2024/10/new_git-1-300x67.png" style="height: 50px; margin-right: 5px;" title="GITHUB"/></a></td>
|
| 67 |
+
<td style="text-align: center; vertical-align: middle;"><a href="https://huggingface.co/MiniAiLive"><img src="https://miniai.live/wp-content/uploads/2024/10/new_hugging-1-300x67.png" style="height: 50px; margin-right: 5px;" title="HuggingFace"/></a></td>
|
| 68 |
+
<td style="text-align: center; vertical-align: middle;"><a href="https://demo.miniai.live"><img src="https://miniai.live/wp-content/uploads/2024/10/new_gradio-300x67.png" style="height: 50px; margin-right: 5px;" title="Gradio"/></a></td>
|
| 69 |
+
</tr>
|
| 70 |
+
<tr>
|
| 71 |
+
<td style="text-align: center; vertical-align: middle;"><a href="https://docs.miniai.live/"><img src="https://miniai.live/wp-content/uploads/2024/10/a-300x70.png" style="height: 50px; margin-right: 5px;" title="Documentation"/></a></td>
|
| 72 |
+
<td style="text-align: center; vertical-align: middle;"><a href="https://www.youtube.com/@miniailive"><img src="https://miniai.live/wp-content/uploads/2024/10/Untitled-1-300x70.png" style="height: 50px; margin-right: 5px;" title="Youtube"/></a></td>
|
| 73 |
+
<td style="text-align: center; vertical-align: middle;"><a href="https://play.google.com/store/apps/dev?id=5831076207730531667"><img src="https://miniai.live/wp-content/uploads/2024/10/googleplay-300x62.png" style="height: 50px; margin-right: 5px;" title="Google Play"/></a></td>
|
| 74 |
+
</tr>
|
| 75 |
+
</table>
|
| 76 |
</div>
|
| 77 |
+
<br/>
|
| 78 |
+
"""
|
| 79 |
+
)
|
| 80 |
with gr.Row():
|
| 81 |
+
with gr.Column(scale=0.5):
|
| 82 |
+
im_input = gr.Image(height=400, label="Input Image")
|
| 83 |
gr.Examples(
|
| 84 |
+
examples=example_files,
|
| 85 |
+
inputs=im_input,
|
| 86 |
label="Try example images ↓"
|
| 87 |
)
|
| 88 |
+
btn_remove = gr.Button("Remove Background 🚀 🚀 🚀", elem_classes="button-gradient")
|
| 89 |
+
with gr.Column(scale=0.5):
|
| 90 |
+
im_output = gr.Image(height=400, label="Result")
|
| 91 |
|
| 92 |
+
# Connect the button
|
| 93 |
+
btn_remove.click(
|
| 94 |
+
fn=remove_background,
|
| 95 |
+
inputs=im_input,
|
| 96 |
+
outputs=im_output
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
if __name__ == "__main__":
|
| 100 |
+
app.launch()
|