当前位置: 首页 > news >正文

删除小目标 cocojson

coco 删除小目标

import glob
import os
import jsondef get_normalized_box(points):"""将任意顺序的点转换为 [x_min, y_min, x_max, y_max] 格式"""x_coords = [point[0] for point in points]y_coords = [point[1] for point in points]x_min = min(x_coords)y_min = min(y_coords)x_max = max(x_coords)y_max = max(y_coords)return [x_min, y_min, x_max, y_max]def is_small_box(box, min_size=10):"""检查框是否小于指定尺寸"""x_min, y_min, x_max, y_max = boxwidth = x_max - x_minheight = y_max - y_minreturn width * height < min_size * min_sizedef remove_small_boxes(json_path, min_size=10):"""删除文件中的小框"""with open(json_path, 'r', encoding='utf-8') as f:data = json.load(f)shapes = data.get('shapes', [])kept_shapes = []removed_boxes = []for shape in shapes:box = get_normalized_box(shape['points'])if is_small_box(box, min_size):width = box[2] - box[0]height = box[3] - box[1]removed_boxes.append({'label': shape['label'],'width': width,'height': height})else:kept_shapes.append(shape)# 如果有小框被删除,更新文件if removed_boxes:print(f"删除小框: {json_path}")for box_info in removed_boxes:print(f"  - 删除 {box_info['label']}: {box_info['width']:.1f}x{box_info['height']:.1f}")data['shapes'] = kept_shapeswith open(json_path, 'w', encoding='utf-8') as f:json.dump(data, f, ensure_ascii=False, indent=4)return True, len(removed_boxes)return False, 0if __name__ == '__main__':folder = r"D:\data\det_chantu\val_pen"dirs = glob.glob(os.path.join(folder, "*"))total_files = 0total_removed_boxes = 0for dir_a in dirs:json_files = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(dir_a) for j in i[-1] ifj.lower().endswith('.json')]print(f"=== 目录: {os.path.basename(dir_a)} ===")dir_files = 0dir_removed_boxes = 0for json_file in json_files:# json_file=r"D:\data\det_chantu\train_pen\20251028_1646_part007_seg\0_ok\28_19_0.json"removed, count = remove_small_boxes(json_file)if removed:dir_files += 1dir_removed_boxes += counttotal_files += 1total_removed_boxes += countif dir_removed_boxes > 0:print(f"目录统计: {dir_files} 个文件删除了小框, 共删除 {dir_removed_boxes} 个小框")print(f"\n总体统计: {total_files} 个文件删除了小框")print(f"总共删除 {total_removed_boxes} 个小框")

http://www.dtcms.com/a/572798.html

相关文章:

  • 汽车EDI:基于知行之桥的 Gnotec EDI解决方案
  • 垂直行业门户网站建设方案自己做的网站被黑了怎么办
  • 地图可视化实践录:leaflet学习之综合示例工程设计
  • 《Python工匠》第二章 数值与字符串 关键知识摘录与梳理
  • QuickSSO 与 ECreator 实操应用案例手册
  • Flutter Android Kotlin 插件编译错误完整解决方案
  • 网站设计问题网站开发包含的项目和分工
  • FPGA—ZYNQ学习UART环回(五)
  • 电动汽车充电云与移动应用基础设施的漏洞识别、攻击实验及缓解策略
  • PFMEA中的预防措施和探测措施区别
  • 做钢材的网站php 7 wordpress
  • 告别繁琐手工录入:智能银行票据套打软件,让制单效率飞跃
  • 【Java】理解 Java 中的 this 关键字
  • 在编译OpenHarmony遇到third_party/libnl编译报错的修复办法
  • 建c2c网站俄罗斯乌克兰为什么打仗
  • vue在获取某一个div的大小,怎么确保div渲染好,内容撑开后才去获取大小
  • ITIL 4 复习练习及解题思路
  • 数据结构:单链表(2)
  • MYSQL数据库--索引、视图练习
  • 2.2.5 运算方法和运算电路【2011统考真题】
  • 前端上传图片加裁剪功能
  • DevEco Studio 鸿蒙 引入lib中的文件
  • 简述数据库设计--范式、ER图
  • 【Linux】权限(1):初识权限与使用理解
  • 深圳专业做网站的公司河南企业建站系统信息
  • 企业门户网站设计报告wordpress接口
  • 基于大数据的天气分析与应用系统
  • spark读取table中的数据【hive】
  • 最后一轮征稿开启 | ACM出版 | 第二届大数据分析与人工智能应用学术会议(BDAIA2025)
  • 史诗级:在麒麟离线服务器上部署 Dify (含 Weaviate、Nginx 网关、FIP 及离线插件)