-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_web.py
59 lines (41 loc) · 1.41 KB
/
main_web.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
import io
import uvicorn
from decouple import config
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from PIL import Image
from services import run_all_models_web
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST"], # , "PUT", "DELETE"],
allow_headers=["*"],
expose_headers=["X-Image-Description"],
)
# todo; this does not work all the times
api_port = int(config("API_PORT"))
def format_header_value(value):
value = value.encode("ascii", "ignore").decode()
value = value.replace("\r", "").replace("\n", "<br>").replace("\t", " ")
value = value.strip()
return value
@app.post("/process_image_web/")
async def process_image_web(file: UploadFile):
contents = await file.read()
pil_image = Image.open(io.BytesIO(contents))
croped, text = run_all_models_web(pil_image)
croped = Image.fromarray(croped)
text = format_header_value(text)
img_byte_array = io.BytesIO()
croped.save(img_byte_array, format="PNG")
img_byte_array = img_byte_array.getvalue()
return StreamingResponse(
io.BytesIO(img_byte_array),
media_type="image/png",
headers={"X-Image-Description": text},
)
if __name__ == "__main__":
uvicorn.run("main_web:app", host="0.0.0.0", port=api_port)