我正在尝试删除包含边界框值的嵌套列表。我正在使用 easyocr 库来提取文本和边界框值


bounds = reader.readtext(np.array(images[0]), min_size=0, slope_ths=0.2, ycenter_ths=0.7, height_ths=0.6, width_ths=0.8,decoder='beamsearch', beamWidth=10)
print(bounds)

[([[1002126],[1212126],[121022],[100222]],‘uz5’,0.048652395606040955(,([[177179],[349179],[349241],[177241]],"OIZI",0.793368584632874(,([[180236],[422236],[42268],[180268]],"Oki Electric Industry Co",0.4165174067020416(]

print(bounds[0][0])

[[1002126],[120126],[1210222],[100222]]

如何删除嵌套列表并为"bounds"变量中的所有边界框值制作一个平面列表

这种方法生成一个list,它将嵌套列表简化为第一个元素中的扁平列表->边界的每个条目的bound[0],在展平列表之后,元组中的其他元素被解包。

bounds = [([[1002, 126], [1210, 126], [1210, 222], [1002, 222]], 'uz5', 0.048652395606040955), ([[177, 179], [349, 179], [349, 241], [177, 241]], 'OIZI', 0.7936368584632874), ([[180, 236], [422, 236], [422, 268], [180, 268]], 'Oki Electric Industry Co', 0.4165174067020416)]
out = [(sum(bound[0], []), *bound[1:]) for bound in bounds]
print(out)

输出:

[([1002, 126, 1210, 126, 1210, 222, 1002, 222], 'uz5', 0.048652395606040955), 
([177, 179, 349, 179, 349, 241, 177, 241], 'OIZI', 0.7936368584632874), 
([180, 236, 422, 236, 422, 268, 180, 268], 'Oki Electric Industry Co', 0.4165174067020416)]

或者,如果你只想保留第一个扁平列表,你可以这样做:

bounds = [([[1002, 126], [1210, 126], [1210, 222], [1002, 222]], 'uz5', 0.048652395606040955), ([[177, 179], [349, 179], [349, 241], [177, 241]], 'OIZI', 0.7936368584632874), ([[180, 236], [422, 236], [422, 268], [180, 268]], 'Oki Electric Industry Co', 0.4165174067020416)]
out = [sum(bound[0], []) for bound in bounds]
print(out)

输出:

[[1002, 126, 1210, 126, 1210, 222, 1002, 222], 
[177, 179, 349, 179, 349, 241, 177, 241], 
[180, 236, 422, 236, 422, 268, 180, 268]]