uvicorn.run
时间: 2023-11-18 14:05:59 浏览: 74
uvicorn.run()是uvicorn库中的一个方法,用于运行FastAPI应用程序。它有以下几个常用参数:
- app:指定要运行的FastAPI应用程序的py文件和实例对象。
- host:指定访问的URL,默认为127.0.0.1。
- port:指定访问的端口,默认为8080。
- reload:启用热更新功能,当代码内容发生修改时,自动重启服务器。
- debug:启用调试模式,可以在终端中看到更详细的日志信息。
- reload_dirs:设置需要进行热更新的目录,以列表形式提供。
- log_level:设置日志级别,默认为info。
相关问题
uvicorn.run怎么放入证书
您可以使用 `--ssl-keyfile` 和 `--ssl-certfile` 参数来指定证书和私钥文件的路径,然后将其传递给 `uvicorn.run` 方法。例如:
```
import uvicorn
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8000, ssl_keyfile="/path/to/keyfile.key", ssl_certfile="/path/to/certfile.crt")
```
其中,`app:app` 是您的应用程序的名称和实例。请确保将 `keyfile.key` 和 `certfile.crt` 替换为您实际使用的证书和私钥文件的路径。
import uvicorn from fastapi import FastAPI, UploadFile, File from io import BytesIO from PIL import Image, ImageDraw from utils.operation import YOLO def detect(onnx_path='ReqFile/yolov5n-7-k5.onnx', img=r'ReqFile/bus.jpg', show=True): ''' 检测目标,返回目标所在坐标如: {'crop': [57, 390, 207, 882], 'classes': 'person'},...] :param onnx_path:onnx模型路径 :param img:检测用的图片 :param show:是否展示 :return: ''' yolo = YOLO(onnx_path=onnx_path) # 加载yolo类 det_obj = yolo.decect(img) # 检测 # 打印检测结果 print(det_obj) # 画框框 if show: img = Image.open(img) draw = ImageDraw.Draw(img) for i in range(len(det_obj)): draw.rectangle(det_obj[i]['crop'], width=3) img.show() # 展示 return det_obj app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.post("/detect/") async def create_upload_file(file: UploadFile = File(...)): contents = await file.read() # 接收浏览器上传的图片 im1 = BytesIO(contents) # 将数据流转换成二进制文件存在内存中 # 返回结果 return detect(onnx_path='ReqFile/best-0206.onnx', img=im1, show=False) # 启动项目 if __name__ == "__main__": uvicorn.run(app='main:app', port=8000, host='0.0.0.0', reload=True)
这段代码使用 FastAPI 框架搭建了一个接口,可以接收浏览器上传的图片,调用 `detect()` 函数进行目标检测,最后返回检测结果。其中使用了 YOLO 模型进行目标检测,`detect()` 函数接收 ONNX 模型路径和图片,返回目标在图片中的位置和类别。`create_upload_file()` 函数接收上传的图片,将其转换成二进制文件存在内存中,调用 `detect()` 函数进行目标检测,并返回结果。最后使用 Uvicorn 启动项目。