|
import gradio as gr |
|
import os |
|
from PIL import Image |
|
from torchkeras import plots |
|
from torchkeras.data import get_url_img |
|
from typing import List |
|
|
|
|
|
UPLOAD_FOLDER = "app_src/input" |
|
if not os.path.exists(UPLOAD_FOLDER): |
|
os.makedirs(UPLOAD_FOLDER) |
|
|
|
|
|
DISPLAY_IMAGE_PATH = "app_src/output/test_1_seg_result.png" |
|
|
|
|
|
def input_fn(img, text): |
|
if isinstance(img,str): |
|
try: |
|
img = get_url_img(img) if img.startswith('http') else Image.open(img).convert('RGB') |
|
except: |
|
raise gr.Error("Failed to get image from URL, please upload from local.") |
|
|
|
image_path = os.path.join(UPLOAD_FOLDER, 'input.png') |
|
img.save(image_path) |
|
output_data = output_fn(text) |
|
return output_data |
|
|
|
|
|
def output_fn(text): |
|
|
|
img = Image.open(DISPLAY_IMAGE_PATH).convert('RGB') |
|
info = {"size": img.size, "format": img.format} |
|
text = "<center><h1><span style='color:green'>Successful detection! 🎉</span></h1></center>" |
|
return img, text |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
flag=0 |
|
with gr.Blocks() as demo: |
|
gr.Markdown("# TOIST v2") |
|
with gr.Row(): |
|
with gr.Tab("Camera"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
camera = gr.Image(source='webcam',type='pil').style( height=300) |
|
text = gr.inputs.Textbox(label="Please enter prompt, what do you want to do ?"), |
|
camera_button = gr.Button("Submit",variant="primary") |
|
|
|
with gr.Tab("Url"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
default_url = '' |
|
url = gr.Textbox(value=default_url) |
|
text = gr.Textbox(label="Please enter prompt, what do you want to do ?") |
|
url_button = gr.Button("Submit",variant="primary") |
|
|
|
|
|
with gr.Tab("Upload"): |
|
with gr.Row(): |
|
with gr.Column(equal_weight=True): |
|
input_img = gr.Image(type='pil').style( height=300) |
|
text = gr.Textbox(label="Please enter prompt, what do you want to do ?") |
|
img_button = gr.Button("Submit",variant="primary") |
|
|
|
|
|
|
|
with gr.Column(equal_weight=True): |
|
gr.Markdown("<h3> Output </h3>") |
|
output = [ |
|
gr.Image(type='pil', label='Image').style(height=400, align="center"), |
|
|
|
gr.Markdown() |
|
|
|
] |
|
|
|
camera_button.click(input_fn, |
|
[camera,text], |
|
output |
|
) |
|
url_button.click(input_fn, |
|
[url,text], |
|
output |
|
) |
|
img_button.click(input_fn, |
|
[input_img,text], |
|
output |
|
) |
|
examples = gr.Examples([[UPLOAD_FOLDER+'/exp1.png','sit on something'], |
|
[UPLOAD_FOLDER+'/exp2.png','sit on something']], |
|
inputs=[input_img,text], |
|
outputs=output, |
|
fn = input_fn) |
|
|
|
gr.close_all() |
|
demo.queue() |
|
demo.launch() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|