CoolFace
Apppublic

JacobLinCool/captcha-recognizer

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
solve.py56 linesDownload Raw Back to src
1import pytesseract2import numpy as np3 4 5def solve(image: np.ndarray) -> str:6    for mode in [7, 10, 11, 12, 13]:7        result = normalize(8            pytesseract.image_to_string(9                image, lang="eng", config=f"--oem 3 --psm {mode}", timeout=0.510            ).strip()11        )12        if result != "":13            return result14 15    return "not sure"16 17 18def normalize(s: str) -> str:19    print(s)20    if "\n" in s:21        return ""22 23    s = s.replace(" ", "").lower()24 25    if len(s) < 3:26        return ""27 28    # if first is number29    if s[0].isdigit() and s[2].isdigit():30        if s[1] in ["+", "4"]:31            return str(int(s[0]) + int(s[2]))32        elif s[1] in ["-", "_"]:33            return str(int(s[0]) - int(s[2]))34        else:35            return str(int(s[0]) * int(s[2]))36 37    # possible alphabet mapping38    mapping = {39        ")": "l",40        "¥": "y",41        "2": "z",42        "é": "e",43    }44 45    for k, v in mapping.items():46        s = s.replace(k, v)47 48    # if not all alphabet49    if not all([c.isalpha() for c in s]):50        return ""51 52    if len(s) != 4:53        return ""54 55    return s56