在Yolov5中同时实现文件夹全部图片的检测

首先在文件yolov5文件夹内创建一个.py文件,名字就叫dataread,然后在里面加入如下代码。然后把图片文件夹放入当前目录,目录命名为data

from torch.utils.data import Dataset
import os



class MyData(Dataset):

    def __init__(self, root_dir, image_dir):
        self.root_dir = root_dir
        self.image_dir = image_dir
        self.image_path = os.path.join(self.root_dir, self.image_dir)
        self.image_list = os.listdir(self.image_path)
        self.image_list.sort()

    def __getitem__(self, idx):
        img_name = self.image_list[idx]
        img_item_path = os.path.join(self.root_dir, self.image_dir, img_name)
        img_item_path.replace("\\", "/")
        return img_item_path
    def __len__(self):
        return len(self.image_list)

if __name__ == '__main__':
    root_dir = ""#当前根目录,在当前文件夹就不写
    image_ants = "data"#图片文件夹名字,需要跟detect.py同一个文件夹下面
    ants_dataset = MyData(root_dir, image_ants)
    print(ants_dataset[1])



然后在detect.py文件第一排加上

from dataread import MyData

然后在然后在detect.py文件最后

if __name__ == "__main__":里面全部改成如下
if __name__ == "__main__":
    opt = parse_opt()
    root_dir = ""#当前根目录,在当前文件夹就不写
    image = "data"#图片文件夹名字,需要跟detect.py同一个文件夹下面
    img = MyData(root_dir,image)
    for i in range(len(img)):
        opt.source = img[i]
        main(opt)

效果如图:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值