Spaces:
Sleeping
Sleeping
美化UI界面:添加新布局和样式
Browse files
app.py
CHANGED
@@ -14,56 +14,117 @@ app = FastAPI()
|
|
14 |
model = YOLO("NailongKiller.yolo11n.pt")
|
15 |
|
16 |
def detect_objects(image):
|
|
|
|
|
|
|
17 |
# 运行推理
|
18 |
results = model(image)
|
19 |
-
|
20 |
-
# 获取第一个结果
|
21 |
result = results[0]
|
22 |
-
|
23 |
-
# 在图像上绘制检测框
|
24 |
annotated_image = result.plot()
|
25 |
-
|
26 |
-
# 转换为RGB格式
|
27 |
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
28 |
|
29 |
return annotated_image
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# API端点
|
32 |
@app.post("/detect/")
|
33 |
async def detect_api(file: UploadFile = File(...)):
|
34 |
-
# 读取上传的图片
|
35 |
contents = await file.read()
|
36 |
image = Image.open(io.BytesIO(contents))
|
37 |
-
|
38 |
-
# 转换为numpy数组
|
39 |
image_np = np.array(image)
|
40 |
|
41 |
-
# 运行推理
|
42 |
results = model(image_np)
|
43 |
result = results[0]
|
44 |
|
45 |
-
# 获取检测结果
|
46 |
detections = []
|
47 |
for box in result.boxes:
|
48 |
detection = {
|
49 |
-
"bbox": box.xyxy[0].tolist(),
|
50 |
-
"confidence": float(box.conf[0]),
|
51 |
-
"class": int(box.cls[0])
|
52 |
}
|
53 |
detections.append(detection)
|
54 |
|
55 |
return {"detections": detections}
|
56 |
|
57 |
-
# 创建Gradio界面
|
58 |
-
demo = gr.Interface(
|
59 |
-
fn=detect_objects,
|
60 |
-
inputs=gr.Image(type="numpy"),
|
61 |
-
outputs=gr.Image(),
|
62 |
-
title="奶龙杀手 (Nailong Killer)",
|
63 |
-
description="上传图片来检测奶龙 (Upload an image to detect Nailong)",
|
64 |
-
examples=["example1.jpg", "example2.jpg"]
|
65 |
-
)
|
66 |
-
|
67 |
# 将Gradio接口挂载到FastAPI
|
68 |
app = gr.mount_gradio_app(app, demo, path="/")
|
69 |
|
|
|
14 |
model = YOLO("NailongKiller.yolo11n.pt")
|
15 |
|
16 |
def detect_objects(image):
|
17 |
+
if image is None:
|
18 |
+
return None
|
19 |
+
|
20 |
# 运行推理
|
21 |
results = model(image)
|
|
|
|
|
22 |
result = results[0]
|
|
|
|
|
23 |
annotated_image = result.plot()
|
|
|
|
|
24 |
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
25 |
|
26 |
return annotated_image
|
27 |
|
28 |
+
# 创建自定义CSS
|
29 |
+
custom_css = """
|
30 |
+
#component-0 {
|
31 |
+
max-width: 900px;
|
32 |
+
margin: auto;
|
33 |
+
padding: 20px;
|
34 |
+
border-radius: 15px;
|
35 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
36 |
+
}
|
37 |
+
.gradio-container {
|
38 |
+
font-family: 'Source Sans Pro', sans-serif;
|
39 |
+
}
|
40 |
+
.gr-button {
|
41 |
+
background: linear-gradient(45deg, #FF6B6B, #FF8E53) !important;
|
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 |
+
# 创建Gradio界面
|
51 |
+
with gr.Blocks(css=custom_css) as demo:
|
52 |
+
gr.Markdown(
|
53 |
+
"""
|
54 |
+
# 🐉 奶龙杀手 (Nailong Killer)
|
55 |
+
## 一个基于YOLO的奶龙检测系统
|
56 |
+
"""
|
57 |
+
)
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column():
|
61 |
+
input_image = gr.Image(
|
62 |
+
label="上传图片 (Upload Image)",
|
63 |
+
type="numpy",
|
64 |
+
tool="select"
|
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 |
+
### 使用说明 (Instructions):
|
76 |
+
1. 点击上传或拖拽图片到左侧区域
|
77 |
+
2. 点击"检测"按钮开始识别
|
78 |
+
3. 右侧将显示检测结果
|
79 |
+
|
80 |
+
### 注意事项 (Notes):
|
81 |
+
- 支持常见图片格式 (jpg, png, etc.)
|
82 |
+
- 建议上传清晰的图片以获得更好的检测效果
|
83 |
+
"""
|
84 |
+
)
|
85 |
+
|
86 |
+
# 事件处理
|
87 |
+
submit_btn.click(
|
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 |
+
gr.Examples(
|
100 |
+
examples=["example1.jpg", "example2.jpg"],
|
101 |
+
inputs=input_image,
|
102 |
+
outputs=output_image,
|
103 |
+
fn=detect_objects,
|
104 |
+
cache_examples=True
|
105 |
+
)
|
106 |
+
|
107 |
# API端点
|
108 |
@app.post("/detect/")
|
109 |
async def detect_api(file: UploadFile = File(...)):
|
|
|
110 |
contents = await file.read()
|
111 |
image = Image.open(io.BytesIO(contents))
|
|
|
|
|
112 |
image_np = np.array(image)
|
113 |
|
|
|
114 |
results = model(image_np)
|
115 |
result = results[0]
|
116 |
|
|
|
117 |
detections = []
|
118 |
for box in result.boxes:
|
119 |
detection = {
|
120 |
+
"bbox": box.xyxy[0].tolist(),
|
121 |
+
"confidence": float(box.conf[0]),
|
122 |
+
"class": int(box.cls[0])
|
123 |
}
|
124 |
detections.append(detection)
|
125 |
|
126 |
return {"detections": detections}
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
# 将Gradio接口挂载到FastAPI
|
129 |
app = gr.mount_gradio_app(app, demo, path="/")
|
130 |
|