吉祥猴378 2024-08-01 13:04 采纳率: 95.8%
浏览 31
已结题

哪位大姥,知道为什么我yolov5识别模型然后出现边框,识别不出来

monitor
    "left": 0,
    "top": 0,
    "width": 2560,
    "height": 1440 }
cv2.getWindowProperty('SF_TRT', cv2.WND_PROP_VISIBLE

wh

img = np.array(img)  
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)  
img = cv2.resize(img,(640,640))
model = model.to(device)
results =model(img)
xmins = results.pandas().xyxy[0]['xmin']
ymins = results.pandas().xyxy[0]['ymin']
xmaxs = results.pandas().xyxy[0]['xmax']
ymaxs = results.pandas().xyxy[0]['ymax']

for xmin, ymin, xmax, ymax in zip(xmins, ymins, xmaxs, ymaxs):
    cv2.rectangle(img, (xmin[0], ymin[1]), (xmax[2], ymax[3]), (0, 255, 0), 2)

cv2.namedWindow('SF_TRT', cv2.WINDOW_NORMAL)  
cv2.getWindowProperty('SF_TRT', cv2.WND_PROP_VISIBLE)
cv2.imshow('SF_TRT', img)
k = cv2.waitKey(1)
if k % 256 == 27
  • 写回答

1条回答 默认 最新

  • Mr' 郑 2024-08-05 16:46
    关注

    从您提供的代码片段来看,有几个地方需要注意和修改,以确保正确运行。下面是对您代码的一些修改建议:

    1. 加载模型和设备分配:确保模型是在正确的设备(CPU 或 GPU)上加载的,并且在进行预测之前将图像也移动到相同的设备上。
    2. 图像处理:确保图像的尺寸和格式正确。
    3. 结果处理:正确地从结果中提取边界框坐标。
    4. 绘制边界框:确保边界框坐标正确传递给 cv2.rectangle 函数。

    修改后的代码示例

    import cv2
    import torch
    from yolov5.models.experimental import attempt_load
    from yolov5.utils.datasets import LoadImages
    from yolov5.utils.general import non_max_suppression, scale_coords
    from yolov5.utils.plots import plot_one_box
    
    # 确保使用正确的模型路径
    weights_path = 'path/to/your/yolov5_weights.pt'
    
    # 加载模型
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = attempt_load(weights_path, map_location=device)
    
    # 设置模型的输入尺寸
    img_size = 640
    
    # 加载图像
    img_path = 'path/to/your/image.jpg'
    img0 = cv2.imread(img_path)
    
    # 图像预处理
    img = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (img_size, img_size))
    img = img.transpose((2, 0, 1))  # HWC to CHW
    img = img.astype(np.float32) / 255.0  # 归一化到0-1
    img = torch.from_numpy(img).unsqueeze(0).to(device)
    
    # 运行模型
    model.eval()
    with torch.no_grad():
        pred = model(img)[0]
    
    # 非极大值抑制 (NMS)
    pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45)
    
    # 处理检测结果
    for i, det in enumerate(pred):  # detections per image
        if len(det):
            det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
    
            for *xyxy, conf, cls in reversed(det):
                label = f'{model.names[int(cls)]} {conf:.2f}'
                plot_one_box(xyxy, img0, label=label, color=(0, 255, 0), line_thickness=2)
    
    # 显示结果
    cv2.namedWindow('SF_TRT', cv2.WINDOW_NORMAL)
    cv2.imshow('SF_TRT', img0)
    cv2.waitKey(0)  # 等待按键退出
    cv2.destroyAllWindows()
    

    关键点解析

    • 模型加载:使用 attempt_load 来加载模型,并确保模型和图像都在同一个设备上。
    • 图像预处理:确保图像的尺寸、通道顺序和归一化正确。
    • 预测:使用 non_max_suppression 来过滤掉重叠的边界框。
    • 边界框绘制:使用 plot_one_box 函数来简化边界框的绘制过程。

    注意事项

    • 确保您的权重文件路径正确。
    • 调整 conf_thresiou_thres 参数以优化检测结果。
    • 确保您的图像路径正确无误。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月10日
  • 已采纳回答 9月2日
  • 创建了问题 8月1日