更改目标检测标签xml文件中的path

实现目标

根据labelme或labelimg生成的xml文件中,有对应图像的路径。当别人帮你打标签时,对应图像的路径是别人电脑上面的,并非是你想要的。 
说明路径备注
原始xml文件存放路径D:\aaa\source/别人帮你打的标签
现在xml文件存放路径D:\aaa\target/你自己想要放的位置
图像路径D:\aaa\img/假设你最开始图像就是在这里

在这里插入图片描述

代码

import xml.etree.ElementTree as ET
import os
# xml文件原始存放路径
source_path = r"D:\aaa\yuanshi/"
# 修改后的xml文件存放路径
target_path = r"D:\aaa\target/"  
img_path = r"D:\aaa\img/"  
files = os.listdir(source_path)
for xmlFile in files:
    if xmlFile.endswith('.xml'):
        tree = ET.ElementTree(file=source_path + xmlFile)
        root = tree.getroot()
        root[0].text = 'images'
        root[2].text = img_path + xmlFile
        root[2].text = root[2].text.replace('xml', 'jpg')
        tree.write(target_path + xmlFile)
print('全部替换完成')
可以使用OpenCV和xml.etree.ElementTree模块实现,代码如下: ```python import cv2 import numpy as np import os import xml.etree.ElementTree as ET folder_A = "path/to/folder/A" folder_B = "path/to/folder/B" # Function to generate VOC format XML file def generate_xml(filename, width, height, objects): annotation = ET.Element("annotation") ET.SubElement(annotation, "folder").text = "Folder name" ET.SubElement(annotation, "filename").text = filename ET.SubElement(annotation, "path").text = os.path.join(os.getcwd(), filename) size = ET.SubElement(annotation, "size") ET.SubElement(size, "width").text = str(width) ET.SubElement(size, "height").text = str(height) ET.SubElement(size, "depth").text = "3" for obj in objects: ob = ET.SubElement(annotation, "object") ET.SubElement(ob, "name").text = "slip" ET.SubElement(ob, "pose").text = "Unspecified" ET.SubElement(ob, "truncated").text = "0" ET.SubElement(ob, "difficult").text = "0" bbox = ET.SubElement(ob, "bndbox") ET.SubElement(bbox, "xmin").text = str(obj[0]) ET.SubElement(bbox, "ymin").text = str(obj[1]) ET.SubElement(bbox, "xmax").text = str(obj[2]) ET.SubElement(bbox, "ymax").text = str(obj[3]) xml_str = ET.tostring(annotation) return xml_str # Loop through all images in folder A for file in os.listdir(folder_A): if file.endswith(".bin"): # Read binary image img_bin = np.fromfile(os.path.join(folder_A, file), np.uint8) img = cv2.imdecode(img_bin, cv2.IMREAD_GRAYSCALE) # Convert binary to binary edge image ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) edges = cv2.Canny(thresh, 100, 200) # Find contours and draw bounding boxes contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) objects = [] for cnt in contours: # Approximate contour as polygon epsilon = 0.01 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) # Check if polygon has 4 sides (i.e., a rectangle) if len(approx) == 4: x, y, w, h = cv2.boundingRect(approx) objects.append([x, y, x + w, y + h]) # Generate XML file xml_str = generate_xml(file + ".xml", img.shape[1], img.shape[0], objects) # Save XML file with open(os.path.join(folder_B, file + ".xml"), "wb") as f: f.write(xml_str) ``` 需要安装OpenCV和numpy模块,代码的文件夹路径需要根据实际情况修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值