yolov5检测到的框画到原图上

1.把检测到的框画到原图上

import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')
import torch
import cv2
import numpy as np
import requests
import torchvision.transforms as transforms

from models.common 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
YOLOv5 中,删除锚框并得到目标的中心点坐标的代码如下所示: ```python import torch import cv2 from models.experimental import attempt_load from utils.general import non_max_suppression # 加载模型 weights = 'path/to/weights.pt' img_size = 640 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = attempt_load(weights, map_location=device) model.to(device).eval() # 加载图像 img_path = 'path/to/image.jpg' img0 = cv2.imread(img_path) # BGR img = cv2.resize(img0, (img_size, img_size)) img = img[:, :, ::-1].transpose(2, 0, 1) # RGB img = torch.from_numpy(img).to(device).float() img /= 255.0 if img.ndimension() == 3: img = img.unsqueeze(0) # 进行目标检测 pred = model(img) pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45) # 处理预测结果 for i, det in enumerate(pred): if det is not None and len(det): det[:, :4] = det[:, :4].clamp(min=0, max=img_size) for *xyxy, conf, cls in reversed(det): x, y, w, h = (xyxy[0] + xyxy[2]) / 2, (xyxy[1] + xyxy[3]) / 2, xyxy[2] - xyxy[0], xyxy[3] - xyxy[1] print('Object {}: center point = ({}, {})'.format(i, x, y)) ``` 在上面的代码中,我们首先加载了 YOLOv5 模型和待检测的图像,然后通过模型进行目标检测,并使用 `non_max_suppression` 函数进行非极大值抑制,得到预测结果 `pred`。接着,我们遍历预测结果,计算每个预测框的中心点坐标,并输出到控制台上。 需要注意的是,在上面的代码中,我们使用了非极大值抑制函数 `non_max_suppression` 来过滤掉重叠的预测框,如果您不需要使用非极大值抑制,可以将 `non_max_suppression` 函数的参数 `conf_thres` 和 `iou_thres` 设置为 0。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飞龙程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值