移除列表中满足条件的列表
Remove list in lists that satisfied the condition
我正在尝试为特定用途制作一个快速的 OCR,我知道应该只为普通 OCR 编写一个预处理器,这样会更快,但这个想法首先出现在我的脑海中,我想我应该试试反正它哈哈。该程序会在屏幕区域拍照并识别其中的数字,截至目前,它只有 0 和 1,但我一直在研究它并遇到了一些问题。这是我的代码
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
这会识别一些预定的数字集,如this on screen and right now, it would give me a set of coordinate for number zero on screen, here is the result,请忽略其他部分,这只是我在玩。问题是 pyautogui.locateAllOnScreen
如果不正确设置置信度,有时会在大约 0-5 像素的坐标范围内生成相同图片的重复值。
示例:
值应该是 [ (1655,1024,20,26),(1675,1024,20,26) ]
但会产生第三个值,例如 [ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ]
.
这就是我尝试对此进行更正的原因。无论如何要确定第二个重复坐标的 x 值是否在第一个坐标的 0-5 像素范围内并删除它,将其余部分向上移动,以便数字正确有序地出现?谢谢!
注意:我还在努力自学列表删除过程,使用lambda 阅读删除列表对我来说就像是胡言乱语。如有不妥请见谅。祝大家有个美好的一天!
你可以试试这个。
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
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)
输出:
[(1655, 1024, 20, 26), (1675, 1024, 20, 26)]
我正在尝试为特定用途制作一个快速的 OCR,我知道应该只为普通 OCR 编写一个预处理器,这样会更快,但这个想法首先出现在我的脑海中,我想我应该试试反正它哈哈。该程序会在屏幕区域拍照并识别其中的数字,截至目前,它只有 0 和 1,但我一直在研究它并遇到了一些问题。这是我的代码
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
这会识别一些预定的数字集,如this on screen and right now, it would give me a set of coordinate for number zero on screen, here is the result,请忽略其他部分,这只是我在玩。问题是 pyautogui.locateAllOnScreen
如果不正确设置置信度,有时会在大约 0-5 像素的坐标范围内生成相同图片的重复值。
示例:
值应该是 [ (1655,1024,20,26),(1675,1024,20,26) ]
但会产生第三个值,例如 [ (1655,1024,20,26), (1658,1024,20,26), (1675,1024,20,26) ]
.
这就是我尝试对此进行更正的原因。无论如何要确定第二个重复坐标的 x 值是否在第一个坐标的 0-5 像素范围内并删除它,将其余部分向上移动,以便数字正确有序地出现?谢谢!
注意:我还在努力自学列表删除过程,使用lambda 阅读删除列表对我来说就像是胡言乱语。如有不妥请见谅。祝大家有个美好的一天!
你可以试试这个。
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
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)
输出:
[(1655, 1024, 20, 26), (1675, 1024, 20, 26)]