当前位置:网站首页>Statistics of various target quantities of annotations (XML annotation format)

Statistics of various target quantities of annotations (XML annotation format)

2022-06-26 09:14:00 G fruit

Be careful :

The code in this article suggests that the image and annotation files should be separated

The picture exists images Under the folder ( It's not used here
Annotation file exists annotations Under the folder

There is also a tqdm Library to display the progress bar , No need to comment out

import xml.etree.ElementTree as ET # Import xml modular 
import pickle
import os
import glob
from os import listdir, getcwd
from os.path import join
from tqdm import tqdm# Missing packages need to be installed 


def class_num(_dir,class_name,dataset,nums):
    result = {
    }
    for clss in class_name:
        result[clss]=0 # Number of labels per category 
    #print(result)
    result["other"]=0# Number of other labels 
    result["sum"]=0# Total number of labels 
    
    #total Parameter sets the total length of the progress bar 
    pbar = tqdm(total=nums,desc="%s-porcess"%dataset,unit="xml")

    for xmll in glob.glob(_dir+"*.xml"):
        #print(xml)
        #time.sleep(0.05)
        pbar.update(1)# The length of each update progress bar 
        with open(xmll,"r",encoding="utf-8") as f:
            xml = ET.parse(f)
            # root = xml.getroot()
            # print(root.findall("object"))
            for obj in xml.iter('object'):
                result["sum"] = result["sum"]+1
                if obj.find("name").text not in class_name:
                    result["other"] = result["other"]+1
                for clsn in class_name:
                    if obj.find("name").text == clsn: # Make statistics according to the labeled tag name  
                        result[clsn] = result[clsn]+1
    pbar.close()# Close occupied resources 
    return result

if __name__ == '__main__':
    
    train_dir="E:/DL/detectron2/SwinT_detectron2/datasets/new/train/annotations/"
    test_dir="E:/DL/detectron2/SwinT_detectron2/datasets/test/annotations/"
      
    class_name = ["0","1","2"]# Name of each category when marking 
    
    train_num = len(os.listdir(train_dir))# Calculate the number of label files 
    test_num = len(os.listdir(test_dir))
    print(train_num,test_num)
    
    results1 = class_num(train_dir,class_name,"train",train_num)   
    results2 = class_num(test_dir,class_name,"test",test_num)
                   
    print("\n\n Training set : ",results1)
    print("\n Test set : ",results2)



原网站

版权声明
本文为[G fruit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170552523598.html