Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from ultralytics import YOLO | |
| import numpy as np | |
| from PIL import Image | |
| # 加载模型,注意:模型文件要和 app.py 在同一目录 | |
| model = YOLO('yolo11n.pt') # 你的模型文件名改为 yolo11n.pt 放在目录下 | |
| # 定义推理函数 | |
| def predict(image): | |
| results = model.predict(source=image, save=False) | |
| result_image = results[0].plot() # 直接绘制预测框 | |
| return Image.fromarray(result_image) | |
| # 创建 Gradio 界面 | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Image(type="pil"), | |
| title="YOLOv11n Object Detection", | |
| description="上传图片,返回检测结果" | |
| ) | |
| iface.launch() | |