Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,239 Bytes
8d9f842 fd3ecfb e45be51 0ce6f4c 2150861 f69b08f 8d9f842 e45be51 0ce6f4c 2150861 57f4ecb b482a00 0be7f95 b482a00 0be7f95 57f4ecb 8d9f842 e9671ed fd3ecfb 57f4ecb fd3ecfb 57f4ecb e9671ed 0f3261d 8d9f842 b482a00 |
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 35 36 37 38 39 40 |
import gradio as gr
from ultralytics import YOLO
from fastapi import FastAPI
from PIL import Image
import torch
import spaces
import numpy as np
app = FastAPI()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = YOLO('NailongKiller.yolo11n.pt').to(device)
@spaces.GPU
def predict(img):
# 优化图像预处理
img_resized = np.array(Image.fromarray(img).resize((640, 640)))
# 规范化像素值到 0-1 范围
img_tensor = torch.tensor(img_resized, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).div(255.0).to(device)
# 设置模型预测参数以加快后处理速度
results = model.predict(
img_tensor,
conf=0.50, # 提高置信度阈值
iou=0.45, # 调整 IOU 阈值
max_det=100 # 限制最大检测数量
)
return results[0].plot()
demo = gr.Interface(
fn=predict,
inputs=gr.Image(label="输入图片"),
outputs=gr.Image(label="检测结果", type="numpy"),
title="🐉 奶龙杀手 (NailongKiller)",
description="上传图片来检测奶龙 | Upload an image to detect Nailong",
examples=[["example1.jpg"]],
cache_examples=True
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |