图片和标注同步缩放

转发于:https://blog.csdn.net/weixin_44554475/article/details/102666636

仅保存关键信息,更详细请移步原博

一、xml数据的读取

​import xml.etree.cElementTree as et


tree = et.parse("./datasets/01/双/微信图片_20190820094420.xml")
root = tree.getroot()

fileName = root.find("filename").text		# 获取文件名

# 读取标注框信息
for obj in root.findall("object"):
    name = obj.find("name").text
    bndbox = obj.find("bndbox")
    xmin = int(bndbox.find('xmin').text)
    ymin = int(bndbox.find('ymin').text)
    xmax = int(bndbox.find('xmax').text)
    ymax = int(bndbox.find('ymax').text)
    print(name, xmin, ymin, xmax, ymax)

二、图片和标注数据的同步resize

计算出缩放比例,将标注数据乘相应缩放比例

import cv2


img = cv2.imread("./datasets/01/20190820094420.jpg")
height, weight = img.shape[:2]
x = 416/weight              # 图片宽的缩放比例
y = 416/height              # 图片稿的缩放比例
img = cv2.resize(img, (416, 416))

# xmin, ymin, xmax, ymax分别为xml读取的坐标信息
tl = (int(xmin*x), int(ymin*y))
br= (int(xmax*x), int(ymax*y))
cv2.rectangle(img, tl, br, (255, 0, 0), 1)

 

你可能感兴趣的:(图片和标注同步缩放)