Home > Software design >  Remove list in lists that satisfied the condition
Remove list in lists that satisfied the condition

Time:05-15

I'm trying to make a quick OCR for specific use, I know should've just write a preprocessor for normal OCR and that would been faster but this idea came up to me first and I figure I should just try it anyway haha. This program would take a picture on a region of screen and identify the number within it, as of right now, it's only 0 and 1 but I've been working on it and stuck with some problems. Here is my code

while True:
    if keyboard.is_pressed('`'):
        Firstlist =  list(pyautogui.locateAllOnScreen(image[0], confidence = 0.95,region=( 1570 , 990 , 230 , 70 )))
        print(len(Firstlist))
        Firstlist1 = list(pyautogui.locateAllOnScreen(image1, confidence = 0.95,region=( 1570 , 990 , 230 , 70 )))   Firstlist
        print(len(Firstlist1))
        print(Firstlist) 
        if len(Firstlist) > 0:
            print(Firstlist[0][0])
            #compare all first instance of that number and eliminate all duplicated with in a different of 5 x pixel
        break

Which would identify some predetermined set of number like this on screen and right now, it would give me a set of coordinate for number zero on screen, here is the result, please ignore other parts, it's just me playing around. Problem with this is pyautogui.locateAllOnScreen would sometimes generate duplicate value of the same picture within the coordinate ranging from approx 0-5 pixels if not set the confidence level right.

Example:

Value supposed to be [ (1655,1024,20,26),(1675,1024,20,26) ] but will yield a third value like [ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ].

And that's why I'm trying to make a correction for this. Is there anyway to identified if that x value of second duplicate coordinate is within a range of 0-5 pixels to the first coordinate and just delete it, moving the rest up the ladder so that the number would come up right and in order? Thank you!

Note: I'm still working on learning the list removal process by myself, and read the removing list with lambda to me is like gibberish. Please forgive me if something is wrong. Have a good day y'all!

CodePudding user response:

You can try this.

if len(Firstlist) > 2:
    elems = [f[0] for f in Firstlist]  # create a list of just first index
    i = 0
    while i < len(elems) - 1:  # iterate through the list with i
        j = i   1
        while j < len(elems):  # iterate through the rest of the list with j
            if abs(elems[i] - elems[j]) <= 5:  # if item at index i is within 5 pixels of item at index j
                del elems[j]   # delete item j and continue
            else:
                j  = 1    # otherwise move to next item
        i  = 1  #  Move to next i item

CodePudding user response:

list1 = [ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ]
x = [list1[0]]   [x for x in list1 if abs(list1[0][0] - x[0]) > 5]
print(x)

Output:

[(1655, 1024, 20, 26), (1675, 1024, 20, 26)]
  • Related