labelImg 标签处理

26 篇文章 1 订阅
5 篇文章 0 订阅

labelimg 标签处理

xyxy 转 xywh

# 将x1, y1, x2, y2转换成yolov5所需要的x, y, w, h格式
def xyxy2xywh(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[2]) / 2 * dw
    y = (box[1] + box[3]) / 2 * dh
    w = (box[2] - box[0]) * dw
    h = (box[3] - box[1]) * dh
    return (x, y, w, h)         # 返回的都是标准化后的值

xywh 转 xyxy

def xywh2xyxy(size, box):
    width = size[0]
    height = size[1]
    x1 = box[0]*width - 0.5*box[2]*width
    x2 = box[0]*width + 0.5*box[2]*width
    y1 = box[1]*height - 0.5*box[3]*height
    y2 = box[1]*height + 0.5*box[3]*height
    return (x1,y1,x2,y2)

读xml

import os
import xml.etree.ElementTree as ET

classes = [类别信息]
tree = ET.parse(label_file)
root = tree.getroot()
size = root.find('size')            # 图片的shape值
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
    difficult = obj.find('difficult').text
    cls = obj.find('name').text
    if cls not in classes or int(difficult) == 1:
        continue
     # 将名称转换为id下标
     cls_id = classes.index(cls)
     # 获取整个bounding box框
     bndbox = obj.find('bndbox')
     # xml给出的是x1, y1, x2, y2
     box = [float(bndbox.find('xmin').text), float(bndbox.find('ymin').text), float(bndbox.find('xmax').text),
     float(bndbox.find('ymax').text)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值