Spaces:
Runtime error
Runtime error
Commit
·
dfae564
1
Parent(s):
61b4717
Upload 5 files
Browse files- captcha_processor.py +109 -0
- main.py +36 -0
- model.h5 +3 -0
- temp/.nomedia +0 -0
- utils.py +137 -0
captcha_processor.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from numpy import asarray as np_as_array
|
| 3 |
+
from numpy import all as np_all
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CaptchaProcessor:
|
| 7 |
+
|
| 8 |
+
WHITE_RGB = (255, 255, 255)
|
| 9 |
+
|
| 10 |
+
def __init__(self, data: bytes):
|
| 11 |
+
self.img = cv2.imdecode(
|
| 12 |
+
np_as_array(bytearray(data), dtype="uint8"),
|
| 13 |
+
cv2.IMREAD_ANYCOLOR
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def threshold(self):
|
| 17 |
+
self.img = cv2.threshold(self.img, 0, 255, cv2.THRESH_OTSU)[1]
|
| 18 |
+
|
| 19 |
+
def convert_color_space(self, target_space: int):
|
| 20 |
+
self.img = cv2.cvtColor(self.img, target_space)
|
| 21 |
+
|
| 22 |
+
def get_background_color(self) -> tuple:
|
| 23 |
+
return tuple(self.img[0, 0])
|
| 24 |
+
|
| 25 |
+
def resize(self, x: int, y: int):
|
| 26 |
+
self.img = cv2.resize(self.img, (x, y))
|
| 27 |
+
|
| 28 |
+
def save(self, name: str):
|
| 29 |
+
cv2.imwrite(name, self.img)
|
| 30 |
+
|
| 31 |
+
def get_letters_color(self) -> tuple:
|
| 32 |
+
colors = []
|
| 33 |
+
for y in range(self.img.shape[1]):
|
| 34 |
+
for x in range(self.img.shape[0]):
|
| 35 |
+
color = tuple(self.img[x, y])
|
| 36 |
+
if color != self.WHITE_RGB: colors.append(color)
|
| 37 |
+
return max(set(colors), key=colors.count)
|
| 38 |
+
|
| 39 |
+
def replace_color(self, target: tuple, to: tuple):
|
| 40 |
+
self.img[np_all(self.img == target, axis=-1)] = to
|
| 41 |
+
|
| 42 |
+
def replace_colors(self, exception: tuple, to: tuple):
|
| 43 |
+
self.img[np_all(self.img != exception, axis=-1)] = to
|
| 44 |
+
|
| 45 |
+
def increase_contrast(self, alpha: float, beta: float):
|
| 46 |
+
self.img = cv2.convertScaleAbs(self.img, alpha=alpha, beta=beta)
|
| 47 |
+
|
| 48 |
+
def increase_letters_size(self, add_pixels: int):
|
| 49 |
+
pixels = []
|
| 50 |
+
for y in range(self.img.shape[1]):
|
| 51 |
+
for x in range(self.img.shape[0]):
|
| 52 |
+
if self.img[x, y] == 0: pixels.append((x, y))
|
| 53 |
+
for y, x in pixels:
|
| 54 |
+
for i in range(1, add_pixels + 1):
|
| 55 |
+
self.img[y + i, x] = 0
|
| 56 |
+
self.img[y - i, x] = 0
|
| 57 |
+
self.img[y, x + i] = 0
|
| 58 |
+
self.img[y, x - i] = 0
|
| 59 |
+
self.img[y + i, x] = 0
|
| 60 |
+
self.img[y - i, x] = 0
|
| 61 |
+
self.img[y, x + i] = 0
|
| 62 |
+
self.img[y, x - i] = 0
|
| 63 |
+
|
| 64 |
+
# Отдаление символов друг от друга
|
| 65 |
+
# Может многократно повысить точность, но я так и не придумал правильную реализацию
|
| 66 |
+
def distance_letters(self, cf: float):
|
| 67 |
+
pixels = []
|
| 68 |
+
for y in range(self.img.shape[1]):
|
| 69 |
+
for x in range(self.img.shape[0]):
|
| 70 |
+
if self.img[x, y] == 0: pixels.append((x, y))
|
| 71 |
+
for y, x in pixels:
|
| 72 |
+
self.img[y, x] = 255
|
| 73 |
+
center = self.img.shape[1] / 2
|
| 74 |
+
z = self.img.shape[1] / x
|
| 75 |
+
if z >= 2: self.img[y, x - int((900 // x) * cf)] = 0
|
| 76 |
+
else: self.img[y, x + int((900 // x) * cf)] = 0
|
| 77 |
+
|
| 78 |
+
def slice_letters(self):
|
| 79 |
+
contours, hierarchy = cv2.findContours(self.img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
|
| 80 |
+
letter_image_regions = []
|
| 81 |
+
letters = []
|
| 82 |
+
for idx, contour in enumerate(contours):
|
| 83 |
+
if hierarchy[0][idx][3] != 0: continue
|
| 84 |
+
(x, y, w, h) = cv2.boundingRect(contour)
|
| 85 |
+
if w / h > 1.5:
|
| 86 |
+
half_width = int(w / 2)
|
| 87 |
+
letter_image_regions.append((idx, x, y, half_width, h))
|
| 88 |
+
letter_image_regions.append((idx, x + half_width, y, half_width, h))
|
| 89 |
+
else:
|
| 90 |
+
letter_image_regions.append((idx, x, y, w, h))
|
| 91 |
+
letter_image_regions = sorted(letter_image_regions, key=lambda z: z[1])
|
| 92 |
+
for _, x, y, w, h in letter_image_regions:
|
| 93 |
+
frame = self.img[y:y + h, x:x + w]
|
| 94 |
+
if frame.shape[1] > 35: continue
|
| 95 |
+
frame = cv2.resize(frame, (20, 40))
|
| 96 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 97 |
+
letters.append(frame)
|
| 98 |
+
return letters
|
| 99 |
+
|
| 100 |
+
def show(self):
|
| 101 |
+
cv2.imshow("Captcha Processor", self.img)
|
| 102 |
+
cv2.waitKey(0)
|
| 103 |
+
|
| 104 |
+
@classmethod
|
| 105 |
+
def from_file_name(cls, name: str):
|
| 106 |
+
file = open(name, "rb")
|
| 107 |
+
processor = cls(file.read())
|
| 108 |
+
file.close()
|
| 109 |
+
return processor
|
main.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import main things
|
| 2 |
+
from fastapi import Depends, FastAPI, Body
|
| 3 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 4 |
+
from uvicorn import run
|
| 5 |
+
from utils import predict
|
| 6 |
+
|
| 7 |
+
from fastapi_limiter import FastAPILimiter
|
| 8 |
+
from fastapi_limiter.depends import RateLimiter
|
| 9 |
+
|
| 10 |
+
import redis.asyncio as aioredis
|
| 11 |
+
|
| 12 |
+
# initing things
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
@app.on_event("startup")
|
| 16 |
+
async def startup():
|
| 17 |
+
redis = aioredis.from_url("redis://localhost", encoding="utf-8", decode_responses=True)
|
| 18 |
+
await FastAPILimiter.init(redis)
|
| 19 |
+
|
| 20 |
+
@app.get("/", dependencies=[Depends(RateLimiter(times=5, minutes=1))])
|
| 21 |
+
@app.post("/", dependencies=[Depends(RateLimiter(times=5, minutes=1))])
|
| 22 |
+
async def root():
|
| 23 |
+
return JSONResponse({"detail":"Not Found"}, 404)
|
| 24 |
+
|
| 25 |
+
@app.get("/amino-captcha-ocr/api/v1/autoregister/version")
|
| 26 |
+
async def v(): return {"v": 4, "l": ""}
|
| 27 |
+
|
| 28 |
+
@app.get("/amino-captcha-ocr/api/v1/predict", dependencies=[Depends(RateLimiter(times=5, minutes=1))])
|
| 29 |
+
async def resolveGet():
|
| 30 |
+
return JSONResponse({"detail":"Use POST instead GET"}, 400)
|
| 31 |
+
|
| 32 |
+
@app.post("/amino-captcha-ocr/api/v1/predict", dependencies=[Depends(RateLimiter(times=5, minutes=1))])
|
| 33 |
+
async def resolvePost(data = Body()):
|
| 34 |
+
return await predict(data["url"])
|
| 35 |
+
|
| 36 |
+
run(app, host="0.0.0.0", port=80)
|
model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:792c015158ffcfaadbb2a65fef9623af7fa1d243e3e1f915444f86c40049ea13
|
| 3 |
+
size 3730536
|
temp/.nomedia
ADDED
|
File without changes
|
utils.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.models import load_model
|
| 2 |
+
from aiohttp import ClientSession
|
| 3 |
+
from numpy import expand_dims as np_expand_dims
|
| 4 |
+
from captcha_processor import CaptchaProcessor
|
| 5 |
+
from asyncio import get_running_loop
|
| 6 |
+
from asyncio import sleep as asyncsleep
|
| 7 |
+
from random import randint
|
| 8 |
+
import aiofiles
|
| 9 |
+
|
| 10 |
+
model = load_model("model.h5")
|
| 11 |
+
proxies = [
|
| 12 |
+
#"http://q2adq9_proton_me:qwerty123123@la.residential.rayobyte.com:8000",
|
| 13 |
+
"http://ocjjjsgs:igbepiym05rl@95.164.235.50:6106"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
async def get_binary_from_link(link: str) -> bytes:
|
| 17 |
+
async with ClientSession() as session:
|
| 18 |
+
for _ in range(20):
|
| 19 |
+
try:
|
| 20 |
+
a = await session.get(link, proxy=proxies[randint(0, len(proxies)-1)])
|
| 21 |
+
if int(a.status) == 200:
|
| 22 |
+
print("Got binary")
|
| 23 |
+
return await a.read()
|
| 24 |
+
else:
|
| 25 |
+
await asyncsleep(0.125)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(e)
|
| 28 |
+
return randint(100000, 999999)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
async def predict(url: str, recursion: int = 0, fnfnfn: int = randint(1, 10000000)) -> dict:
|
| 32 |
+
binary = await get_binary_from_link(url)
|
| 33 |
+
if type(binary) == type(0):
|
| 34 |
+
return {
|
| 35 |
+
"WARNING": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
|
| 36 |
+
"prediction": binary,
|
| 37 |
+
"letters_predictions": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
|
| 38 |
+
"full_prediction": binary,
|
| 39 |
+
"recursion": recursion
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
async with aiofiles.open(f"/root/c-s-api/temp/{fnfnfn}.png", "wb") as outfile:
|
| 43 |
+
print(f"Trying to do smth with {fnfnfn}")
|
| 44 |
+
await outfile.write(binary)
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
processor = CaptchaProcessor(binary)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
if recursion > 10:
|
| 50 |
+
return {
|
| 51 |
+
"WARNING": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
|
| 52 |
+
"prediction": binary,
|
| 53 |
+
"letters_predictions": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
|
| 54 |
+
"full_prediction": binary,
|
| 55 |
+
"recursion": recursion
|
| 56 |
+
}
|
| 57 |
+
else:
|
| 58 |
+
print(f"1, {recursion}, {str(e)}")
|
| 59 |
+
return await predict(url, recursion + 1, fnfnfn)
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
processor.replace_color(processor.get_background_color(), processor.WHITE_RGB)
|
| 63 |
+
processor.replace_colors(processor.get_letters_color(), processor.WHITE_RGB)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
if recursion > 10:
|
| 66 |
+
return {
|
| 67 |
+
"WARNING": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
|
| 68 |
+
"prediction": binary,
|
| 69 |
+
"letters_predictions": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
|
| 70 |
+
"full_prediction": binary,
|
| 71 |
+
"recursion": recursion
|
| 72 |
+
}
|
| 73 |
+
else:
|
| 74 |
+
print(f"2, {recursion}, {str(e)}")
|
| 75 |
+
return await predict(url, recursion + 1, fnfnfn)
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
processor.convert_color_space(6)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
if recursion > 10:
|
| 81 |
+
return {
|
| 82 |
+
"WARNING": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
|
| 83 |
+
"prediction": binary,
|
| 84 |
+
"letters_predictions": "SOMETHING WENT WRONG. CONTACT OWNER IMMEDIATLY.",
|
| 85 |
+
"full_prediction": binary,
|
| 86 |
+
"recursion": recursion
|
| 87 |
+
}
|
| 88 |
+
else:
|
| 89 |
+
print(f"3, {recursion}, {str(e)}")
|
| 90 |
+
return await predict(url, recursion + 1, fnfnfn)
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
processor.threshold()
|
| 94 |
+
except Exception as e:
|
| 95 |
+
if recursion > 10:
|
| 96 |
+
return {
|
| 97 |
+
"WARNING": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
|
| 98 |
+
"prediction": binary,
|
| 99 |
+
"letters_predictions": "PROXY RETURNING INVALID IMAGE. CONTACT OWNER IMMEDIATLY.",
|
| 100 |
+
"full_prediction": binary,
|
| 101 |
+
"recursion": recursion
|
| 102 |
+
}
|
| 103 |
+
else:
|
| 104 |
+
print(f"4, {recursion}, {str(e)}")
|
| 105 |
+
return await predict(url, recursion + 1, fnfnfn)
|
| 106 |
+
|
| 107 |
+
# processor = CaptchaProcessor(binary)
|
| 108 |
+
# processor.replace_color(processor.get_background_color(), processor.WHITE_RGB)
|
| 109 |
+
# processor.replace_colors(processor.get_letters_color(), processor.WHITE_RGB)
|
| 110 |
+
# processor.convert_color_space(6)
|
| 111 |
+
# processor.threshold()
|
| 112 |
+
#except Exception as e:
|
| 113 |
+
# print(f"error with image, trying again {e}")
|
| 114 |
+
# return await predict(url, recursion + 1)
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
processor.increase_letters_size(2)
|
| 118 |
+
except IndexError:
|
| 119 |
+
return await predict(url, recursion + 1, fnfnfn)
|
| 120 |
+
letters = processor.slice_letters()
|
| 121 |
+
if len(letters) != 6: return await predict(url, recursion + 1, fnfnfn)
|
| 122 |
+
shorts = []
|
| 123 |
+
final = ""
|
| 124 |
+
letters_solving = [
|
| 125 |
+
get_running_loop().run_in_executor(None, model.predict, np_expand_dims(letter, axis=0))
|
| 126 |
+
for letter in letters
|
| 127 |
+
]
|
| 128 |
+
letters_solving = [await result for result in letters_solving]
|
| 129 |
+
fulls = [list(map(lambda x: float(x), letter[0])) for letter in letters_solving]
|
| 130 |
+
for prediction in fulls: shorts.append(prediction.index(max(*prediction)))
|
| 131 |
+
for short in shorts: final += str(short)
|
| 132 |
+
return {
|
| 133 |
+
"prediction": final,
|
| 134 |
+
"letters_predictions": shorts,
|
| 135 |
+
"full_prediction": fulls,
|
| 136 |
+
"recursion": recursion
|
| 137 |
+
}
|