gpt4 book ai didi

python - 删除列表中满足条件的列表

转载 作者:行者123 更新时间:2023-12-02 01:36:51 34 4
gpt4 key购买 nike

我正在尝试为特定用途制作一个快速 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现在,它会给我屏幕上数字零的一组坐标,这里是 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

关于python - 删除列表中满足条件的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72245304/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com