ishworrsubedii's picture
Upload 42 files
0f25777 verified
"""
Created By: ishwor subedi
Date: 2024-05-19
"""
from PIL import Image
import numpy as np
import gradio as gr
import os
import replicate
from supabase import create_client
def example_inference(image_bytes):
output = replicate.run(
"cjwbw/rembg:fb8af171cfa1616ddcf1242c093f9c46bcada5ad4cf6f2fbe8b81b330ec5c003",
input={
"image": "https://replicate.delivery/pbxt/Ho28olmw8dnOffOz7yjuPK6UGsOPqFUfpCnq1ur8zaAKxiPH/animal-1.jpeg"
}
)
return output[0]
original_image, binary_image = None, None
colors = [Image.open(path) for path in
[os.path.join("bg_images/color", file) for file in os.listdir("bg_images/color")]]
houses = [Image.open(path) for path in
[os.path.join("bg_images/house", file) for file in os.listdir("bg_images/house")]]
natures = [Image.open(path) for path in
[os.path.join("bg_images/nature", file) for file in os.listdir("bg_images/nature")]]
studios = [Image.open(path) for path in
[os.path.join("bg_images/studio", file) for file in os.listdir("bg_images/studio")]]
walls = [Image.open(path) for path in [os.path.join("bg_images/wall", file) for file in os.listdir("bg_images/wall")]]
woods = [Image.open(path) for path in [os.path.join("bg_images/wood", file) for file in os.listdir("bg_images/wood")]]
with gr.Blocks(
theme=gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.indigo)) as demo:
with gr.Row():
input_img = gr.Image(label="Input", interactive=True, type='pil')
hidden_img = gr.Image(label="Chosen Background", visible=False)
output_img = gr.Image(label="Output", interactive=False, type='pil')
def clearFunc():
global original_image
global binary_image
def update_visibility():
return gr.Image(visible=True)
torch.cuda.empty_cache()
gc.collect()
return gr.Image(visible=False, value=None)
with gr.Row():
examples = gr.Examples(examples=studios, inputs=[hidden_img], label="Studio Backgrounds")
with gr.Row():
examples6 = gr.Examples(examples=colors, inputs=[hidden_img], label="Color Backgrounds")
with gr.Row():
examples2 = gr.Examples(examples=walls, inputs=[hidden_img], label="Wall Backgrounds")
examples3 = gr.Examples(examples=natures, inputs=[hidden_img], label="Nature Backgrounds")
with gr.Row():
examples4 = gr.Examples(examples=houses, inputs=[hidden_img], label="House Backgrounds")
examples5 = gr.Examples(examples=woods, inputs=[hidden_img], label="Wood Backgrounds")
with gr.Row():
submit = gr.Button("Submit")
clear = gr.ClearButton(components=[input_img, output_img, hidden_img], value="Reset", variant="stop")
def generate_img(image, background):
orig_img = example_inference(image)
width, height = orig_img.size
background = Image.fromarray(background).resize((width, height))
orig_img = Image.fromarray(np.array(orig_img)).resize((width, height))
background.paste(orig_img, (0, 0), mask=orig_img)
return background
hidden_img.change(fn=update_visibility, inputs=[], outputs=[hidden_img])
submit.click(generate_img, inputs=[input_img, hidden_img], outputs=[output_img])
clear.click(fn=clearFunc, inputs=[], outputs=[hidden_img])
demo.launch(share=True, debug=True)