forked from fb1h2s/captcha-cracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
36 lines (23 loc) · 948 Bytes
/
main.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
import io
import os
from fastapi import FastAPI, File, UploadFile
from PIL import Image
from captcha_cracker import CaptchaModel
app = FastAPI()
current_dir = os.path.dirname(os.path.abspath(__file__))
weights_path = os.path.join(current_dir, "models", "weights.h5")
captcha_model = CaptchaModel()
@app.post("/", summary="CAPTCHA IMAGE")
async def upload(file: UploadFile = File(...)):
read_image = Image.open(io.BytesIO(await file.read()))
if read_image.mode == "RGBA":
background = Image.new("RGB", read_image.size, (255, 255, 255))
background.paste(read_image, (0, 0), read_image)
image = background
else:
image = read_image.convert("RGB")
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format="PNG")
img_byte_arr.seek(0)
prediction = captcha_model.predict_from_bytes(img_byte_arr.getvalue())
return {"content_type": file.content_type, "prediction": prediction}