Пример #1
0
def test_recognize_http(show_img=False, num=1, reconstruct=False, force_partition=True):
    time_start = time.time()
    provider = BilibiliCaptchaProvider()
    recognizer = CaptchaRecognizer()
    fail = 0
    right_strong = 0
    right_weak = 0
    wrong_strong = 0
    wrong_weak = 0
    for i in range(num):
        image = time_func("fetch" if num == 1 else None, lambda: provider.fetch())
        if show_img and num == 1:
            show_image(image)
        if num == 1:
            success, seq, weak_confidence = recognizer.recognize(
                image, save_intermediate=True, verbose=True, reconstruct=reconstruct, force_partition=force_partition
            )
        else:
            if i == 0:
                success, seq, weak_confidence = recognizer.recognize(
                    image,
                    save_intermediate=False,
                    verbose=False,
                    reconstruct=reconstruct,
                    force_partition=force_partition,
                )
            else:
                success, seq, weak_confidence = recognizer.recognize(
                    image, save_intermediate=False, verbose=False, reconstruct=False, force_partition=force_partition
                )
        if success:
            print(seq)
            result = time_func("verify" if num == 1 else None, lambda: provider.verify(seq))
            if num == 1:
                print("Recognized seq is {}".format(result))
            if result:
                if weak_confidence:
                    right_weak += 1
                else:
                    right_strong += 1
            else:
                if weak_confidence:
                    wrong_weak += 1
                else:
                    wrong_strong += 1
        else:
            fail += 1
    right_total = right_strong + right_weak
    wrong_total = wrong_strong + wrong_weak
    print("Fail: ", fail)
    print("Right weak: ", right_weak)
    print("Right strong: ", right_strong)
    print("Right total: ", right_total)
    print("Wrong weak: ", wrong_weak)
    print("Wrong strong: ", wrong_strong)
    print("Wrong total: ", wrong_total)
    print("Total success rate: ", (right_weak + right_strong) / num)
    print("Success rate when confident: ", (right_strong + right_weak) / (num - fail) if num - fail > 0 else 0)
    print(
        "Success rate when strongly confident: ",
        right_strong / (right_strong + wrong_strong) if right_strong + wrong_strong > 0 else 0,
    )
    print(
        "Success rate when weakly confident: ",
        right_weak / (right_weak + wrong_weak) if right_weak + wrong_weak > 0 else 0,
    )
    time_end = time.time()
    print("Time used to test recognize http is: ", time_end - time_start)
Пример #2
0
def test_recognize_http(show_img=False,
                        num=1,
                        reconstruct=False,
                        force_partition=True):
    time_start = time.time()
    provider = BilibiliCaptchaProvider()
    recognizer = CaptchaRecognizer()
    fail = 0
    right_strong = 0
    right_weak = 0
    wrong_strong = 0
    wrong_weak = 0
    for i in range(num):
        image = time_func('fetch' if num == 1 else None,
                          lambda: provider.fetch())
        if show_img and num == 1:
            show_image(image)
        if num == 1:
            success, seq, weak_confidence = recognizer.recognize(
                image,
                save_intermediate=True,
                verbose=True,
                reconstruct=reconstruct,
                force_partition=force_partition)
        else:
            if i == 0:
                success, seq, weak_confidence = recognizer.recognize(
                    image,
                    save_intermediate=False,
                    verbose=False,
                    reconstruct=reconstruct,
                    force_partition=force_partition)
            else:
                success, seq, weak_confidence = recognizer.recognize(
                    image,
                    save_intermediate=False,
                    verbose=False,
                    reconstruct=False,
                    force_partition=force_partition)
        if success:
            print(seq)
            result = time_func('verify' if num == 1 else None,
                               lambda: provider.verify(seq))
            if num == 1:
                print('Recognized seq is {}'.format(result))
            if result:
                if weak_confidence:
                    right_weak += 1
                else:
                    right_strong += 1
            else:
                if weak_confidence:
                    wrong_weak += 1
                else:
                    wrong_strong += 1
        else:
            fail += 1
    right_total = right_strong + right_weak
    wrong_total = wrong_strong + wrong_weak
    print('Fail: ', fail)
    print('Right weak: ', right_weak)
    print('Right strong: ', right_strong)
    print('Right total: ', right_total)
    print('Wrong weak: ', wrong_weak)
    print('Wrong strong: ', wrong_strong)
    print('Wrong total: ', wrong_total)
    print('Total success rate: ', (right_weak + right_strong) / num)
    print('Success rate when confident: ',
          (right_strong + right_weak) / (num - fail) if num - fail > 0 else 0)
    print(
        'Success rate when strongly confident: ',
        right_strong / (right_strong + wrong_strong)
        if right_strong + wrong_strong > 0 else 0)
    print(
        'Success rate when weakly confident: ', right_weak /
        (right_weak + wrong_weak) if right_weak + wrong_weak > 0 else 0)
    time_end = time.time()
    print('Time used to test recognize http is: ', time_end - time_start)