python - Getting digits in an order (left to right) after OCR from x,y coordinates -
so wrote ocr script grabs image , performs ocr , return x,y coordinates digit , when plot underlying image.
the x,y coordinates digits not predicted sequentially contours detected (almost randomly).
is there way them arrange in readable left right order?
if able x, y coordinate pairs, can sort y, x values.
xy_list = ((2, 1), (1, 2), (0, 1), (1, 0), (0, 0), (2, 2), (1, 1), (0, 2), (2, 0)) xy_list = sorted(xy_list, key=lambda tup: (tup[1], tup[0])) print(xy_list[:3]) print(xy_list[3:6]) print(xy_list[6:])
gives
[(0, 0), (1, 0), (2, 0)] [(0, 1), (1, 1), (2, 1)] [(0, 2), (1, 2), (2, 2)]
this won't account slight variations in y values should considered on same line, off-center.
edit
this lets y vary specified offset. (so set 10.0, in case). note i'm using python 3, if you're using 2, make sure make offset float.
offset = .5 xy_list = ((2, 1), (1, 2.1), (0, 1), (1, 0.1), (0, 0), (2, 1.6), (1, 1.3), (0, 2), (2, 0.3)) xy_list = sorted(xy_list, key=lambda tup: ( round(tup[1]/(2*offset)), tup[0] ))
gives
[(0, 0), (1, 0.1), (2, 0.3)] [(0, 1), (1, 1.3), (2, 1)] [(0, 2), (1, 2.1), (2, 1.6)]
Comments
Post a Comment