Upload 6 files
Browse files- app.py +114 -0
- app_src/input/exp1.png +0 -0
- app_src/input/exp2.png +0 -0
- app_src/input/input.png +0 -0
- app_src/input/test_1.png +0 -0
- app_src/output/test_1_seg_result.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from torchkeras import plots
|
| 5 |
+
from torchkeras.data import get_url_img
|
| 6 |
+
from typing import List
|
| 7 |
+
|
| 8 |
+
# 定义需要保存上传图像的文件夹路径
|
| 9 |
+
UPLOAD_FOLDER = "app_src/input"
|
| 10 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
| 11 |
+
os.makedirs(UPLOAD_FOLDER)
|
| 12 |
+
|
| 13 |
+
# 定义需要显示的图像文件路径
|
| 14 |
+
DISPLAY_IMAGE_PATH = "app_src/output/test_1_seg_result.png"
|
| 15 |
+
|
| 16 |
+
# 输入图像和文本的接口
|
| 17 |
+
def input_fn(img, text):
|
| 18 |
+
if isinstance(img,str):
|
| 19 |
+
try:
|
| 20 |
+
img = get_url_img(img) if img.startswith('http') else Image.open(img).convert('RGB')
|
| 21 |
+
except:
|
| 22 |
+
raise gr.Error("Failed to get image from URL, please upload from local.")
|
| 23 |
+
|
| 24 |
+
image_path = os.path.join(UPLOAD_FOLDER, 'input.png')
|
| 25 |
+
img.save(image_path)
|
| 26 |
+
output_data = output_fn(text)
|
| 27 |
+
return output_data
|
| 28 |
+
|
| 29 |
+
# 输出图像的接口
|
| 30 |
+
def output_fn(text):
|
| 31 |
+
# 返回指定路径的图像
|
| 32 |
+
img = Image.open(DISPLAY_IMAGE_PATH).convert('RGB')
|
| 33 |
+
info = {"size": img.size, "format": img.format}
|
| 34 |
+
text = "<center><h1><span style='color:green'>Successful detection! 🎉</span></h1></center>"
|
| 35 |
+
return img, text
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
# 创建 Gradio 接口
|
| 40 |
+
flag=0
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# TOIST v2")
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Tab("Camera"):
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
camera = gr.Image(source='webcam',type='pil').style( height=300)
|
| 48 |
+
text = gr.inputs.Textbox(label="Please enter prompt, what do you want to do ?"), # 文本输入组件
|
| 49 |
+
camera_button = gr.Button("Submit",variant="primary")
|
| 50 |
+
|
| 51 |
+
with gr.Tab("Url"):
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
default_url = ''
|
| 55 |
+
url = gr.Textbox(value=default_url)
|
| 56 |
+
text = gr.Textbox(label="Please enter prompt, what do you want to do ?") # 文本输入组件
|
| 57 |
+
url_button = gr.Button("Submit",variant="primary")
|
| 58 |
+
# examples = gr.Examples = [UPLOAD_FOLDER+'exp1.png',UPLOAD_FOLDER+'exp2.png']
|
| 59 |
+
|
| 60 |
+
with gr.Tab("Upload"):
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column(equal_weight=True):
|
| 63 |
+
input_img = gr.Image(type='pil').style( height=300)
|
| 64 |
+
text = gr.Textbox(label="Please enter prompt, what do you want to do ?") # 文本输入组件
|
| 65 |
+
img_button = gr.Button("Submit",variant="primary")
|
| 66 |
+
# examples = gr.Examples(examples=[UPLOAD_FOLDER+'exp1.png',UPLOAD_FOLDER+'exp2.png'])
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
with gr.Column(equal_weight=True):
|
| 70 |
+
gr.Markdown("<h3> Output </h3>")
|
| 71 |
+
output = [
|
| 72 |
+
gr.Image(type='pil', label='Image').style(height=400, align="center"),
|
| 73 |
+
# gr.Textbox(label="Successful detection!<span style='color: green'>😀</span><br> 🎉 \nYour prompt: ", type='text'),
|
| 74 |
+
gr.Markdown()
|
| 75 |
+
# gr.JSON(type='text')
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
camera_button.click(input_fn,
|
| 79 |
+
[camera,text],
|
| 80 |
+
output
|
| 81 |
+
)
|
| 82 |
+
url_button.click(input_fn,
|
| 83 |
+
[url,text],
|
| 84 |
+
output
|
| 85 |
+
)
|
| 86 |
+
img_button.click(input_fn,
|
| 87 |
+
[input_img,text],
|
| 88 |
+
output
|
| 89 |
+
)
|
| 90 |
+
examples = gr.Examples([[UPLOAD_FOLDER+'/exp1.png','sit on something'],
|
| 91 |
+
[UPLOAD_FOLDER+'/exp2.png','sit on something']],
|
| 92 |
+
inputs=[input_img,text],
|
| 93 |
+
outputs=output,
|
| 94 |
+
fn = input_fn)
|
| 95 |
+
|
| 96 |
+
gr.close_all()
|
| 97 |
+
demo.queue()
|
| 98 |
+
demo.launch()
|
| 99 |
+
|
| 100 |
+
# gr.Interface(
|
| 101 |
+
# fn=input_fn, # 输出函数
|
| 102 |
+
# inputs=[
|
| 103 |
+
# gr.inputs.Image(label="upload your image", type="pil" ,input_size=(256, 256)), # 图像输入组件
|
| 104 |
+
# gr.inputs.Textbox(label="Please enter prompt, what do you want to do ?"), # 文本输入组件
|
| 105 |
+
# ],
|
| 106 |
+
# outputs=gr.outputs.Image(type="pil",image_size=(256, 256)), # 图像输出组件
|
| 107 |
+
# title="TOIST v2",
|
| 108 |
+
# description="Experience TOIST v2",
|
| 109 |
+
# theme="github", # 采用 GitHub 风格
|
| 110 |
+
# server_name="0.0.0.0", # 指定服务器地址
|
| 111 |
+
# server_port=7878, # 指定服务器端口
|
| 112 |
+
# # auth=gr.auth.LoginRequired(authenticate), # 需要登录验证
|
| 113 |
+
# ).launch()
|
| 114 |
+
|
app_src/input/exp1.png
ADDED
|
app_src/input/exp2.png
ADDED
|
app_src/input/input.png
ADDED
|
app_src/input/test_1.png
ADDED
|
app_src/output/test_1_seg_result.png
ADDED
|