基于 export_entrance_position.py 的 HTTP API 服务,支持远程调用入户门检测功能。
# 激活环境
conda activate yolo26
# 启动服务
python server.py --host 0.0.0.0 --port 8000
# 或使用 uvicorn 直接启动
python -m uvicorn server:app --host 0.0.0.0 --port 8000
浏览器打开:http://localhost:8000/docs
这是 Swagger UI 界面,可以交互式测试所有 API。
# 健康检查
python client.py --health
# 检测场景
python client.py -s scene0001
# 带可视化输出
python client.py -s scene0001 --vis_ply
# 指定远程服务器
python client.py -s scene0001 --server http://192.168.1.100:8000
GET /health
响应示例:
{
"status": "healthy",
"model_loaded": true,
"gpu_available": false,
"timestamp": "2026-04-14T14:17:34.406460"
}
POST /detect
Content-Type: application/json
{
"scene_folder": "/path/to/scene0001",
"conf": 0.35,
"iou": 0.45,
"voxel_size": 0.03,
"imgsz": [1024, 2048],
"vis_ply": true
}
响应:
{
"task_id": "d36d7028-7b15-41bd-99b7-194647ef6656",
"status": "pending",
"message": "任务已提交,正在处理中"
}
GET /status/{task_id}
响应示例:
{
"task_id": "d36d7028-7b15-41bd-99b7-194647ef6656",
"status": "processing",
"progress": 0.3,
"message": "正在检测门并识别入户门..."
}
状态说明:
pending: 等待处理processing: 处理中completed: 已完成failed: 失败GET /result/{task_id}
响应: JSON 格式的入户门检测结果
GET /result/{task_id}/ply
响应: PLY 文件(二进制)
DELETE /task/{task_id}
from client import EntranceDoorClient
# 创建客户端
client = EntranceDoorClient(base_url="http://localhost:8000")
# 健康检查
health = client.health_check()
print(f"服务状态:{health['status']}")
# 提交检测任务并等待结果
result = client.detect(
scene_folder="/path/to/scene0001",
conf=0.35,
iou=0.45,
vis_ply=True,
poll_interval=2.0 # 查询状态间隔(秒)
)
print(f"入户门位置:{result['result']['entrance_position']}")
# 保存结果到本地
result_data = client.detect_and_save(
scene_folder="/path/to/scene0001",
output_dir="results",
vis_ply=True
)
# 仅查询状态(不等待)
task_id = "..."
status = client.get_status(task_id)
print(f"任务状态:{status['status']}")
# 获取结果
if status['status'] == 'completed':
result = client.get_result(task_id)
# 下载 PLY 文件
client.download_ply(task_id, "output/vis.ply")
# 删除任务
client.delete_task(task_id)
# 健康检查
curl http://localhost:8000/health
# 提交检测任务
curl -X POST http://localhost:8000/detect \
-H "Content-Type: application/json" \
-d '{
"scene_folder": "/path/to/scene0001",
"conf": 0.35,
"vis_ply": true
}'
# 查询状态
curl http://localhost:8000/status/{task_id}
# 获取结果
curl http://localhost:8000/result/{task_id}
# 下载 PLY
curl -o vis.ply http://localhost:8000/result/{task_id}/ply
python server.py --reload --port 8000
使用 gunicorn + uvicorn workers:
pip install gunicorn
gunicorn server:app \
-w 2 \
-k uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:8000 \
--timeout 120
FROM python:3.12-slim
WORKDIR /app
COPY requirements_server.txt .
RUN pip install -r requirements_server.txt
COPY export_entrance_position.py .
COPY camera_spherical.py .
COPY yoloe-26x-seg.pt .
COPY server.py .
EXPOSE 8000
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]
| 参数 | 默认值 | 说明 |
|---|---|---|
--host |
0.0.0.0 |
监听地址 |
--port |
8000 |
监听端口 |
--reload |
False |
开发模式(热重载) |
--workers |
1 |
工作进程数 |
| 参数 | 默认值 | 说明 |
|---|---|---|
--server |
http://localhost:8000 |
服务器地址 |
--conf |
0.35 |
检测置信度阈值 |
--iou |
0.45 |
NMS IoU 阈值 |
--voxel-size |
0.03 |
点云体素尺寸 |
--imgsz |
1024 2048 |
YOLOE 输入图像尺寸 |
--vis_ply |
False |
是否生成可视化 PLY |
--timeout |
600 |
超时时间(秒) |
使用客户端调用时,结果文件保存在场景文件夹的 output/ 目录下:
scene0001/
└── output/
├── entrance_position.json # JSON 结果
└── vis.ply # 可视化 PLY 文件(如使用 --vis_ply)
这样每个场景的检测结果都保存在各自的场景目录下,更有针对性。
服务器使用内存存储任务状态,重启后任务信息会丢失。
POST /detect 创建任务,返回 task_idGET /status/{task_id} 查询进度GET /result/{task_id} 获取检测结果DELETE /task/{task_id} 删除任务和结果文件server_results/ 目录client_results/ 目录DELETE /task/{task_id} 会自动清理相关文件400 Bad Request
404 Not Found
500 Internal Server Error
TimeoutError
降低图像分辨率
python client.py -s scene0001 --imgsz 640 1280
提高体素尺寸(降低点云密度)
python client.py -s scene0001 --voxel-size 0.05
多进程部署
gunicorn server:app -w 4 -k uvicorn.workers.UvicornWorker
检测过程会加载大量点云数据,建议:
--voxel-size 0.05 或更大值降低内存占用api_key_header = APIKeyHeader(name="X-API-Key")
@app.post("/detect") async def detect(api_key: str = Depends(api_key_header)):
# 验证 API key
2. **启用 HTTPS**
使用 Nginx 反向代理:
```nginx
server {
listen 443 ssl;
location / {
proxy_pass http://localhost:8000;
}
}
python
app = FastAPI()
app.config.max_upload_size = 100 * 1024 * 1024 # 100MB
# 检查端口占用
lsof -i :8000
# 检查依赖
pip install -r requirements_server.txt
# 检查服务器是否运行
curl http://localhost:8000/health
# 检查防火墙
telnet localhost 8000
查看服务器日志中的详细错误信息:
# 服务器日志会显示详细错误
# 检查 vision.txt 格式
# 检查图像文件是否存在