根据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
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值