-2

I'm making a project about handwriting recognition so i have picture i have to recognize the text in it so what i did is that I made all the characters in the picture an objects (contour) ,the problem is that after printing the characters its printed unsorted(or by the x or by the y), and what i need is to sort a list full object from the top left to the bottom right.

I have tried to sorted by the y coordinates and then by x coordinates it will be sorted as an x coordinates from the beginning.

a.sort(key = operator.attrgetter("intRectx"))     
a.sort(key = operator.attrgetter("intRectY"))

I expect the project will print the characters as they written.

class Data():
 num1 = None           
 num2 = None        
 intRectX = 0                # x
 intRectY = 0                # y
 num3 = 0            
 num4 = 0        
 num5 = 0.0             

a = []                                                       # we will fill these shortly
for Data in all:                                    # for all contours
    if Data.checkIfContourIsValid():              # check if valid
        a.append(Data)                            # if so, append to valid contour list
a.sort(key = lambda x: (x.intRectY, x.intRectx))
Dean Amir
  • 11
  • 2

1 Answers1

2

Sort with a lambda expression that returns a tuple:

a.sort(key = lambda x: (x.intRectx, x.intRectY))

Edit:

If you want to sort by y then the x swap the order:

a.sort(key = lambda x: (x.intRectY, x.intRectx))
Primusa
  • 13,136
  • 3
  • 33
  • 53