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_list
    df_nearby['x'] = x_list
    df_nearby['y'] = y_list
    df_nearby['w'] = w_list
    df_nearby['h'] = h_list
    df_nearby['score'] = score_list
    df_nearby['defect'] = defect_list
    df_nearby['date'] = time_list
    result_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_result


def 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_list
    df_nearby['x'] = x_list
    df_nearby['y'] = y_list
    df_nearby['w'] = w_list
    df_nearby['h'] = h_list
    df_nearby['score'] = score_list
    df_nearby['defect'] = defect_list
    df_nearby['date'] = time_list
    return 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 // 2
        line_1_centre_y = line_1_y + line_1_h // 2
        nearby_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 // 2
            line_2_centre_y = line_2_y + line_2_h // 2
            if(line_1==line_2):
                continue
            # if(line_1 in single_defect_list):
            #     break
            dis = 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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值