删除无object的xml避免产生错误副样本

import shutil, os
import cv2
from pathlib import Path 
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

save_draw_imgDir = r"E:\liufangtao\image\testjyz\2\b"
Path_dir = r"E:\liufangtao\image\testjyz\2\images"  #xml文件和jpg文件在一个文件夹
os.listdir(Path_dir)


def GetAnnotBoxLoc():
    for parent, dirnames, filenames in os.walk(Path_dir):
        for filename in filenames:
            if filename.endswith(".xml"):#判断字符串是否以指定的x结尾
                filename_qianzhui = Path(filename).stem #排除后缀名的文件或路径名
                img_file_path = os.path.join(Path_dir, filename_qianzhui) + ".jpg"
                xml_file_path = os.path.join(Path_dir, filename_qianzhui) + ".xml"
                copy_file = os.path.join(Path_dir, xml_file_path)

                tree = ET.ElementTree(file=copy_file) 
                root = tree.getroot()  
                ObjectSet = root.findall('object')
                i = 0
                for child in ObjectSet:
                    if child != "":
                        i= i+1
                if i == 0:
                    shutil.move(xml_file_path, save_draw_imgDir)
                    shutil.move(img_file_path, save_draw_imgDir)
                    # os.remove(xml_file_path)

GetAnnotBoxLoc()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,那我假设你的数据集中每张图片只对应一个标注文件,以下是可以实现分析样本大小分布、样本比例分布和样本 GT 与锚框的 IoU 分布的 Python 代码: ```python import os import cv2 import xml.etree.ElementTree as ET import numpy as np import matplotlib.pyplot as plt # 图像和标注文件所在目录 image_dir = "images" annotation_dir = "annotations" # 获取所有图像和标注文件路径 image_paths = [os.path.join(image_dir, file) for file in os.listdir(image_dir)] annotation_paths = [os.path.join(annotation_dir, file) for file in os.listdir(annotation_dir)] # 样本数据列表,每个元素为一个样本(图像、标注文件路径、锚框等信息) samples = [] # 遍历每个图像和标注文件,生成样本数据 for image_path, annotation_path in zip(image_paths, annotation_paths): # 加载图像和标注文件 image = cv2.imread(image_path) tree = ET.parse(annotation_path) root = tree.getroot() # 获取图像大小 image_h, image_w, _ = image.shape # 获取所有边界框的信息 bboxes = [] for obj in root.findall("object"): bbox = obj.find("bndbox") xmin = int(bbox.find("xmin").text) ymin = int(bbox.find("ymin").text) xmax = int(bbox.find("xmax").text) ymax = int(bbox.find("ymax").text) bboxes.append([xmin, ymin, xmax, ymax]) # 生成样本数据 if len(bboxes) > 0: samples.append({ "image_path": image_path, "annotation_path": annotation_path, "image_size": (image_w, image_h), "bboxes": np.array(bboxes) }) # 计算每个样本的大小和正负样本比例 sample_sizes = [] sample_ratios = [] for sample in samples: image_w, image_h = sample["image_size"] bbox_w = sample["bboxes"][:, 2] - sample["bboxes"][:, 0] bbox_h = sample["bboxes"][:, 3] - sample["bboxes"][:, 1] sample_sizes.append(image_w * image_h) sample_ratios.append(len(bbox_w[bbox_w > 0]) / len(bbox_w[bbox_w == 0])) # 绘制直方图 fig, axs = plt.subplots(1, 3, figsize=(15, 5)) axs[0].hist(sample_sizes, bins=20, color='steelblue', edgecolor='k') axs[0].set_xlabel('Sample Size') axs[0].set_ylabel('Frequency') axs[0].set_title('Distribution of Sample Sizes') axs[1].hist(sample_ratios, bins=20, color='steelblue', edgecolor='k') axs[1].set_xlabel('Positive/Negative Ratio') axs[1].set_ylabel('Frequency') axs[1].set_title('Distribution of Sample Ratios') ious = [] for sample in samples: image = cv2.imread(sample["image_path"]) for bbox in sample["bboxes"]: iou = calculate_iou(bbox, gt_bbox) ious.append(iou) axs[2].hist(ious, bins=20, color='steelblue', edgecolor='k') axs[2].set_xlabel('IoU') axs[2].set_ylabel('Frequency') axs[2].set_title('Distribution of IoUs') plt.show() ``` 其中,`image_dir` 和 `annotation_dir` 分别指定了图像和标注文件所在的目录,`calculate_iou` 函数计算两个边界框的 IoU。可视化结果包括三个直方图,分别对应样本大小分布、样本比例分布和样本 GT 与锚框的 IoU 分布。注意,这里使用了 `subplots` 函数在一张图中绘制了三个直方图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容洛凝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值