目标检测脚本之统计数据集中某个类别某个尺寸的目标数量

   
# 计算label标签中各个尺寸的数量分布
import os
import shutil
import cv2
from tqdm import tqdm

def calculate(image_dir,label_dir):
    # 10以下尺寸数量
    small_10_count = 0      # 10 以下尺寸数量
    # 10-32 尺寸数量
    small_10_32_count = 0    #  10-32 尺寸数量
    # 大于 32尺寸数量
    other_object_count = 0   # 大于 32尺寸数量

    # 获取标签文件列表
    label_file_list = os.listdir(label_dir)
    # 遍历标签文件列表
    for label_file in tqdm(label_file_list):
        # 拼接图片路径
        image_path = os.path.join(image_dir,label_file.replace("txt","jpg"))
        # 拼接标签路径
        label_path = os.path.join(label_dir,label_file)
        # 如果图片路径不存在,则跳过当前循环
        if not os.path.exists(image_path):
            continue
        # 读取图片
        image = cv2.imread(image_path)
        # 获取图片的宽和高
        image_w,image_h = image.shape[1],image.shape[0]

        with open(label_path,"r",encoding='utf-8') as f:
            # 读取标签文件内容,按行存储到labels列表中
            labels = f.readlines()
            # 遍历标签列表
            for label in labels:
                # 将标签内容按空格分割成items列表
                items = label.split(" ")
                # 如果items列表的第一个元素为'0',则执行以下判断和计数操作
                if items[0] == '0':
                    # 如果图片的宽小于10或高小于10,则small_10_count加1
                    if (float(items[2]) * image_w < 10) or (float(items[3]) * image_h < 10):
                        small_10_count +=1
                    # 如果图片的宽小于32或高小于32,则small_10_32_count加1
                    elif (float(items[2]) * image_w < 32.0) or (float(items[3]) * image_h < 32.0):
                        small_10_32_count +=1
                    # 其他情况则other_object_count加1
                    else:
                        other_object_count +=1
            
    return small_10_count,small_10_32_count,other_object_count                            


if __name__ == "__main__":
    image_dir = '' # 图片地址
    label_dir = '' # 标签地址
    
   
    small_10_counts,small_10_32_count,other_counts = calculate(image_dir,label_dir)
    
    print(small_10_counts,small_10_32_count,other_counts)
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏代有工的玉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值