Spaces:
Running
Running
美化UI界面:添加新布局和样式
Browse files- app.py +23 -32
- requirements.txt +5 -9
app.py
CHANGED
@@ -1,42 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
-
import torch
|
4 |
-
import cv2
|
5 |
-
import numpy as np
|
6 |
-
from fastapi import FastAPI, File, UploadFile
|
7 |
-
from PIL import Image
|
8 |
-
import io
|
9 |
-
|
10 |
-
# 初始化 FastAPI
|
11 |
-
app = FastAPI()
|
12 |
|
13 |
# 加载模型
|
14 |
-
model = YOLO(
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
results = model(image)
|
22 |
-
result = results[0]
|
23 |
-
annotated_image = result.plot()
|
24 |
-
return cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
25 |
-
except Exception as e:
|
26 |
-
return None
|
27 |
|
28 |
-
# 创建 Gradio
|
29 |
demo = gr.Interface(
|
30 |
-
|
31 |
-
inputs=
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
39 |
-
|
40 |
if __name__ == "__main__":
|
41 |
-
|
42 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# 加载模型
|
5 |
+
model = YOLO('NailongKiller.yolo11n.pt')
|
6 |
|
7 |
+
def predict(img):
|
8 |
+
# 运行推理
|
9 |
+
results = model.predict(img)
|
10 |
+
# 返回第一个结果的绘制图像
|
11 |
+
return results[0].plot()
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# 创建 Gradio 演示
|
14 |
demo = gr.Interface(
|
15 |
+
predict,
|
16 |
+
inputs=[
|
17 |
+
gr.Image(label="输入图片")
|
18 |
+
],
|
19 |
+
outputs=[
|
20 |
+
gr.Image(label="检测结果", type="numpy")
|
21 |
+
],
|
22 |
+
title="🐉 奶龙杀手 (NailongKiller)",
|
23 |
+
description="上传图片来检测奶龙 | Upload an image to detect Nailong",
|
24 |
+
examples=[
|
25 |
+
["example1.jpg"],
|
26 |
+
["example2.jpg"]
|
27 |
+
],
|
28 |
+
cache_examples=True
|
29 |
)
|
30 |
|
31 |
+
# 启动应用
|
|
|
|
|
32 |
if __name__ == "__main__":
|
33 |
+
demo.launch()
|
|
requirements.txt
CHANGED
@@ -1,9 +1,5 @@
|
|
1 |
-
|
2 |
-
torch>=
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
python-multipart
|
7 |
-
uvicorn
|
8 |
-
pillow
|
9 |
-
gradio>=3.0.0
|
|
|
1 |
+
gradio
|
2 |
+
torch>=1.8.0
|
3 |
+
torchvision>=0.9.0
|
4 |
+
ultralytics>=8.3.0
|
5 |
+
Pillow
|
|
|
|
|
|
|
|