根据yolo目标检测的检测框裁剪检测目标

根据已经训练好的模型在yolov5中检测抠图

首先,在detect代码中找到

parser.add_argument('--save-txt',  action='store_true', help='save results to *.txt')

添加default=True,这一步是为了输出检测框的txt文件

parser.add_argument('--save-txt', default=True, action='store_true', help='save results to *.txt')
import os
import cv2
 
path = 'runs/detect/exp4/exp4'        # jpg图片和对应的生成结果的txt标注文件放在一个文件夹下
path3 = 'runs/cut_picture'    # 裁剪出来的小图保存的根目录
path2 = 'runs/crop_picture'   # 覆盖目标区域后的原图
 
file = os.listdir(path)

img_total = []
txt_total = []
for filename in file:
    first, last = os.path.splitext(filename)
    if last == ".jpg":                      # 图片的后缀名
        img_total.append(first)
    else:
        txt_total.append(first)
for img_name in img_total:
    if img_name in txt_total:
        filename_img = img_name+".jpg"
        path1 = os.path.join(path, filename_img)
        img = cv2.imread(path1)
        h, w = img.shape[0], img.shape[1]
        img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)  # resize 图像大小,否则roi区域可能会报错
        filename_txt = img_name+".txt"
        n = 1
        with open(os.path.join(path, filename_txt), "r+", encoding="utf-8", errors="ignore") as f:
            for line in f:
                coordinate = line.split(" ")
                x_center = w * float(coordinate[1])       # coordinate[1]左上点的x坐标
                y_center = h * float(coordinate[2])       # coordinate[2]左上点的y坐标
                width = int(w*float(coordinate[3]))       # coordinate[3]图片width
                height = int(h*float(coordinate[4]))      # coordinate[4]图片height
                lefttopx = int(x_center-width/2.0)
                lefttopy = int(y_center-height/2.0)
                filename_last = img_name + "_" + str(n) + ".jpg"
                roi = img[lefttopy+1:lefttopy+height+3, lefttopx+1:lefttopx+width+1]
                cv2.imwrite(os.path.join(path3, filename_last), roi)
                filename_last = img_name+"_"+str(n)+".jpg"    # 裁剪出来的小图文件名
                img[lefttopy + 1:lefttopy + height + 3, lefttopx + 1:lefttopx + width + 1] = (255, 255, 255)
                n = n+1
            cv2.imwrite(os.path.join(path2, filename_last), img)
    else:
        continue
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
PyTorch是一个广泛用于机器学习和深度学习的开源架。YOLO(You Only Look Once)是一种流行的目标检测算法,它通过将目标检测任务转化为一个回归问题,在一次前向传播中同时预测目标的边界和类别。 使用PyTorch实现YOLO目标检测算法,需要以下步骤: 1. 数据准备:收集和标注图像数据集,标注每个图像中的目标位置和类别。 2. 网络模型定义:使用PyTorch定义YOLO网络模型。YOLO网络通常由卷积层、池化层和全连接层组成。网络的最后一层输出包含目标边界的坐标和类别概率。 3. 损失函数定义:为了训练模型,需要定义损失函数。YOLO使用交叉熵损失函数来度量预测类别和真实类别之间的差异,以及预测边界和真实边界之间的差异。 4. 数据加载和预处理:使用PyTorch提供的数据加载函数加载和预处理图像数据集。预处理步骤可能包括图像缩放、裁剪、归一化和数据增强(如随机翻转、旋转等)。 5. 网络训练:使用加载的数据集和定义的网络模型进行训练。通过计算损失函数,并使用反向传播算法更新网络权重,来调整网络模型以更好地预测目标。 6. 目标检测:使用训练好的模型对新的图像进行目标检测。首先将图像输入网络,然后解码预测的边界和类别概率,最后根据设定的阈值和非极大值抑制方法,确定最终的目标检测结果。 总之,使用PyTorch实现YOLO目标检测算法需要进行数据准备、网络模型定义、损失函数定义、数据加载和预处理、网络训练以及目标检测等步骤。这个过程可以通过PyTorch提供的丰富功能和易于使用的API实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值