Spaces:
Sleeping
Sleeping
File size: 1,805 Bytes
29fa6f4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# CS5330 Lab 4
# This file builds the Gradio interface
import gradio as gr
from segmentation import extract_person
from insert import Background, Disparity, insert_person_in_pair
from anaglyph import create_anaglyph
# Gradio interface function
def gradio_interface(person_image, background, disparity):
# Gradio dropdown menu does not accept enum as a type
# So convert str to enum
if background == 'Nature':
background_arg = Background.NATURE
elif background == 'NEU':
background_arg = Background.NEU
else:
background_arg = Background.GASTOWN
if disparity == 'Close':
disparity_arg = Disparity.CLOSE
elif disparity == 'Medium':
disparity_arg = Disparity.MED
else:
disparity_arg = Disparity.FAR
person_arg = extract_person(person_image)
# If there's no person detected, raise an error message
if not person_arg:
return None, 'Error: No person detected in the image.'
left_image, right_image = insert_person_in_pair(background_arg, extract_person(person_image), disparity_arg)
anaglyph = create_anaglyph(left_image, right_image)
return anaglyph, 'Successfully generated the anaglyph image.'
demo = gr.Interface(
fn=gradio_interface,
inputs=[gr.Image(type='pil', label='Upload an image of a person'),
gr.Dropdown(choices=['Nature', 'NEU', 'Gastown'], label='Choose background'),
gr.Dropdown(choices=['Close', 'Medium', 'Far'], label='Choose disparity')],
outputs=[gr.Image(type='pil', label='Anaglyph Image'),
gr.Textbox(label='Message')],
title='Anaglyph Generator',
description='Upload an image of a person, then choose a background image and disparity to determine how the anaglyph is generated.'
)
demo.launch(share=True) |