深度学习,YOLOV8(安全帽佩戴识别)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用yolov8进行目标检测推理并画框的Python代码示例: ```python import cv2 import torch from models.experimental import attempt_load from utils.general import non_max_suppression, scale_coords, plot_one_box from utils.torch_utils import select_device # 加载模型 weights = 'yolov8s.pt' device = select_device('') model = attempt_load(weights, map_location=device) stride = int(model.stride.max()) # 加载图片 img_path = 'test.jpg' img = cv2.imread(img_path) # 图片预处理 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (640, 640)) img = torch.from_numpy(img).to(device).float() img /= 255.0 if img.ndimension() == 3: img = img.unsqueeze(0) # 模型推理 pred = model(img)[0] pred = non_max_suppression(pred, 0.4, 0.5) # 画框 for det in pred: if det is not None and len(det): det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img.shape[2:]).round() for *xyxy, conf, cls in reversed(det): label = f'{model.names[int(cls)]} {conf:.2f}' plot_one_box(xyxy, img, label=label) # 显示图片 img = img.mul(255).byte().cpu().numpy() img = cv2.cvtColor(img[0], cv2.COLOR_RGB2BGR) cv2.imshow('result', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 上述代码中,我们首先加载了yolov8模型,然后使用OpenCV库加载了一张图片。接着,我们对图片进行了预处理,将其转换为模型所需的格式。然后,我们使用模型对图片进行了推理,并使用非极大值抑制算法过滤了一些冗余的检测结果。最后,我们使用plot_one_box函数在图片上画出了检测框,并显示了结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值