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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互