I am trying to sort contours for OCR to detect them, the problem is that I have tried to sort the contours from left to right and top to bottom but that does not help. I have text on two lines where I want the contours in first line to be sorted left to right and then the same for the second line. Have tried several ways to figure this out but I keep failing at it. Below is a code snippet which shows what I have tried to implement
def get_precedence(contour, cols):
tolerance_factor = 10
origin = cv2.boundingRect(contour)
return ((origin[1] // tolerance_factor) * tolerance_factor) * cols + origin[0]
image = cv2.imread(file)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 30, 150)
cnts, h= cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts.sort(key=lambda x:get_precedence(x, edged.shape[1]))
The resulting order I get is still messed up.
The image I am working on is this