切片辅助超推理-sahi库-get_sliced_prediction使用示例

from sahi import AutoDetectionModel
from sahi.predict import get_prediction, get_sliced_prediction, predict

yolov8_model_path = r'yolov8n.pt'
imfile = r'small-vehicles1.jpeg'
export_dir = r'ashi-res'

detection_model = AutoDetectionModel.from_pretrained(
    model_type='yolov8',
    model_path=yolov8_model_path,
    confidence_threshold=0.3,
    device="cpu",  # or 'cuda:0'
)

def set_cfg_predict():
    slice_height = 320
    slice_width = 320
    result = get_sliced_prediction(
        imfile,
        detection_model,
        slice_height=slice_height,
        slice_width=slice_width,
        overlap_height_ratio=0.2,
        overlap_width_ratio=0.2
    )
    result.export_visuals(export_dir=export_dir, hide_labels=True)

# std_predict()
# auto_cfg_predict()
set_cfg_predict()

上面以yolov8n对工程中示例图为例进行了简单测试。

原图

1、标准推理

def std_predict():
    result = get_prediction(
        imfile,
        detection_model)
    result.export_visuals(export_dir=export_dir, hide_labels=True)

大量漏检

2、自动分割

def auto_cfg_predict():
    result = get_sliced_prediction(
        imfile,
        detection_model
    )
    result.export_visuals(export_dir=export_dir, hide_labels=True)

有少量漏检

3、设置h-w=512

有错误检测

4、设置h-w=416

综合最好

5、设置h-w=320

有错误检测

SAHI是一种切片辅助推理框架,旨在帮助开发人员解决现实世界中的目标检测问题。它通过将图像分成多个切片来提高检测性能,从而克服了现实世界中的一些问题,例如目标尺寸变化,目标遮挡和目标密度变化等。SAHI的核心思想是将图像分成多个切片,然后对每个切片进行单独的检测,最后将检测结果合并起来得到最终的检测结果。这种方法可以提高检测性能,特别是对于小目标的检测效果更好。 下面是一个使用SAHI进行目标检测的Python代码示例: ```python import cv2 import numpy as np # 加载图像 img = cv2.imread('test.jpg') # 定义切片大小 slice_size = 512 # 获取图像大小 height, width, _ = img.shape # 计算切片数量 num_slices_h = int(np.ceil(height / slice_size)) num_slices_w = int(np.ceil(width / slice_size)) # 定义检测器 detector = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'model.caffemodel') # 定义类别标签 class_labels = ['person', 'car', 'truck', 'bus'] # 定义检测结果列表 results = [] # 循环遍历每个切片 for i in range(num_slices_h): for j in range(num_slices_w): # 计算切片的坐标 x1 = j * slice_size y1 = i * slice_size x2 = min(x1 + slice_size, width) y2 = min(y1 + slice_size, height) # 提取切片 slice_img = img[y1:y2, x1:x2] # 构建输入blob blob = cv2.dnn.blobFromImage(slice_img, 1.0, (300, 300), (104.0, 177.0, 123.0)) # 进行检测 detector.setInput(blob) detections = detector.forward() # 解析检测结果 for k in range(detections.shape[2]): confidence = detections[0, 0, k, 2] class_id = int(detections[0, 0, k, 1]) # 如果置信度大于0.5,则将检测结果添加到列表中 if confidence > 0.5 and class_labels[class_id] == 'person': x = int(detections[0, 0, k, 3] * slice_size) + x1 y = int(detections[0, 0, k, 4] * slice_size) + y1 w = int(detections[0, 0, k, 5] * slice_size) - x h = int(detections[0, 0, k, 6] * slice_size) - y results.append((x, y, w, h)) # 在原始图像上绘制检测结果 for (x, y, w, h) in results: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示结果 cv2.imshow('result', img) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值