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创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财