yolov5只显示标签
时间: 2023-09-10 22:08:38 浏览: 76
ros-yolov5-ws
5星 · 资源好评率100%
YoloV5 是一种目标检测模型,它可以在图像或视频中识别和定位不同的物体。如果你只看到了标签而没有看到物体框,这可能是因为你的代码中没有将物体框绘制出来。可以在代码中添加绘制物体框的功能,以便更清楚地展示检测结果。下面是一个绘制物体框的示例代码:
```python
import cv2
import torch
from models.experimental import attempt_load
from utils.general import non_max_suppression
def detect(model, img, conf_thres=0.5, iou_thres=0.5, device='cpu'):
# Convert image to Tensor
img = torch.from_numpy(img).to(device)
img = img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
model.eval()
with torch.no_grad():
pred = model(img)
# Apply NMS
pred = non_max_suppression(pred, conf_thres, iou_thres)
# Draw bounding boxes
for det in pred[0]:
x1, y1, x2, y2, conf, cls = det.tolist()
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
label = f'{classes[int(cls)]} {conf:.2f}'
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
return img
if __name__ == '__main__':
# Load model
weights = 'yolov5s.pt'
model = attempt_load(weights, map_location=torch.device('cpu'))
# Load image
img = cv2.imread('image.jpg')
# Detect objects
result = detect(model, img)
# Show result
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上面的代码中,我们将物体框和标签绘制在了图片上。如果你想只显示标签,可以将 `cv2.rectangle()` 函数和 `cv2.putText()` 函数中的绘制物体框的代码注释掉即可。
阅读全文