-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastapp.py
67 lines (52 loc) · 1.81 KB
/
fastapp.py
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
from os import listdir
import cv2
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import numpy
import uvicorn
from captcha_solver import CaptchaSolver, Preprocessor
app = FastAPI()
solver = CaptchaSolver()
preprocessor = Preprocessor()
error_count = len(listdir("captcha_error/")) + 1
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
async def solve_captcha(image, bgr=False):
global error_count
if bgr:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
prediction = solver.predict_captcha(image)
result = 0
if prediction == "star":
result = solver.solve_star(gray)
else:
result = solver.solve_number(gray)
if result > 8 or result < 0:
result = 0
if result == 0:
cv2.imwrite("captcha_error/" + str(error_count).zfill(3) + ".png", image)
error_count += 1
return {"predict": prediction, "data": str(result)}
@app.post("/solver/argonclick")
async def solve_argonclick(file: UploadFile = File(...)):
npimg = numpy.frombuffer(await file.read(), numpy.uint8)
image = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = preprocessor.argonclick(image)
if isinstance(image, numpy.ndarray):
return await solve_captcha(image)
return {"predict": "New Background Color", "data": str(0)}
@app.post("/solve")
async def solve_general(file: UploadFile = File(...)):
npimg = numpy.frombuffer(await file.read(), numpy.uint8)
image = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
return await solve_captcha(image, True)
if __name__ == "__main__":
uvicorn.run("fastapp:app", port=5000)