File size: 5,343 Bytes
dfae564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from keras.models import load_model
from aiohttp import ClientSession
from numpy import expand_dims as np_expand_dims
from captcha_processor import CaptchaProcessor
from asyncio import get_running_loop
from asyncio import sleep as asyncsleep
from random import randint
import aiofiles

model = load_model("model.h5")
proxies = [
    #"http://q2adq9_proton_me:[email protected]:8000",
    "http://ocjjjsgs:[email protected]:6106"
]

async def get_binary_from_link(link: str) -> bytes:
    async with ClientSession() as session:
        for _ in range(20):
            try:
                a = await session.get(link, proxy=proxies[randint(0, len(proxies)-1)])
                if int(a.status) == 200:
                    print("Got binary")
                    return await a.read()
                else:
                    await asyncsleep(0.125)
            except Exception as e:
                print(e)
        return randint(100000, 999999)


async def predict(url: str, recursion: int = 0, fnfnfn: int = randint(1, 10000000)) -> dict:
    binary = await get_binary_from_link(url)
    if type(binary) == type(0):
        return {
                "WARNING": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
                "prediction": binary,
                "letters_predictions": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
                "full_prediction": binary,
                "recursion": recursion
            }
    
    async with aiofiles.open(f"/root/c-s-api/temp/{fnfnfn}.png", "wb") as outfile:
        print(f"Trying to do smth with {fnfnfn}")
        await outfile.write(binary)
    
    try:
        processor = CaptchaProcessor(binary)
    except Exception as e:
        if recursion > 10:
            return {
                "WARNING": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
                "prediction": binary,
                "letters_predictions": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
                "full_prediction": binary,
                "recursion": recursion
            }
        else:
            print(f"1, {recursion}, {str(e)}")
            return await predict(url, recursion + 1, fnfnfn)
    
    try:
        processor.replace_color(processor.get_background_color(), processor.WHITE_RGB)
        processor.replace_colors(processor.get_letters_color(), processor.WHITE_RGB)
    except Exception as e:
        if recursion > 10:
            return {
                "WARNING": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
                "prediction": binary,
                "letters_predictions": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
                "full_prediction": binary,
                "recursion": recursion
            }
        else:
            print(f"2, {recursion}, {str(e)}")
            return await predict(url, recursion + 1, fnfnfn)
    
    try:
        processor.convert_color_space(6)
    except Exception as e:
        if recursion > 10:
            return {
                "WARNING": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
                "prediction": binary,
                "letters_predictions": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
                "full_prediction": binary,
                "recursion": recursion
            }
        else:
            print(f"3, {recursion}, {str(e)}")
            return await predict(url, recursion + 1, fnfnfn)
    
    try:
        processor.threshold()
    except Exception as e:
        if recursion > 10:
            return {
                "WARNING": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
                "prediction": binary,
                "letters_predictions": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
                "full_prediction": binary,
                "recursion": recursion
            }
        else:
            print(f"4, {recursion}, {str(e)}")
            return await predict(url, recursion + 1, fnfnfn)
        
    #    processor = CaptchaProcessor(binary)
    #    processor.replace_color(processor.get_background_color(), processor.WHITE_RGB)
    #    processor.replace_colors(processor.get_letters_color(), processor.WHITE_RGB)
    #    processor.convert_color_space(6)
    #    processor.threshold()
    #except Exception as e:
    #    print(f"error with image, trying again {e}")
    #    return await predict(url, recursion + 1)
        
    try:
        processor.increase_letters_size(2)
    except IndexError:
        return await predict(url, recursion + 1, fnfnfn)
    letters = processor.slice_letters()
    if len(letters) != 6: return await predict(url, recursion + 1, fnfnfn)
    shorts = []
    final = ""
    letters_solving = [
        get_running_loop().run_in_executor(None, model.predict, np_expand_dims(letter, axis=0))
        for letter in letters
    ]
    letters_solving = [await result for result in letters_solving]
    fulls = [list(map(lambda x: float(x), letter[0])) for letter in letters_solving]
    for prediction in fulls: shorts.append(prediction.index(max(*prediction)))
    for short in shorts: final += str(short)
    return {
        "prediction": final,
        "letters_predictions": shorts,
        "full_prediction": fulls,
        "recursion": recursion
    }