我的 tqdm 进度条无法正常工作

时间:2021-02-13 13:05:51

标签: python tqdm

我编写了一个脚本来解析和下载 prnt.sc 中的图像,它运行良好。然后我想修改它,以便它使用 tqdm 进度条显示进度,但由于检查功能中的 for 循环,它无法正常工作。我重新设计了该函数,它可以完美地与常规文本一起使用,但它不适用于 tqdm - 它只是跳过了一些文件并且没有被检查。代码如下:

检查功能:

def start_checking(iteration):
global mails_found
session_time = time.strftime("%H_%M_%b%d", time.localtime()).lower()
mails_availible = []
mail_in_text = False
image = DIRPATH + f"image-{iteration}" + '.png'
img = cv2.imread(image)
config = r'--oem 3 --psm 6'

try:
    img_text = pytesseract.image_to_string(img, config=config).lower()
    print("\n" + image + " checked")
    if "gmail" in img_text or "yahoo" in img_text or "yandex" in img_text or "mail" in img_text or "icloud" in img_text or "password" in img_text or "giftcard" in img_text or "gift" in img_text or "coupon" in img_text or "gift card" in img_text:
        save(img, iteration)
    else:
        os.remove(image)
except:
    iteration += 1

显示该图像已通过短信进行检查(有效):

for i in range(1, iterations+1)[::-1]:
    start_checking(i)
    print(f"image-{iterations} checked")
    imgs += 1
    iterations -= 1

显示使用 tqdm 检查图像(不起作用):

pbar2 = tqdm([el for el in range(1, iterations+1)[::-1]])
for char2 in pbar2:
    start_checking(char2)
    pbar2.set_description(f"Checking image-{iterations}")
    imgs += 1
    iterations -= 1
    time.sleep(0.5)

如您所见,我在 for 循环中添加了延迟,因为我认为 tqdm 没有足够的时间查看图像是否被检查,但它有,它只是跳过图像。

Output of version one (using text)

Output of version two (using tqdm)

正如您可能看到的,下载功能完美运行 我还在检查功能中添加了一个打印,显示图像已检查(因为 tqdm 不留下任何日志)

那么我应该怎么做才能使 tqdm 按预期工作?

0 个答案:

没有答案
相关问题