sahi小目标检测通过读取文件夹检测图片

学习记录

网上现有的sahi检测只能检测单张图片,加上循环之后,可以实现读取文件夹内的图片,循环检测。

import os
import ultralytics
from sahi.models.yolov8 import Yolov8DetectionModel
from sahi.predict import get_sliced_prediction

model_path = "D:/DeepL/ultralytics-main/runs/detect/train8/weights/best.pt"

# 使用的YOLOv5检测模型,使用gpu加速,置信度0.25
detection_model = Yolov8DetectionModel(
    model_path=model_path,
    confidence_threshold=0.5,
    device="cpu"
)

train_path = "D:/DeepL/ultralytics-main/data_fushi/testimages"
allpath = []

imglist = ultralytics.data.utils.getFileList(train_path, [], 'jpg')
print('本次执行检索到 ' + str(len(imglist)) + ' 张图像\n')

def get_lableandwav(path, dir):
    dirs = os.listdir(path)
    for a in dirs:
        imgname = os.path.splitext(os.path.basename(a))[0]
        print('获取图片: ' + imgname + '.jpg' )

        if os.path.isfile(path + "/" + a):
            #print("D:/DeepL/ultralytics-main/data_fushi/testimages/" + a + ".jpg")
            imgpath="D:/DeepL/ultralytics-main/data_fushi/testimages/" + a

            result = get_sliced_prediction(
                imgpath,
                detection_model,
                slice_height=160,  # 切分子图高度
                slice_width=160,  # 切分子图宽度
                overlap_height_ratio=0.2,  # 子图高度之间的重叠率
                overlap_width_ratio=0.2   # 子图宽度之间的重叠率
            )
            # 保存检测图片
            result.export_visuals(export_dir="D:/DeepL/ultralytics-main/sahi-result/", file_name=imgname)

            print('完成检测 ' + imgpath + '\n')
            allpath.append(a)
            # if dir != "":
            #     lllables.append(dir)
        else:
            get_lableandwav(str(path) + "/" + str(a), a)
        ##循环遍历这个文件夹

    return allpath

allpath = get_lableandwav(train_path, "")
print(allpath)
print('SAHI检测结束\n')

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值