jinyin_chen
commited on
Commit
·
4334a37
1
Parent(s):
e8b0040
ui
Browse files
app.py
CHANGED
@@ -1,20 +1,31 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
def
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
inputs =
|
9 |
-
gr.inputs.Textbox(label="Your Name"),
|
10 |
-
gr.inputs.Image(label="Your Image")
|
11 |
-
]
|
12 |
|
13 |
-
#
|
14 |
outputs = gr.outputs.Textbox()
|
15 |
|
16 |
# 创建 Interface 对象,设置 live=False 以添加提交按钮
|
17 |
-
demo = gr.Interface(fn=
|
18 |
|
19 |
# 启动界面
|
20 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
|
4 |
+
def process_image(image):
|
5 |
+
# 设置保存图片的路径,指向当前工程的 images 目录
|
6 |
+
save_path = os.path.join(os.getcwd(), "images")
|
7 |
+
# 确保目录存在
|
8 |
+
os.makedirs(save_path, exist_ok=True)
|
9 |
+
|
10 |
+
# 为每个上传的图片生成唯一的文件名
|
11 |
+
from datetime import datetime
|
12 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
13 |
+
image_filename = f"uploaded_{timestamp}.png"
|
14 |
+
image_path = os.path.join(save_path, image_filename)
|
15 |
+
|
16 |
+
# 保存图片
|
17 |
+
image.save(image_path)
|
18 |
+
|
19 |
+
return f"图片已保存至 {image_path}" # 返回保存的位置信息
|
20 |
|
21 |
+
# 定义输入:一个图片选择框
|
22 |
+
inputs = gr.inputs.Image(label="Upload Image", type="pil")
|
|
|
|
|
|
|
23 |
|
24 |
+
# 定义输出:一个文本框,用来显示确认信息或处理结果
|
25 |
outputs = gr.outputs.Textbox()
|
26 |
|
27 |
# 创建 Interface 对象,设置 live=False 以添加提交按钮
|
28 |
+
demo = gr.Interface(fn=process_image, inputs=inputs, outputs=outputs, live=False)
|
29 |
|
30 |
# 启动界面
|
31 |
demo.launch()
|