Spaces:
Sleeping
Sleeping
美化UI界面:添加新布局和样式
Browse files
app.py
CHANGED
@@ -35,108 +35,24 @@ def detect_objects(image):
|
|
35 |
except Exception as e:
|
36 |
return None, f"Error: {str(e)}"
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
button_primary_text_color="white",
|
50 |
-
border_color_primary="*primary_300",
|
51 |
-
)
|
52 |
-
|
53 |
-
with gr.Blocks(theme=theme) as demo:
|
54 |
-
gr.Markdown(
|
55 |
-
"""
|
56 |
-
# 🐉 奶龙杀手 (NailongKiller)
|
57 |
-
|
58 |
-
这是一个基于 YOLO 的奶龙检测系统。上传图片即可自动检测图中的奶龙。
|
59 |
-
|
60 |
-
This is a YOLO-based Nailong detection system. Upload an image to detect Nailong automatically.
|
61 |
-
"""
|
62 |
-
)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
width=512,
|
71 |
-
)
|
72 |
-
|
73 |
-
with gr.Row():
|
74 |
-
clear_btn = gr.Button("清除 | Clear", variant="secondary", size="lg")
|
75 |
-
detect_btn = gr.Button("检测 | Detect", variant="primary", size="lg")
|
76 |
-
|
77 |
-
with gr.Column(scale=1):
|
78 |
-
output_image = gr.Image(
|
79 |
-
label="检测结果 | Detection Result",
|
80 |
-
height=512,
|
81 |
-
width=512,
|
82 |
-
)
|
83 |
-
result_text = gr.Textbox(
|
84 |
-
label="检测信息 | Detection Info",
|
85 |
-
placeholder="等待检测...",
|
86 |
-
)
|
87 |
-
|
88 |
-
gr.Markdown(
|
89 |
-
"""
|
90 |
-
### 📝 使用说明 | Instructions
|
91 |
-
|
92 |
-
1. 点击上传或拖拽图片到左侧输入区域
|
93 |
-
2. 点击"检测"按钮开始识别
|
94 |
-
3. 右侧将显示检测结果和统计信息
|
95 |
-
|
96 |
-
### ⚠️ 注意事项 | Notes
|
97 |
-
|
98 |
-
- 支持常见图片格式 (jpg, png, etc.)
|
99 |
-
- 建议上传清晰的图片以获得更好的检测效果
|
100 |
-
- 图片会自动调整大小以优化性能
|
101 |
-
|
102 |
-
### 🔗 相关链接 | Links
|
103 |
-
|
104 |
-
- [项目地址 | Project Repository](https://huggingface.co/spaces/Hakureirm/NailongKiller)
|
105 |
-
- [YOLO Documentation](https://docs.ultralytics.com/)
|
106 |
-
"""
|
107 |
-
)
|
108 |
-
|
109 |
-
# 事件处理
|
110 |
-
detect_btn.click(
|
111 |
-
fn=detect_objects,
|
112 |
-
inputs=input_image,
|
113 |
-
outputs=[output_image, result_text],
|
114 |
-
)
|
115 |
-
|
116 |
-
clear_btn.click(
|
117 |
-
lambda: (None, None, None),
|
118 |
-
outputs=[input_image, output_image, result_text],
|
119 |
-
)
|
120 |
-
|
121 |
-
# 修改示例部分的代码
|
122 |
-
# 使用try-except来处理示例图片
|
123 |
-
try:
|
124 |
-
examples = []
|
125 |
-
if os.path.exists("example1.jpg"):
|
126 |
-
examples.append("example1.jpg")
|
127 |
-
if os.path.exists("example2.jpg"):
|
128 |
-
examples.append("example2.jpg")
|
129 |
-
|
130 |
-
if examples: # 只有在有示例图片时才添加Examples组件
|
131 |
-
gr.Examples(
|
132 |
-
examples=examples,
|
133 |
-
inputs=input_image,
|
134 |
-
outputs=[output_image, result_text],
|
135 |
-
fn=detect_objects,
|
136 |
-
cache_examples=True,
|
137 |
-
)
|
138 |
-
except Exception as e:
|
139 |
-
print(f"Warning: Could not load example images: {e}")
|
140 |
|
141 |
# API端点
|
142 |
@app.post("/detect/")
|
|
|
35 |
except Exception as e:
|
36 |
return None, f"Error: {str(e)}"
|
37 |
|
38 |
+
# 创建Gradio界面
|
39 |
+
demo = gr.Interface(
|
40 |
+
fn=detect_objects,
|
41 |
+
inputs=gr.Image(type="numpy", label="输入图片 | Input Image"),
|
42 |
+
outputs=[
|
43 |
+
gr.Image(type="numpy", label="检测结果 | Detection Result"),
|
44 |
+
gr.Textbox(label="检测信息 | Detection Info")
|
45 |
+
],
|
46 |
+
title="🐉 奶龙杀手 (NailongKiller)",
|
47 |
+
description="""
|
48 |
+
这是一个基于 YOLO 的奶龙检测系统。上传图片即可自动检测图中的奶龙。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
This is a YOLO-based Nailong detection system. Upload an image to detect Nailong automatically.
|
51 |
+
""",
|
52 |
+
theme=gr.themes.Default(),
|
53 |
+
allow_flagging="never",
|
54 |
+
examples=["example1.jpg", "example2.jpg"] if all(os.path.exists(f) for f in ["example1.jpg", "example2.jpg"]) else None
|
55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# API端点
|
58 |
@app.post("/detect/")
|