我正在编写代码以对来自对象检测模型的边界框执行非最大抑制。我有一个地理定位边界框的主列表,这些边界框是geolocated_annotations =[[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]]
坐标的嵌套列表。 (x3,y3)和(x4,y4)坐标是边界框的地理位置。我在深度学习模型中使用了嵌入式NMS函数,结果是keep=[[x1, y1, x2, y2]]
坐标的嵌套列表。 keep
中的结果没有最后两个坐标,因为嵌入式函数未考虑地理位置。
我想将keep
列表与geolocated_annotations
列表进行比较,并从keep
中找到所有等于(x1,y1)和(x2,(x1,y1)和x2,y2)的坐标, y2)在geolocated_annotations
中的坐标。 keep
中的这两个坐标与geolocated_annotations
中的地理位置坐标相关联,因此,如果(x1,y1)和(x2,y2)值相等,则(x3,y3)和(x4,y4)主列表中的值可以与它们关联。我需要以包含所有四个(x1,y1),(x2,y2),(x3,y3)和(x4,y4)坐标的列表结尾。
我有一个半工作代码,但是最终它在最终列表中比在keep
列表中保留了[[more坐标。遍历所有坐标也需要很长时间。
geolocated_annotations=[[(624169.99807, 4344801.75893), (624170.64013, 4344801.34655), (624159.5841700002, 4344797.0139500005), (624159.1195900001, 4344797.0139500005)], [(624159.9808900001, 4344799.4673500005), (624160.4089300002, 4344799.4673500005), (624160.4089300002, 4344798.887930001), (624159.9808900001, 4344798.887930001)]]
keep=[[624169.99807, 4344801.75893, 624170.64013, 4344801.34655],
[624151.50361, 4344753.26513, 624151.93165, 4344752.69093],
[624150.44917, 4344812.13107, 624150.88243, 4344811.58297]]
这是我的入门代码:
bboxes_keep=[] for i in geolocated_annotations: for j in keep: if i[0][0]==j[0] and i[0][1]==j[1] and i[1][0]==j[2] and i[2][1]==j[3]: bboxes_keep.append(i)
预期输出示例:
bboxes_keep=[[(624169.99807, 4344801.75893), (624170.64013, 4344801.34655), (624159.5841700002, 4344797.0139500005), (624159.1195900001, 4344797.0139500005)]]
关于如何使我的代码更加精致和准确的任何想法?
geolocated_annotations=[[(624169.99807, 4344801.75893), (624170.64013, 4344801.34655), (624159.5841700002, 4344797.0139500005), (624159.1195900001, 4344797.0139500005)], [(624159.9808900001, 4344799.4673500005), (624160.4089300002, 4344799.4673500005), (624160.4089300002, 4344798.887930001), (624159.9808900001, 4344798.887930001)]]
keep=[[624169.99807, 4344801.75893, 624170.64013, 4344801.34655],
[624151.50361, 4344753.26513, 624151.93165, 4344752.69093],
[624150.44917, 4344812.13107, 624150.88243, 4344811.58297]]
datastore = {}
for i in keep:
try:
datastore[i[0]][i[1]] = True
except:
datastore[i[0]] = {i[1]:True}
try:
datastore[i[2]][i[3]] = True
except:
datastore[i[2]] = {i[3]:True}
result = []
for row in geolocated_annotations:
for x,y in row:
try:
match = datastore[x][y]
result.append([x,y])
except:
pass
注意:我不得不对两个列表的结构进行一些假设,因为正如评论者所指出的那样,您的示例似乎有一些错误。如果我假设有误,请修正您的代码,然后通知我。