Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,642 Bytes
8d9f842 fd3ecfb e45be51 0ce6f4c 2150861 f69b08f 8d9f842 e45be51 0ce6f4c f8b6390 0ce6f4c 2150861 57f4ecb 4b635e1 0be7f95 34d8201 4b635e1 34d8201 4b635e1 f8b6390 da19eb4 4b635e1 da19eb4 4b635e1 da19eb4 f8b6390 da19eb4 9f7b52a da19eb4 9f7b52a f8b6390 9f7b52a da19eb4 9f7b52a da19eb4 34d8201 da19eb4 8d9f842 e9671ed fd3ecfb da19eb4 57f4ecb a046a3a 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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'
# 移除 .to(device),在预测时指定设备
model = YOLO('nailong_yolo11.onnx')
@spaces.GPU
def predict(img):
# 将输入图像转换为PIL Image对象
input_image = Image.fromarray(img)
# 保持长宽比的情况下调整尺寸
w, h = input_image.size
scale = min(640/w, 640/h)
new_w, new_h = int(w * scale), int(h * scale)
if scale != 1:
input_image = input_image.resize((new_w, new_h), Image.LANCZOS)
# 转换为numpy数组并进行预测
img_array = np.array(input_image)
# 在predict时指定device
results = model.predict(img_array, device=device)
result = results[0]
# 获取预测结果
result_img = result.plot()
# 处理检测信息
info = {
"detected": len(result.boxes) > 0,
"count": len(result.boxes),
"detections": []
}
if info["detected"]:
# 获取每个检测框的信息
for box in result.boxes:
conf = float(box.conf[0])
cls = int(box.cls[0])
cls_name = result.names[cls]
detection_info = {
"class": cls_name,
"confidence": f"{conf:.2%}"
}
info["detections"].append(detection_info)
# 生成输出文本
output_text = f"""检测结果:
- 是否检测到目标: {'是' if info['detected'] else '否'}
- 检测到的目标数量: {info['count']}"""
if info["detections"]:
output_text += "\n- 详细信息:"
for idx, det in enumerate(info["detections"], 1):
output_text += f"\n 目标 {idx}: {det['class']} (置信度: {det['confidence']})"
# 如果需要将结果图像缩放回原始尺寸
if scale != 1:
result_img = Image.fromarray(result_img)
result_img = result_img.resize((w, h), Image.LANCZOS)
result_img = np.array(result_img)
return result_img, output_text
demo = gr.Interface(
fn=predict,
inputs=gr.Image(label="输入图片"),
outputs=[
gr.Image(label="检测结果", type="numpy"),
gr.Textbox(label="检测信息")
],
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) |