import gradio as gr from rembg import remove from PIL import Image import numpy as np custom_css = """ .button-gradient { background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c); background-size: 400% 400%; border: none; padding: 14px 28px; font-size: 16px; font-weight: bold; color: white; border-radius: 10px; cursor: pointer; transition: 0.3s ease-in-out; animation: gradientAnimation 2s infinite linear; box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6); } @keyframes gradientAnimation { 0% { background-position: 0% 50%; } 25% { background-position: 50% 100%; } 50% { background-position: 100% 50%; } 75% { background-position: 50% 0%; } 100% { background-position: 0% 50%; } } .button-gradient:hover { transform: scale(1.05); box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8); } """ def remove_background(input_image): # Конвертируем изображение из Gradio в PIL image = Image.fromarray(input_image.astype('uint8'), 'RGB') # Удаляем фон с помощью U²-Net output_image = remove(image) # Конвертируем результат обратно в numpy array для Gradio return np.array(output_image) # Define examples as a list of lists: [source, target, enhance] example_files = [ "examples/1.png", "examples/2.png", "examples/3.png", "examples/4.jpg", "examples/5.jpg" ] # APP Interface with gr.Blocks(css=custom_css) as app: # Header with logo and title gr.Markdown( """

Background Removal Pro


""" ) # Main Interface with gr.Row(): with gr.Column(scale=0.5): im_input = gr.Image(height=400, label="Input Image") gr.Examples( examples=example_files, inputs=im_input, label="Try example images ↓" ) btn_remove = gr.Button("Remove Background 🚀 🚀 🚀", elem_classes="button-gradient") with gr.Column(scale=0.5): im_output = gr.Image(height=400, label="Result") # Connect the button btn_remove.click( fn=remove_background, inputs=im_input, outputs=im_output ) if __name__ == "__main__": app.launch()