yolov5打印检测结果
时间: 2023-07-03 10:31:37 浏览: 73
在使用Yolov5进行目标检测后,可以通过以下代码打印检测结果:
```
from PIL import Image
import torch
import numpy as np
from matplotlib import pyplot as plt
from utils.datasets import letterbox
# 加载模型
model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='path/to/weights.pt')
# 读取图像
img_path = 'path/to/image.jpg'
img = Image.open(img_path)
# 对图像进行预处理
img_size = 640
img = letterbox(img, img_size, stride=32)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
# 将图像转为Tensor
img = torch.from_numpy(img).to(model.device)
img = img.float()
img /= 255.0
img = img.unsqueeze(0)
# 进行目标检测
results = model(img)
# 打印检测结果
results.print()
```
其中,`model`为Yolov5模型,`img_path`为待检测图像的路径,`img_size`为图像输入模型时的大小。`results.print()`会打印出检测结果,包括检测到的目标的类别、置信度以及坐标等信息。