forked from GDGVIT/vitty-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (38 loc) · 1.26 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Import necessary modules"""
import cv2
import numpy as np
import uvicorn
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from starlette.middleware.cors import CORSMiddleware
from tableDetection import fetch_data, fetch_text_timetable
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@app.get("/")
async def testing():
"""Check if server is working"""
return "Ok! Working!"
@app.post("/uploadfile/")
async def predict_api(file: UploadFile = File(...)):
"""Upload timetable"""
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png", "JPG", "PNG")
if not extension:
raise HTTPException(
status_code=400, detail="File must be an image, in jpg or png format!"
)
image = await file.read()
nparr = np.fromstring(image, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
data = fetch_data(img)
return data
@app.post("/uploadtext/")
async def get_timetable(request: str = Form(...)):
data = fetch_text_timetable(request)
return data
if __name__ == "__main__":
uvicorn.run(app, debug=True)