Spaces:
Running
on
Zero
Running
on
Zero
美化UI界面:添加新布局和样式
Browse files
app.py
CHANGED
@@ -15,94 +15,117 @@ model = YOLO("NailongKiller.yolo11n.pt")
|
|
15 |
|
16 |
def detect_objects(image):
|
17 |
if image is None:
|
18 |
-
return None
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
border: none !important;
|
43 |
-
}
|
44 |
-
.gr-button:hover {
|
45 |
-
transform: translateY(-2px);
|
46 |
-
box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
|
47 |
-
}
|
48 |
-
"""
|
49 |
|
50 |
-
|
51 |
-
with gr.Blocks(css=custom_css) as demo:
|
52 |
gr.Markdown(
|
53 |
"""
|
54 |
-
# 🐉 奶龙杀手 (
|
55 |
-
|
|
|
|
|
|
|
56 |
"""
|
57 |
)
|
58 |
|
59 |
with gr.Row():
|
60 |
-
with gr.Column():
|
61 |
-
# 移除了 tool 参数
|
62 |
input_image = gr.Image(
|
63 |
-
label="
|
64 |
-
type="numpy"
|
|
|
|
|
65 |
)
|
66 |
-
with gr.Row():
|
67 |
-
clear_btn = gr.Button("清除 (Clear)", variant="secondary")
|
68 |
-
submit_btn = gr.Button("检测 (Detect)", variant="primary")
|
69 |
-
|
70 |
-
with gr.Column():
|
71 |
-
output_image = gr.Image(label="检测结果 (Detection Result)")
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
gr.Markdown(
|
74 |
"""
|
75 |
-
### 使用说明
|
76 |
-
|
|
|
77 |
2. 点击"检测"按钮开始识别
|
78 |
-
3.
|
|
|
|
|
79 |
|
80 |
-
### 注意事项 (Notes):
|
81 |
- 支持常见图片格式 (jpg, png, etc.)
|
82 |
- 建议上传清晰的图片以获得更好的检测效果
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
"""
|
84 |
)
|
85 |
|
86 |
# 事件处理
|
87 |
-
|
88 |
fn=detect_objects,
|
89 |
inputs=input_image,
|
90 |
-
outputs=output_image
|
91 |
)
|
92 |
|
93 |
clear_btn.click(
|
94 |
-
lambda: (None, None),
|
95 |
-
outputs=[input_image, output_image]
|
96 |
)
|
97 |
|
98 |
# 添加示例
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
|
107 |
# API端点
|
108 |
@app.post("/detect/")
|
|
|
15 |
|
16 |
def detect_objects(image):
|
17 |
if image is None:
|
18 |
+
return None, "No image provided"
|
19 |
|
20 |
+
try:
|
21 |
+
# 运行推理
|
22 |
+
results = model(image)
|
23 |
+
result = results[0]
|
24 |
+
|
25 |
+
# 在图像上绘制检测框
|
26 |
+
annotated_image = result.plot()
|
27 |
+
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
28 |
+
|
29 |
+
# 获取检测结果统计
|
30 |
+
num_detections = len(result.boxes)
|
31 |
+
detection_info = f"检测到 {num_detections} 个目标"
|
32 |
+
|
33 |
+
return annotated_image, detection_info
|
34 |
+
except Exception as e:
|
35 |
+
return None, f"Error: {str(e)}"
|
36 |
|
37 |
+
# 创建主题和样式
|
38 |
+
theme = gr.themes.Soft(
|
39 |
+
primary_hue="indigo",
|
40 |
+
secondary_hue="blue",
|
41 |
+
).set(
|
42 |
+
body_background_fill="*neutral_50",
|
43 |
+
block_background_fill="*neutral_100",
|
44 |
+
block_label_background_fill="*primary_100",
|
45 |
+
block_label_text_color="*primary_500",
|
46 |
+
button_primary_background_fill="*primary_500",
|
47 |
+
button_primary_background_fill_hover="*primary_600",
|
48 |
+
button_primary_text_color="white",
|
49 |
+
border_color_primary="*primary_300",
|
50 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
with gr.Blocks(theme=theme) as demo:
|
|
|
53 |
gr.Markdown(
|
54 |
"""
|
55 |
+
# 🐉 奶龙杀手 (NailongKiller)
|
56 |
+
|
57 |
+
这是一个基于 YOLO 的奶龙检测系统。上传图片即可自动检测图中的奶龙。
|
58 |
+
|
59 |
+
This is a YOLO-based Nailong detection system. Upload an image to detect Nailong automatically.
|
60 |
"""
|
61 |
)
|
62 |
|
63 |
with gr.Row():
|
64 |
+
with gr.Column(scale=1):
|
|
|
65 |
input_image = gr.Image(
|
66 |
+
label="输入图片 | Input Image",
|
67 |
+
type="numpy",
|
68 |
+
height=512,
|
69 |
+
width=512,
|
70 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
with gr.Row():
|
73 |
+
clear_btn = gr.Button("清除 | Clear", variant="secondary", size="lg")
|
74 |
+
detect_btn = gr.Button("检测 | Detect", variant="primary", size="lg")
|
75 |
+
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
output_image = gr.Image(
|
78 |
+
label="检测结果 | Detection Result",
|
79 |
+
height=512,
|
80 |
+
width=512,
|
81 |
+
)
|
82 |
+
result_text = gr.Textbox(
|
83 |
+
label="检测信息 | Detection Info",
|
84 |
+
placeholder="等待检测...",
|
85 |
+
)
|
86 |
+
|
87 |
gr.Markdown(
|
88 |
"""
|
89 |
+
### 📝 使用说明 | Instructions
|
90 |
+
|
91 |
+
1. 点击上传或拖拽图片到左侧输入区域
|
92 |
2. 点击"检测"按钮开始识别
|
93 |
+
3. 右侧将显示检测结果和统计信息
|
94 |
+
|
95 |
+
### ⚠️ 注意事项 | Notes
|
96 |
|
|
|
97 |
- 支持常见图片格式 (jpg, png, etc.)
|
98 |
- 建议上传清晰的图片以获得更好的检测效果
|
99 |
+
- 图片会自动调整大小以优化性能
|
100 |
+
|
101 |
+
### 🔗 相关链接 | Links
|
102 |
+
|
103 |
+
- [项目地址 | Project Repository](https://huggingface.co/spaces/Hakureirm/NailongKiller)
|
104 |
+
- [YOLO Documentation](https://docs.ultralytics.com/)
|
105 |
"""
|
106 |
)
|
107 |
|
108 |
# 事件处理
|
109 |
+
detect_btn.click(
|
110 |
fn=detect_objects,
|
111 |
inputs=input_image,
|
112 |
+
outputs=[output_image, result_text],
|
113 |
)
|
114 |
|
115 |
clear_btn.click(
|
116 |
+
lambda: (None, None, None),
|
117 |
+
outputs=[input_image, output_image, result_text],
|
118 |
)
|
119 |
|
120 |
# 添加示例
|
121 |
+
if os.path.exists("example1.jpg") and os.path.exists("example2.jpg"):
|
122 |
+
gr.Examples(
|
123 |
+
examples=["example1.jpg", "example2.jpg"],
|
124 |
+
inputs=input_image,
|
125 |
+
outputs=[output_image, result_text],
|
126 |
+
fn=detect_objects,
|
127 |
+
cache_examples=True,
|
128 |
+
)
|
129 |
|
130 |
# API端点
|
131 |
@app.post("/detect/")
|