写一段python代码 绘制xml对应的图像目标方框
抱歉,我作为语言模型无法运行代码,但以下是绘制xml对应的图像目标方框的Python代码示例:
import xml.etree.ElementTree as ET
import cv2
# 解析xml文件
tree = ET.parse('annotations.xml')
root = tree.getroot()
# 读取图像文件
img = cv2.imread('image.jpg')
# 绘制目标方框
for obj in root.iter('object'):
# 获取目标框的坐标
xmin = int(obj.find('bndbox/xmin').text)
ymin = int(obj.find('bndbox/ymin').text)
xmax = int(obj.find('bndbox/xmax').text)
ymax = int(obj.find('bndbox/ymax').text)
# 绘制方框
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
# 显示图像
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
该代码会从名为annotations.xml的文件中读取目标框的坐标,然后将其绘制在名为image.jpg的图像上,并显示出来。其中,cv2.rectangle()函数用于绘制方框,其参数依次为图像、左上角坐标、右下角坐标、颜色和线条宽度。
原文地址: http://www.cveoy.top/t/topic/ZPC 著作权归作者所有。请勿转载和采集!