Spaces:
Running
on
Zero
Running
on
Zero
Add YOLO11 Nailong Killer detection app
Browse files- NailongKiller.yolo11n.pt +3 -0
- app.py +73 -0
- requirements.txt +9 -0
NailongKiller.yolo11n.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f1fe4dcb1f98bd02882a151f0c97a889e05884d7b24a7a347667359334cc3dc0
|
3 |
+
size 16040468
|
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# 运行推理
|
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 |
+
|
70 |
+
# 启动应用
|
71 |
+
if __name__ == "__main__":
|
72 |
+
import uvicorn
|
73 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|