Hakureirm commited on
Commit
57f4ecb
·
1 Parent(s): 0b49f38

美化UI界面:添加新布局和样式

Browse files
Files changed (2) hide show
  1. app.py +23 -32
  2. 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("NailongKiller.yolo11n.pt")
15
 
16
- def detect_objects(image):
17
- if image is None:
18
- return None
19
-
20
- try:
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
- fn=detect_objects,
31
- inputs=gr.Image(type="numpy"),
32
- outputs=gr.Image(),
33
- title="奶龙杀手 (Nailong Killer)",
34
- description="上传图片来检测奶龙 | Upload an image to detect Nailong"
 
 
 
 
 
 
 
 
 
35
  )
36
 
37
- # 将 Gradio 接口挂载到 FastAPI
38
- app = gr.mount_gradio_app(app, demo, path="/")
39
-
40
  if __name__ == "__main__":
41
- import uvicorn
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
- ultralytics>=8.0.0
2
- torch>=2.0.0
3
- opencv-python-headless
4
- numpy>=1.22.2
5
- fastapi
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