Search code examples
pythonlistocrnested-listsbounding-box

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]]

How to remove the nested list and make a flat list for all the bounding box values in 'bounds' variable?


Solution

  • This approach makes a list which reduces the nested list into a flattened list found in the first element -> bound[0] of each entry of bounds, the other elements are unpacked in the tuple, after the flattened list.

    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)
    

    Output:

    [([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)]
    

    Alternatively if you only intented to keep the first flattened list you could do this:

    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)
    

    Output:

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