如何根据目标检测结果,根据原始xml快速分析某一类的问题

1. 获取待分析类别的图片

"""
根据xml解析的结果,将特定的类别, 图片和xml,保存到新的文件夹
"""
import os
from shutil import copy
from lxml import etree 
origin_folder = "/Data02/decoded_det_seg/det_img/val"  /Data02/decoded_det_seg/det_img
dst_folder = '/home/xxx/datasets/val_sub_2'
os.makedirs(dst_folder, exist_ok=True)

class_sub = 'traffic_lights'

origin_folder_img = os.path.join(origin_folder, 'images')
origin_folder_xml = os.path.join(origin_folder, 'xmls_15')

dst_folder_img = os.path.join(dst_folder,class_sub, 'images')
dst_folder_xml = os.path.join(dst_folder,class_sub, 'xmls')

os.makedirs(dst_folder_img, exist_ok=True)
os.makedirs(dst_folder_xml, exist_ok=True)


def parse_xml_to_dict(xml):
        """
        将xml文件解析成字典形式, 参考tensorflow的recursive_parse_xml_to_dict
        Args:
            xml: xml tree obtained by parsing XML file contents using lxml.etree

        Returns:
            Python dictionary holding XML contents.
        """

        if len(xml) == 0:  # 遍历到底层,直接返回tag对应的信息
            return {xml.tag: xml.text}

        result = {}
        for child in xml:
            child_result = parse_xml_to_dict(child)  # 递归遍历标签信息
            if child.tag != 'object':
                result[child.tag] = child_result[child.tag]
            else:
                if child.tag not in result:  # 因为object可能有多个,所以需要放入列表里
                    result[child.tag] = []
                result[child.tag].append(child_result[child.tag])
        return {xml.tag: result}



for file in os.listdir(origin_folder_xml):
    img_file = file.replace('xml', 'jpg')
    
    src_xml_path = os.path.join(origin_folder_xml, file)
    src_img_path = os.path.join(origin_folder_img, img_file)
    assert os.path.join(src_img_path)

    
    with open(src_xml_path) as fid:
        xml_str = fid.read()
    xml_str = xml_str.encode('utf-8')  # ascii
    xml = etree.fromstring(xml_str)
    
    data = parse_xml_to_dict(xml)["annotation"]
    if 'object' in data.keys():
        for index, obj in enumerate(data["object"]):
            class_name = obj["name"]
            if class_sub==class_name:
                print(src_xml_path)
                dst_xml_path = os.path.join(dst_folder_xml, file)
                dst_img_path = os.path.join(dst_folder_img, img_file)

                copy(src_img_path, dst_img_path)
                copy(src_xml_path, dst_xml_path)
                continue


2. 分析该类别的预测结果

只把该类的预测结果框出来

target_idx = class_names.index('traffic_lights')
    for label in dets:
        if label != target_idx:
            continue
        for bbox in dets[label]:
            score = bbox[-1]
            if score > score_thresh:
                x0, y0, x1, y1 = [int(i) for i in bbox[:4]]
                all_box.append([label, x0, y0, x1, y1, score])

3. 叠加原始的xml,只用该类

import os
from shutil import copy

import numpy as np
from lxml import etree
import cv2 as cv
from PIL import Image

origin_folder = r"C:\Users\xxx\Desktop\img_no_aug_bicycle_wh"  

class_sub = 'wheel'

origin_folder_img = os.path.join(origin_folder, class_sub, 'img_tmp')
origin_folder_xml = os.path.join(origin_folder, class_sub, 'xml_tmp')


def parse_xml_to_dict(xml):
    """
        将xml文件解析成字典形式, 参考tensorflow的recursive_parse_xml_to_dict
        Args:
            xml: xml tree obtained by parsing XML file contents using lxml.etree

        Returns:
            Python dictionary holding XML contents.
        """

    if len(xml) == 0:  # 遍历到底层,直接返回tag对应的信息
        return {xml.tag: xml.text}

    result = {}
    for child in xml:
        child_result = parse_xml_to_dict(child)  # 递归遍历标签信息
        if child.tag != 'object':
            result[child.tag] = child_result[child.tag]
        else:
            if child.tag not in result:  # 因为object可能有多个,所以需要放入列表里
                result[child.tag] = []
            result[child.tag].append(child_result[child.tag])
    return {xml.tag: result}


for file in os.listdir(origin_folder_xml):
    file_path = os.path.join(origin_folder_xml, file)
    img_path = os.path.join(origin_folder_img, file.replace('xml', 'jpg'))
    img = cv.imread(img_path)
    
    print(file_path)
    with open(file_path) as fid:
        xml_str = fid.read()
    xml_str = xml_str.encode('utf-8')  # ascii
    xml = etree.fromstring(xml_str)

    data = parse_xml_to_dict(xml)["annotation"]
    file_name_t = data["filename"]
    w = data['size']['width']
    h = data['size']['height']
    if 'object' in data.keys():
        bboxes = []
        for index, obj in enumerate(data["object"]):
            # 获取每个object的box信息
            xmin = int(float(obj["bndbox"]["xmin"]))
            xmax = int(float(obj["bndbox"]["xmax"]))
            ymin = int(float(obj["bndbox"]["ymin"]))
            ymax = int(float(obj["bndbox"]["ymax"]))
            class_name = obj["name"]
            
            if class_name == class_sub:
                cv.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 0, 255), 1)
               
    cv.imwrite(img_path, img)
   

如图,预测结果是黄框,xml标注的gt是红框
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值