找回密码
 立即注册
首页 业界区 安全 AICube数据集不合法清洗解决方法

AICube数据集不合法清洗解决方法

田雅宁 16 小时前
1.png

通过kendryte云平台标注后下载的数据集在本地更改为Ai_Cube数据集目录格式后提示数据库不合法需要清洗。
最后发现堪智在线平台导出的数据集中的xml文件缺少size字段,可以通过脚本给xml文件添加size片段,添加结束之后就可以在aicube上面训练数据了,下面是脚本代码
2.png
  1. import os
  2. import cv2
  3. import xml.etree.ElementTree as ET
  4. # 路径配置
  5. image_dir = "JPEGImages"
  6. xml_dir = "Annotations"
  7. # 遍历所有XML文件
  8. for xml_file in os.listdir(xml_dir):
  9.     if not xml_file.endswith(".xml"):
  10.         continue
  11.     xml_path = os.path.join(xml_dir, xml_file)
  12.     tree = ET.parse(xml_path)
  13.     root = tree.getroot()
  14.     # 检查是否已经有 <size> 节点
  15.     if root.find("size") is not None:
  16.         print(f"[跳过] {xml_file} 已有 <size>")
  17.         continue
  18.     # 获取对应的图片路径
  19.     img_filename = root.find("filename").text
  20.     img_path = os.path.join(image_dir, img_filename)
  21.     if not os.path.exists(img_path):
  22.         print(f"[警告] 找不到对应图片:{img_filename}")
  23.         continue
  24.     # 读取图像大小
  25.     img = cv2.imread(img_path)
  26.     if img is None:
  27.         print(f"[错误] 无法读取图像:{img_filename}")
  28.         continue
  29.     height, width, depth = img.shape
  30.     # 创建 <size> 节点并插入到  下
  31.     size_elem = ET.Element("size")
  32.     ET.SubElement(size_elem, "width").text = str(width)
  33.     ET.SubElement(size_elem, "height").text = str(height)
  34.     ET.SubElement(size_elem, "depth").text = str(depth)
  35.     root.insert(1, size_elem)  # 插入在 <filename> 之后更符合VOC结构
  36.     # 保存修改后的XML
  37.     tree.write(xml_path, encoding="utf-8", xml_declaration=True)
  38.     print(f"[更新] 已添加 <size> 至 {xml_file}")
复制代码
3.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册