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

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

Browse files
Files changed (1) hide show
  1. app.py +9 -51
app.py CHANGED
@@ -6,7 +6,6 @@ import numpy as np
6
  from fastapi import FastAPI, File, UploadFile
7
  from PIL import Image
8
  import io
9
- import os
10
 
11
  # 初始化 FastAPI
12
  app = FastAPI()
@@ -16,69 +15,28 @@ model = YOLO("NailongKiller.yolo11n.pt")
16
 
17
  def detect_objects(image):
18
  if image is None:
19
- return None, "No image provided"
20
 
21
  try:
22
- # 运行推理
23
  results = model(image)
24
  result = results[0]
25
-
26
- # 在图像上绘制检测框
27
  annotated_image = result.plot()
28
- annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
29
-
30
- # 获取检测结果统计
31
- num_detections = len(result.boxes)
32
- detection_info = f"检测到 {num_detections} 个目标"
33
-
34
- return annotated_image, detection_info
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/")
59
- async def detect_api(file: UploadFile = File(...)):
60
- contents = await file.read()
61
- image = Image.open(io.BytesIO(contents))
62
- image_np = np.array(image)
63
-
64
- results = model(image_np)
65
- result = results[0]
66
-
67
- detections = []
68
- for box in result.boxes:
69
- detection = {
70
- "bbox": box.xyxy[0].tolist(),
71
- "confidence": float(box.conf[0]),
72
- "class": int(box.cls[0])
73
- }
74
- detections.append(detection)
75
-
76
- return {"detections": detections}
77
-
78
- # 将Gradio接口挂载到FastAPI
79
  app = gr.mount_gradio_app(app, demo, path="/")
80
 
81
- # 启动应用
82
  if __name__ == "__main__":
83
  import uvicorn
84
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
6
  from fastapi import FastAPI, File, UploadFile
7
  from PIL import Image
8
  import io
 
9
 
10
  # 初始化 FastAPI
11
  app = FastAPI()
 
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)