我正在编写代码以对来自对象检测模型的边界框执行非最大抑制。我有一个地理定位边界框的主列表,这些边界框是geolocated_annotations =[[(x1, y1), (x2, y2), (x3, y3), (x4, y4)]]
坐标的嵌套列表。 NMS测试的结果是keep=[[(x1, y1), (x2, y2)]]
坐标的嵌套列表,也是我要保留在最终列表中的坐标。我想将keep
列表与geolocated_annotations
列表进行比较,并从geolocated_annotations
中删除不在keep
中的所有坐标。
我有一个半工作代码,但是最终它在最终列表中比在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], [624169.99807, 4344801.75891], [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}
result = []
for row in geolocated_annotations:
for x,y in row:
try:
match = datastore[x][y]
result.append([x,y])
except:
pass
注意:我不得不对两个列表的结构进行一些假设,因为正如评论者所指出的那样,您的示例似乎有一些错误。如果我假设有误,请修正您的代码,然后通知我。