python 去除 txt 文件中,周围检测框的重复框

2024-04-27 18:58

本文主要是介绍python 去除 txt 文件中,周围检测框的重复框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

去重 CV 检测框的 50 pix 像素值内的重复框:
如果缺陷类型一样,有重复框则取检测分数最大的框;
如果缺陷类型不一致,则保留两个框;
思想:

  1. 先找到每一个缺陷的几何中心 50 pix 内的所有框;
  2. 按照框进行缺陷类型分组:做去重处理;
  3. 删除位于同一范围内的缺陷;
  4. 合并小于 50 像素值和大于 50 pix 的框;

用法:依赖以下 3 个方法,直接调用第 3 个方法 remove_duplicate(txt_path, nearby_pix)
输出是去重后的生成 txt 。原始 txt 格式如下:
id 名称,x, y,w, h, 检测分数,缺陷类型,时间
在这里插入图片描述

# txt to df and select the max score
def choose_only_one(nearby_list, defect_list_):df_nearby = pd.DataFrame()id_list = []x_list = []y_list = []w_list = []h_list = []score_list = []defect_list = []time_list = []for ele_nearby in nearby_list:ele_nearby = ele_nearby.split(',')id_list.append(ele_nearby[0])x_list.append(ele_nearby[1])y_list.append(ele_nearby[2])w_list.append(ele_nearby[3])h_list.append(ele_nearby[4])score_list.append(ele_nearby[5])defect_list.append(ele_nearby[6])time_list.append(ele_nearby[7][:-1])# time_list.append(ele_nearby[7])df_nearby['id'] = id_listdf_nearby['x'] = x_listdf_nearby['y'] = y_listdf_nearby['w'] = w_listdf_nearby['h'] = h_listdf_nearby['score'] = score_listdf_nearby['defect'] = defect_listdf_nearby['date'] = time_listresult_list = []df_result = pd.DataFrame()for ele_defect in defect_list_:df_nearby_temp = df_nearby[df_nearby['defect']==str(ele_defect)]df_nearby_max = df_nearby_temp[df_nearby_temp['score'] == np.max(df_nearby_temp['score'])]# result_list.append(df_nearby_max)df_result = pd.concat([df_result, df_nearby_max], axis=0)return df_resultdef txt_to_df(nearby_list):df_nearby = pd.DataFrame()id_list = []x_list = []y_list = []w_list = []h_list = []score_list = []defect_list = []time_list = []for ele_nearby in nearby_list:ele_nearby = ele_nearby.split(',')id_list.append(ele_nearby[0])x_list.append(ele_nearby[1])y_list.append(ele_nearby[2])w_list.append(ele_nearby[3])h_list.append(ele_nearby[4])score_list.append(ele_nearby[5])defect_list.append(ele_nearby[6])time_list.append(ele_nearby[7][:-1])# time_list.append(ele_nearby[7])df_nearby['id'] = id_listdf_nearby['x'] = x_listdf_nearby['y'] = y_listdf_nearby['w'] = w_listdf_nearby['h'] = h_listdf_nearby['score'] = score_listdf_nearby['defect'] = defect_listdf_nearby['date'] = time_listreturn df_nearby# remove dumplicate jiaoji txt
def remove_duplicate(txt_path, nearby_pix):
'''
txt_path : txt 文件的路径
nearby_pix : 邻近范围的像素值
'''work_dir = './factory_result_dashboard_txt/'if(not os.path.exists(work_dir)):os.makedirs(work_dir)f1=open(txt_path,'r')f1_1 = open(txt_path, 'r').read()if(len(f1_1)<=10):if(os.path.exists(txt_path)):shutil.copy(txt_path, work_dir)lines_1_list = list(set(f1.readlines()))lines_2_list = copy.deepcopy(lines_1_list)single_defect_list = []df_single_defect = pd.DataFrame()# single_defect_list_1 = []for line_1 in lines_1_list:line_1_ele = line_1.split(',')line_1_x = float(line_1_ele[1])line_1_y = float(line_1_ele[2])line_1_w = float(line_1_ele[3])line_1_h = float(line_1_ele[4])line_1_score = float(line_1_ele[5])line_1_defect = float(line_1_ele[6])line_1_centre_x = line_1_x + line_1_w // 2line_1_centre_y = line_1_y + line_1_h // 2nearby_list = []defect_list = []for line_2 in lines_2_list:line_2_ele = line_2.split(',')line_2_x = float(line_2_ele[1])line_2_y = float(line_2_ele[2])line_2_w = float(line_2_ele[3])line_2_h = float(line_2_ele[4])line_2_score = float(line_2_ele[5])line_2_defect = float(line_2_ele[6])line_2_centre_x = line_2_x + line_2_w // 2line_2_centre_y = line_2_y + line_2_h // 2if(line_1==line_2):continue# if(line_1 in single_defect_list):#     breakdis = np.sqrt(math.pow(abs(line_1_centre_x - line_2_centre_x), 2) + math.pow(abs(line_1_centre_y - line_2_centre_y), 2))nearby_pix = float(nearby_pix)if(dis <= nearby_pix):if(line_1 not in nearby_list):nearby_list.append(line_1)defect_list.append(line_1_defect)if(line_2 not in nearby_list):nearby_list.append(line_2)defect_list.append(line_2_defect)defect_list = list(set(defect_list))defect_list = map(int, defect_list)df_res = choose_only_one(nearby_list, defect_list) # select only one defect# single_defect_list.extend(df_res)df_single_defect = pd.concat([df_single_defect, df_res], axis=0)nearby_list = map(str, nearby_list)for ele_nearby in nearby_list:lines_1_list.remove(ele_nearby)df_lines_1 = txt_to_df(lines_1_list)df_single_defect = pd.concat([df_single_defect, df_lines_1], axis=0)df_single_defect = df_single_defect.drop_duplicates()# df_single_defect = df_single_defect.drop([0])df_single_defect.to_csv(work_dir + txt_path, sep=',', header=None, index=False)

这篇关于python 去除 txt 文件中,周围检测框的重复框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/941273

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert