Spaces:
Sleeping
Sleeping
File size: 4,275 Bytes
53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 8018195 b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af b4a4baf 53743af |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent))
from fastapi import FastAPI, Request, File, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.Hackathon_setup import face_recognition, exp_recognition
import numpy as np
from PIL import Image
app = FastAPI(
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
)
# Mount static folder
app.mount("/static", StaticFiles(directory="app/static"), name="static")
# Templates folder
templates = Jinja2Templates(directory="app/templates")
#################################### Home Page ####################################
@app.get("/")
async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
#################################### Face Similarity ####################################
@app.get("/similarity/")
async def similarity_root(request: Request):
return templates.TemplateResponse("similarity.html", {"request": request})
@app.post("/predict_similarity/")
async def predict_similarity(request: Request, file1: UploadFile = File(...), file2: UploadFile = File(...)):
# Save uploaded images
path1 = f'app/static/{file1.filename}'
path2 = f'app/static/{file2.filename}'
contents1 = await file1.read()
with open(path1, 'wb') as f:
f.write(contents1)
contents2 = await file2.read()
with open(path2, 'wb') as f:
f.write(contents2)
# Open images as RGB
img1 = np.array(Image.open(path1).convert("RGB"))
img2 = np.array(Image.open(path2).convert("RGB"))
# Compute similarity
result = face_recognition.get_similarity(img1, img2)
return templates.TemplateResponse("predict_similarity.html", {
"request": request,
"result": result, # <-- only this line changed
"simi_filename1": f"/static/{file1.filename}",
"simi_filename2": f"/static/{file2.filename}"
})
#################################### Face Recognition ####################################
@app.get("/face_recognition/")
async def face_recognition_root(request: Request):
return templates.TemplateResponse("face_recognition.html", {"request": request})
@app.post("/predict_face_recognition/")
async def predict_face_recognition(request: Request, file3: UploadFile = File(...)):
path = f'app/static/{file3.filename}'
contents = await file3.read()
with open(path, 'wb') as f:
f.write(contents)
img = np.array(Image.open(path).convert("RGB"))
result = face_recognition.get_face_class(img)
return templates.TemplateResponse("predict_face_recognition.html", {
"request": request,
"result": result,
"face_rec_filename": f"/static/{file3.filename}"
})
#################################### Expression Recognition ####################################
@app.get("/expr_recognition/")
async def expr_recognition_root(request: Request):
return templates.TemplateResponse("expr_recognition.html", {"request": request})
@app.post("/predict_expr_recognition/")
async def predict_expr_recognition(request: Request, file4: UploadFile = File(...)):
path = f'app/static/{file4.filename}'
contents = await file4.read()
with open(path, 'wb') as f:
f.write(contents)
img = np.array(Image.open(path).convert("RGB"))
result = exp_recognition.get_expression(img)
return templates.TemplateResponse("predict_expr_recognition.html", {
"request": request,
"result": result,
"expr_rec_filename": f"/static/{file4.filename}"
})
#################################### CORS Middleware ####################################
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
#################################### Run App ####################################
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001)
|