Spaces:
Build error
Build error
File size: 911 Bytes
16a2fb8 7f67963 56bb892 16a2fb8 00167e6 16a2fb8 7f67963 16a2fb8 7f67963 00167e6 7f67963 00167e6 7f67963 00167e6 b09c46c 7f67963 56bb892 7f67963 00167e6 56bb892 7f67963 56bb892 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import torch
import gradio as gr
from ultralytics import YOLO
from PIL import Image
# 正确方式:信任 YOLOv8 的 DetectionModel 类(适配 PyTorch 2.6+)
torch.serialization.add_safe_globals(['ultralytics.nn.tasks.DetectionModel'])
# 加载模型
model = YOLO("best.pt")
# 定义推理函数
def predict(img):
results = model(img)[0]
output = []
for box in results.boxes:
cls_id = int(box.cls[0])
label = results.names[cls_id]
conf = float(box.conf[0])
output.append(f"检测到:{label}(置信度:{conf*100:.1f}%)")
return output # ✅ 返回字符串列表,前端可读!
# 创建 Gradio 接口
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="json",
title="眼部疾病检测(YOLOv8)",
description="上传或拍摄眼部图像,模型将自动分析潜在的眼病。"
)
iface.launch()
|