我正在尝试删除包含边界框值的嵌套列表。我正在使用 easyocr 库来提取文本和边界框值
I'm trying to remove nested list which contains bounding box values. I'm using easyocr library to extract the text and bounding box values
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)
[([[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)]
print(bounds[0][0])
[[1002, 126], [1210, 126], [1210, 222], [1002, 222]]
如何删除嵌套列表并为 '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]]
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)
[([[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)]
print(bounds[0][0])
[[1002, 126], [1210, 126], [1210, 222], [1002, 222]]
如何删除嵌套列表并为 '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]]