基于重叠和预测的lable的框架框

发布于 2025-02-03 07:53:11 字数 4227 浏览 1 评论 0原文

此处输入映像我有一个数据框

image     label    bb0         bb1                 bb2                 bb3
im1       L1    0.16602577  0.20398015          0.6750162              1.0
im1       L2    0.38657066  0.5192988000000001  0.45647129999999997    0.93410575
im1       L2    0.019637141 0.66581047          0.1369443              0.9160644
im1       L2    0.3700079   0.32554457          0.45773253             0.581625

在 ,BB3 是按顺序Y1,X1,Y2,X2的预测边界框的坐标。我想根据重叠区域(IOU)合并每个标签的边界框,如果IOU大于某个阈值,则将框合并,否则将其保留为原样。在最终数据框架中,我只需要更新的边界框。 可以在附件图像中看到场景。中间的两个橙色盒子将组合起来制作黄色盒子,所有其他盒子都将保持不变。最终数据将包含三行,一个用于L1,一个不是重叠的橙色盒,一个新的黄色框。我想以这种方式更新数据框。

我正在使用延迟代码 -

def iou(box1, box2):
    assert box1['bb1'] < box1['bb3']
    assert box1['bb0'] < box1['bb2']
    assert box2['bb1'] < box2['bb3']
    assert box2['bb0'] < box2['bb2']

    # determine the coordinates of the intersection rectangle
    x_left   = max(box1['bb1'], box2['bb1'])
    y_top    = max(box1['bb0'], box2['bb0'])
    x_right  = min(box1['bb3'], box2['bb3'])
    y_bottom = min(box1['bb2'], box2['bb2'])

    if x_right < x_left or y_bottom < y_top:
        iou1 = 0
    else:
        intersection_area = (x_right - x_left) * (y_bottom - y_top)
        # compute the area of both AABBs
        bb1_area = (box1["bb3"] - box1["bb1"]) * (box1["bb2"] - box1['bb0'])
        bb2_area = (box2["bb3"] - box2["bb1"]) * (box2["bb2"] - box2['bb0'])
        #intersection over union
        iou1 = intersection_area / float(bb1_area + bb2_area - intersection_area)
    return iou1


def get_iou(df):
    df_out = pd.DataFrame(columns = df.columns)
    for i in (df["image"].unique()):
        print(i)
        #df_out = pd.DataFrame(columns = df.columns)
        k1 = df[df["image"] == str(i)]
        k2 = k1["class"].value_counts().reset_index()
        k3 = k2[k2["class"] > 1]
        k8 = k2[k2["class"] == 1]
        k9 = df[(df["image"] == str(i)) & (df["class"].isin(k8["index"]))]
        df_out = df_out.append(k9, ignore_index = True)
        
        new_df = pd.DataFrame(columns = ["class","Combinations", "IOU"])
        for j in k3["index"].unique():
            k4 = df[(df["image"] == str(i)) & (df["class"] == str(j))]
            k4["unique_id"] = list(range(len(k4)))
            #k5 = k4
            #k6 = k5.to_dict(orient='records')
            #k7 = list(itertools.combinations(k6, 2))
            gg = list(itertools.combinations(k4["unique_id"], 2))
            #new_df = pd.DataFrame(columns = ["Combinations", "IOU"])
            #print(k7, "\n")
            df_out2 = pd.DataFrame(columns = df.columns)
            l = (0,1)
            while l in gg:
                #print(l)
                box1 = k4[k4["unique_id"] == l[0]].to_dict('records')[0]
                #print(box1)
                box2 = k4[k4["unique_id"] == l[1]].to_dict('records')[0]
                iou1 = iou(box1, box2)
                assert iou1 >= 0.0
                assert iou1 <= 1.0
                print(iou1)
                if iou1 <= 0.05:
                    print("No Overlap")
                else:
                    x_left_new   = min(box1["bb1"], box2["bb1"])
                    x_right_new  = max(box2["bb3"], box1["bb3"])
                    y_top_new    = min(box2["bb0"], box2["bb0"])
                    y_bottom_new = max(box1["bb2"], box2["bb2"])
                    new_bb = {'image' : box1["image"], 
                              'class' : box1['class'], 
                              'score' : np.nan, 
                              'bb0' : y_top_new, 
                              'bb1' : x_left_new, 
                              'bb2' : y_bottom_new, 
                              'bb3' : x_right_new,
                              'key' : box1["key"]}
                    k4 = k4.append(new_bb, ignore_index = True)
                    k4 = k4[~k4["unique_id"].isin(l)]
                    k4["unique_id"] = list(range(len(k4)))
                    gg = list(itertools.combinations(k4["unique_id"], 2))
                    df_out2 = df_out2.append(k4)

enter image description hereI have a data frame like -

image     label    bb0         bb1                 bb2                 bb3
im1       L1    0.16602577  0.20398015          0.6750162              1.0
im1       L2    0.38657066  0.5192988000000001  0.45647129999999997    0.93410575
im1       L2    0.019637141 0.66581047          0.1369443              0.9160644
im1       L2    0.3700079   0.32554457          0.45773253             0.581625

where bb0, bb1, bb2, bb3 are coordinates for predicted bounding boxes in order y1, x1, y2, x2. I want to merge the bounding boxes for each label based on the overlapping area (IOU) i.e. if iou is greater than some threshold then merge the boxes otherwise keep it as it is. In the final data frame i want updated bounding boxes only.
A scenario can be seen in the attached image. The two orange boxes in the middle will combine to make the yellow box and all other boxes will remain same. The final data will contain three rows, one for L1, one orange box which is not overlapping and one new yellow box. I want to update the dataframe in this manner.

I am using folowing code -

def iou(box1, box2):
    assert box1['bb1'] < box1['bb3']
    assert box1['bb0'] < box1['bb2']
    assert box2['bb1'] < box2['bb3']
    assert box2['bb0'] < box2['bb2']

    # determine the coordinates of the intersection rectangle
    x_left   = max(box1['bb1'], box2['bb1'])
    y_top    = max(box1['bb0'], box2['bb0'])
    x_right  = min(box1['bb3'], box2['bb3'])
    y_bottom = min(box1['bb2'], box2['bb2'])

    if x_right < x_left or y_bottom < y_top:
        iou1 = 0
    else:
        intersection_area = (x_right - x_left) * (y_bottom - y_top)
        # compute the area of both AABBs
        bb1_area = (box1["bb3"] - box1["bb1"]) * (box1["bb2"] - box1['bb0'])
        bb2_area = (box2["bb3"] - box2["bb1"]) * (box2["bb2"] - box2['bb0'])
        #intersection over union
        iou1 = intersection_area / float(bb1_area + bb2_area - intersection_area)
    return iou1


def get_iou(df):
    df_out = pd.DataFrame(columns = df.columns)
    for i in (df["image"].unique()):
        print(i)
        #df_out = pd.DataFrame(columns = df.columns)
        k1 = df[df["image"] == str(i)]
        k2 = k1["class"].value_counts().reset_index()
        k3 = k2[k2["class"] > 1]
        k8 = k2[k2["class"] == 1]
        k9 = df[(df["image"] == str(i)) & (df["class"].isin(k8["index"]))]
        df_out = df_out.append(k9, ignore_index = True)
        
        new_df = pd.DataFrame(columns = ["class","Combinations", "IOU"])
        for j in k3["index"].unique():
            k4 = df[(df["image"] == str(i)) & (df["class"] == str(j))]
            k4["unique_id"] = list(range(len(k4)))
            #k5 = k4
            #k6 = k5.to_dict(orient='records')
            #k7 = list(itertools.combinations(k6, 2))
            gg = list(itertools.combinations(k4["unique_id"], 2))
            #new_df = pd.DataFrame(columns = ["Combinations", "IOU"])
            #print(k7, "\n")
            df_out2 = pd.DataFrame(columns = df.columns)
            l = (0,1)
            while l in gg:
                #print(l)
                box1 = k4[k4["unique_id"] == l[0]].to_dict('records')[0]
                #print(box1)
                box2 = k4[k4["unique_id"] == l[1]].to_dict('records')[0]
                iou1 = iou(box1, box2)
                assert iou1 >= 0.0
                assert iou1 <= 1.0
                print(iou1)
                if iou1 <= 0.05:
                    print("No Overlap")
                else:
                    x_left_new   = min(box1["bb1"], box2["bb1"])
                    x_right_new  = max(box2["bb3"], box1["bb3"])
                    y_top_new    = min(box2["bb0"], box2["bb0"])
                    y_bottom_new = max(box1["bb2"], box2["bb2"])
                    new_bb = {'image' : box1["image"], 
                              'class' : box1['class'], 
                              'score' : np.nan, 
                              'bb0' : y_top_new, 
                              'bb1' : x_left_new, 
                              'bb2' : y_bottom_new, 
                              'bb3' : x_right_new,
                              'key' : box1["key"]}
                    k4 = k4.append(new_bb, ignore_index = True)
                    k4 = k4[~k4["unique_id"].isin(l)]
                    k4["unique_id"] = list(range(len(k4)))
                    gg = list(itertools.combinations(k4["unique_id"], 2))
                    df_out2 = df_out2.append(k4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文