-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (44 loc) · 1.66 KB
/
app.py
File metadata and controls
59 lines (44 loc) · 1.66 KB
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 os
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import StreamingResponse
import cv2
import numpy as np
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from processors.ContourCornersDetector import ContourCornersDetector
import io
from starlette.responses import Response, FileResponse
contour_page_extractor = ContourCornersDetector()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
return FileResponse(os.path.join("static", "favicon.ico"))
@app.post("/process-image")
async def process_image(file: UploadFile = File(...)):
try:
contents = await file.read()
nparr = np.frombuffer(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
raise HTTPException(status_code=400, detail="Invalid image file")
processed_img = contour_page_extractor(img)
_, buffer = cv2.imencode('.jpg', processed_img)
bytes_io = io.BytesIO(buffer)
return StreamingResponse(
bytes_io,
media_type="image/jpeg",
headers={"Content-Disposition": f"attachment; filename=processed_{file.filename}"}
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)