aminocaptchasolver / utils.py
imperialwool's picture
Update utils.py
d585c90
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
import os
model = load_model("model.h5")
proxies = [
str(os.environ.get("HTTP_PROXY"))
]
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"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)
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
}