Spaces:
Sleeping
Sleeping
| # hugging face requirements for app.py, main file for running the application | |
| # equivalent to end.py to be used in the hugging face inference API,which Hugging Face will recognize as the main file for running application. | |
| # app.py | |
| import sys | |
| import os | |
| sys.path.append(os.path.dirname(os.path.abspath(__file__))) | |
| import gradio as gr | |
| from PIL import Image | |
| from deeplab_demo import get_people | |
| from creat_anaglyph import insert_person_to_stereo_gradio | |
| import torch | |
| from torchvision.transforms import ToPILImage | |
| # Define functions to process the person image and generate the anaglyph image | |
| def process_person_image(person_image): | |
| masked_image_pil, grid_image = get_people(person_image) | |
| if isinstance(masked_image_pil, torch.Tensor): | |
| masked_image_pil = ToPILImage()(masked_image_pil) | |
| if isinstance(grid_image, torch.Tensor): | |
| grid_image = ToPILImage()(grid_image) | |
| return masked_image_pil, grid_image | |
| # Define a function to generate the anaglyph image | |
| def generate_anaglyph(masked_image_pil, scenery_image, depth_option, custom_disparity): | |
| # Define default disparities for non-custom options | |
| # non-custom options: close, medium, far | |
| depth_disparities = { | |
| "close": 10, # Adjust values as needed | |
| "medium": 5, | |
| "far": 2 | |
| } | |
| # Use custom_disparity only if depth_option is "custom" | |
| disparity = custom_disparity if depth_option == "custom" else depth_disparities.get(depth_option, 5) | |
| # Ensure input is PIL image | |
| if isinstance(masked_image_pil, torch.Tensor): | |
| masked_image_pil = ToPILImage()(masked_image_pil) | |
| if isinstance(scenery_image, torch.Tensor): | |
| scenery_image = ToPILImage()(scenery_image) | |
| anaglyph_image = insert_person_to_stereo_gradio(scenery_image, masked_image_pil, disparity) | |
| if isinstance(anaglyph_image, torch.Tensor): | |
| anaglyph_image = ToPILImage()(anaglyph_image) | |
| return anaglyph_image | |
| # Create Gradio interface | |
| with gr.Blocks() as iface: | |
| with gr.Row(): | |
| person_image_input = gr.Image(type="pil", label="Character image") | |
| scenery_image_input = gr.Image(type="pil", label="Landscape images") | |
| depth_option_input = gr.Dropdown(choices=["close", "medium", "far", "custom"], label="Depth Options") | |
| custom_disparity_input = gr.Slider(minimum=0, maximum=50, step=1, label="Custom Depth Disparity", visible=False) | |
| with gr.Row(): | |
| grid_image_output = gr.Image(type="pil", label="Grid", interactive=False) | |
| masked_image_output = gr.Image(type="pil", label="Masked", interactive=False) | |
| anaglyph_image_output = gr.Image(type="pil", label="Anaglyph", interactive=False) | |
| # button1: Process the character image | |
| process_button = gr.Button("Processing human images") | |
| process_button.click( | |
| fn=process_person_image, | |
| inputs=person_image_input, | |
| outputs=[masked_image_output, grid_image_output] | |
| ) | |
| # define a function to update the visibility of the custom disparity slider based on the depth option | |
| def update_custom_slider_visibility(depth_option): | |
| return gr.update(visible=(depth_option == "custom")) | |
| depth_option_input.change( | |
| fn=update_custom_slider_visibility, | |
| inputs=[depth_option_input], | |
| outputs=custom_disparity_input | |
| ) | |
| # button2: Generate anaglyph image | |
| generate_button = gr.Button("Generate Anaglyph Image") | |
| generate_button.click( | |
| fn=generate_anaglyph, | |
| inputs=[masked_image_output, scenery_image_input, depth_option_input, custom_disparity_input], | |
| outputs=anaglyph_image_output | |
| ) | |
| # Launch the Gradio interface | |
| iface.launch() | |