Yolov5获得盒子,分数,类,数字

发布于 2025-01-18 20:57:00 字数 1727 浏览 5 评论 0原文

我试图在我的项目中将对象跟踪与深度排序绑定,我需要获取框、分数、类、数字。

加载预训练的 Yolov5 模型:

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()

获取预测:

result = model(img)
print(result.shape)
print(result)
torch.Size([8, 6])
tensor([[277.50000, 379.25000, 410.50000, 478.75000,   0.90625,   2.00000],
        [404.00000, 205.12500, 498.50000, 296.00000,   0.88623,   2.00000],
        [262.50000, 247.75000, 359.50000, 350.25000,   0.88281,   2.00000],
        [210.50000, 177.75000, 295.00000, 261.75000,   0.83154,   2.00000],
        [195.50000, 152.50000, 257.75000, 226.00000,   0.78223,   2.00000],
        [137.00000, 146.75000, 168.00000, 162.00000,   0.55713,   2.00000],
        [ 96.00000, 130.12500, 132.50000, 161.12500,   0.54199,   2.00000],
        [ 43.56250,  89.56250,  87.68750, 161.50000,   0.50146,   5.00000]], device='cuda:0')
tensor([[277.50000, 379.25000, 410.50000, 478.75000,   0.90625,   2.00000],
        [404.00000, 205.12500, 498.50000, 296.00000,   0.88623,   2.00000],
        [262.50000, 247.75000, 359.50000, 350.25000,   0.88281,   2.00000],
        [210.50000, 177.75000, 295.00000, 261.75000,   0.83154,   2.00000],
        [195.50000, 152.50000, 257.75000, 226.00000,   0.78223,   2.00000],
        [137.00000, 146.75000, 168.00000, 162.00000,   0.55713,   2.00000],
        [ 96.00000, 130.12500, 132.50000, 161.12500,   0.54199,   2.00000],
        [ 43.56250,  89.56250,  87.68750, 161.50000,   0.50146,   5.00000]], device='cuda:0')

所以现在我的问题是如何获取每个变量中的框、分数、类、数字? 我需要它来进行对象跟踪,

我在 Pytorch 文档上的示例中尝试过一次: result.xyxy[0]

但就我而言,我收到错误:

Tensor has no attribute xyxy

im trying to bind the Object Tracking with Deep Sort in my Project and i need to get the boxes, scores, classes, nums.

Loading Pretrained Yolov5 model:

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()

Getting the Prediction:

result = model(img)
print(result.shape)
print(result)
torch.Size([8, 6])
tensor([[277.50000, 379.25000, 410.50000, 478.75000,   0.90625,   2.00000],
        [404.00000, 205.12500, 498.50000, 296.00000,   0.88623,   2.00000],
        [262.50000, 247.75000, 359.50000, 350.25000,   0.88281,   2.00000],
        [210.50000, 177.75000, 295.00000, 261.75000,   0.83154,   2.00000],
        [195.50000, 152.50000, 257.75000, 226.00000,   0.78223,   2.00000],
        [137.00000, 146.75000, 168.00000, 162.00000,   0.55713,   2.00000],
        [ 96.00000, 130.12500, 132.50000, 161.12500,   0.54199,   2.00000],
        [ 43.56250,  89.56250,  87.68750, 161.50000,   0.50146,   5.00000]], device='cuda:0')
tensor([[277.50000, 379.25000, 410.50000, 478.75000,   0.90625,   2.00000],
        [404.00000, 205.12500, 498.50000, 296.00000,   0.88623,   2.00000],
        [262.50000, 247.75000, 359.50000, 350.25000,   0.88281,   2.00000],
        [210.50000, 177.75000, 295.00000, 261.75000,   0.83154,   2.00000],
        [195.50000, 152.50000, 257.75000, 226.00000,   0.78223,   2.00000],
        [137.00000, 146.75000, 168.00000, 162.00000,   0.55713,   2.00000],
        [ 96.00000, 130.12500, 132.50000, 161.12500,   0.54199,   2.00000],
        [ 43.56250,  89.56250,  87.68750, 161.50000,   0.50146,   5.00000]], device='cuda:0')

so now my question is how do i get the boxes, scores, classes, nums in each variables?
I need that for the Object Tracking

I tried it once with the example on Pytorch Documentation:
result.xyxy[0]

but in my Case I get an Error:

Tensor has no attribute xyxy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-01-25 20:57:00

模型的输出是火炬张量,没有 xyxy 方法。您需要手动提取值。您可以一一检查每个检测:

import torch

det = torch.rand(8, 6)

for *xyxy, conf, cls in det:
    print(*xyxy)
    print(conf)
    print(cls)

或者您可以通过以下方式对检测张量进行切片:

xyxy = det[:, 0:4]
conf = det[:, 4]
cls = det[:, 5]

print(xyxy)
print(conf)
print(cls)

The output from the model is a torch tensor and has no xyxy method. You need to extract the values manually. Either you can go through each detection one by one:

import torch

det = torch.rand(8, 6)

for *xyxy, conf, cls in det:
    print(*xyxy)
    print(conf)
    print(cls)

or you can slice the detections tensor by:

xyxy = det[:, 0:4]
conf = det[:, 4]
cls = det[:, 5]

print(xyxy)
print(conf)
print(cls)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文