写了一个用YOLO的txt文件画框的函数

最近让人标了些图片,想看一下他标的是不是符合我的要求,但txt文件毕竟还是不好看,所以写了一个画框的函数, 不过我的目标检测只有一个类,不能区分类别

def yolo_txt2cv_points(file_path,save_path):#前者是需要的文件,文件包含两个文件夹images和labels 后者是需要保存在哪
    images_path = os.path.join(file_path,'images')
    labels_path = os.path.join(file_path,'labels')
    print(len(os.listdir(images_path)))
    for file in os.listdir(images_path):
        image_path = os.path.join(images_path,file)
        image = cv2.imread(image_path)
        file_name = file.split('.')[0]
        label_file = file_name + '.txt'
        label_path = os.path.join(labels_path, label_file)
        width = image.shape[1]
        height = image.shape[0]
        with open(label_path, 'r') as f:
            rects = f.readlines()
            for rect in rects:
                a = float(rect[2:10])
                b = float(rect[11:19])
                c = float(rect[20:28])
                d = float(rect[29:37])
                box_width = c*width
                box_height = d*height
                first_point = (int(a*width - box_width/2), int(b*height - box_height/2))
                last_point = (int(a*width + box_width/2), int(b*height + box_height/2))
                # print(first_point,last_point)
                cv2.rectangle(image, first_point, last_point, (0, 255, 0), 2)
                save = os.path.join(save_path,file)
                # print(save)
                cv2.imwrite(save,image)
                # print(a,b,c,d)

函数效果如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最后一只三脚兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值