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olve().parent.parent))
|
| 4 |
+
#print(sys.path)
|
| 5 |
+
from typing import Any
|
| 6 |
+
|
| 7 |
+
from fastapi import FastAPI, Request, APIRouter, File, UploadFile
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
from fastapi.templating import Jinja2Templates
|
| 10 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
+
from app.config import settings
|
| 12 |
+
from app import __version__
|
| 13 |
+
from app.Hackathon_setup import face_recognition, exp_recognition
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
from PIL import Image
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
app = FastAPI(
|
| 20 |
+
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# To store files uploaded by users
|
| 24 |
+
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
| 25 |
+
|
| 26 |
+
# To access Templates directory
|
| 27 |
+
templates = Jinja2Templates(directory="app/templates")
|
| 28 |
+
|
| 29 |
+
simi_filename1 = None
|
| 30 |
+
simi_filename2 = None
|
| 31 |
+
face_rec_filename = None
|
| 32 |
+
expr_rec_filename = None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
#################################### Home Page endpoints #################################################
|
| 36 |
+
@app.get("/")
|
| 37 |
+
async def root(request: Request):
|
| 38 |
+
return templates.TemplateResponse("index.html", {'request': request,})
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
#################################### Face Similarity endpoints #################################################
|
| 42 |
+
@app.get("/similarity/")
|
| 43 |
+
async def similarity_root(request: Request):
|
| 44 |
+
return templates.TemplateResponse("similarity.html", {'request': request,})
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@app.post("/predict_similarity/")
|
| 48 |
+
async def create_upload_files(request: Request, file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
| 49 |
+
global simi_filename1
|
| 50 |
+
global simi_filename2
|
| 51 |
+
|
| 52 |
+
if 'image' in file1.content_type:
|
| 53 |
+
contents = await file1.read()
|
| 54 |
+
simi_filename1 = 'app/static/' + file1.filename
|
| 55 |
+
with open(simi_filename1, 'wb') as f:
|
| 56 |
+
f.write(contents)
|
| 57 |
+
|
| 58 |
+
if 'image' in file2.content_type:
|
| 59 |
+
contents = await file2.read()
|
| 60 |
+
simi_filename2 = 'app/static/' + file2.filename
|
| 61 |
+
with open(simi_filename2, 'wb') as f:
|
| 62 |
+
f.write(contents)
|
| 63 |
+
|
| 64 |
+
img1 = Image.open(simi_filename1)
|
| 65 |
+
img1 = np.array(img1).reshape(img1.size[1], img1.size[0], 3).astype(np.uint8)
|
| 66 |
+
|
| 67 |
+
img2 = Image.open(simi_filename2)
|
| 68 |
+
img2 = np.array(img2).reshape(img2.size[1], img2.size[0], 3).astype(np.uint8)
|
| 69 |
+
|
| 70 |
+
result = face_recognition.get_similarity(img1, img2)
|
| 71 |
+
#print(result)
|
| 72 |
+
|
| 73 |
+
return templates.TemplateResponse("predict_similarity.html", {"request": request,
|
| 74 |
+
"result": np.round(result, 3),
|
| 75 |
+
"simi_filename1": '../static/'+file1.filename,
|
| 76 |
+
"simi_filename2": '../static/'+file2.filename,})
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
#################################### Face Recognition endpoints #################################################
|
| 80 |
+
@app.get("/face_recognition/")
|
| 81 |
+
async def face_recognition_root(request: Request):
|
| 82 |
+
return templates.TemplateResponse("face_recognition.html", {'request': request,})
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
@app.post("/predict_face_recognition/")
|
| 86 |
+
async def create_upload_files(request: Request, file3: UploadFile = File(...)):
|
| 87 |
+
global face_rec_filename
|
| 88 |
+
|
| 89 |
+
if 'image' in file3.content_type:
|
| 90 |
+
contents = await file3.read()
|
| 91 |
+
face_rec_filename = 'app/static/' + file3.filename
|
| 92 |
+
with open(face_rec_filename, 'wb') as f:
|
| 93 |
+
f.write(contents)
|
| 94 |
+
|
| 95 |
+
img1 = Image.open(face_rec_filename)
|
| 96 |
+
img1 = np.array(img1).reshape(img1.size[1], img1.size[0], 3).astype(np.uint8)
|
| 97 |
+
|
| 98 |
+
result = face_recognition.get_face_class(img1)
|
| 99 |
+
print(result)
|
| 100 |
+
|
| 101 |
+
return templates.TemplateResponse("predict_face_recognition.html", {"request": request,
|
| 102 |
+
"result": result,
|
| 103 |
+
"face_rec_filename": '../static/'+file3.filename,})
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
#################################### Expresion Recognition endpoints #################################################
|
| 107 |
+
@app.get("/expr_recognition/")
|
| 108 |
+
async def expr_recognition_root(request: Request):
|
| 109 |
+
return templates.TemplateResponse("expr_recognition.html", {'request': request,})
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@app.post("/predict_expr_recognition/")
|
| 113 |
+
async def create_upload_files(request: Request, file4: UploadFile = File(...)):
|
| 114 |
+
global expr_rec_filename
|
| 115 |
+
|
| 116 |
+
if 'image' in file4.content_type:
|
| 117 |
+
contents = await file4.read()
|
| 118 |
+
expr_rec_filename = 'app/static/' + file4.filename
|
| 119 |
+
with open(expr_rec_filename, 'wb') as f:
|
| 120 |
+
f.write(contents)
|
| 121 |
+
|
| 122 |
+
img1 = Image.open(expr_rec_filename)
|
| 123 |
+
img1 = np.array(img1).reshape(img1.size[1], img1.size[0], 3).astype(np.uint8)
|
| 124 |
+
|
| 125 |
+
result = exp_recognition.get_expression(img1)
|
| 126 |
+
print(result)
|
| 127 |
+
|
| 128 |
+
return templates.TemplateResponse("predict_expr_recognition.html", {"request": request,
|
| 129 |
+
"result": result,
|
| 130 |
+
"expr_rec_filename": '../static/'+file4.filename,})
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# Set all CORS enabled origins
|
| 135 |
+
if settings.BACKEND_CORS_ORIGINS:
|
| 136 |
+
app.add_middleware(
|
| 137 |
+
CORSMiddleware,
|
| 138 |
+
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
| 139 |
+
allow_credentials=True,
|
| 140 |
+
allow_methods=["*"],
|
| 141 |
+
allow_headers=["*"],
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# Start app
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
import uvicorn
|
| 148 |
+
uvicorn.run(app, host="0.0.0.0", port=8001)
|