Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, APIRouter | |
| from fastapi.staticfiles import StaticFiles | |
| from starlette.responses import FileResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import base64 | |
| from pydantic import BaseModel | |
| import time | |
| from facenet_pytorch import InceptionResnetV1, MTCNN | |
| import warnings | |
| import os | |
| import face_compare | |
| warnings.filterwarnings('ignore', category=FutureWarning, module='facenet_pytorch') | |
| mtcnn = MTCNN(keep_all=False, device='cpu') | |
| model = InceptionResnetV1(pretrained='vggface2').eval() | |
| app = FastAPI() | |
| router = APIRouter() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| pdf = 0 | |
| class ImageData(BaseModel): | |
| image: str | |
| class ImagesData(BaseModel): | |
| idCard: str | |
| profileImage: str | |
| async def index() -> FileResponse: | |
| return FileResponse(path="front/dist/index.html", media_type="text/html") | |
| async def verif() -> FileResponse: | |
| return FileResponse(path="front/dist/index.html", media_type="text/html") | |
| async def upload_pdf(data: ImageData): | |
| header, encoded = data.image.split(',', 1) | |
| binary_data = base64.b64decode(encoded) | |
| # Save the pdf | |
| pdf = binary_data | |
| return {"message": "Image reçue et sauvegardée"} | |
| async def upload_ids(data: ImagesData): | |
| header, encoded1 = data.idCard.split(',', 1) | |
| binary_data1 = base64.b64decode(encoded1) | |
| header, encoded2 = data.profileImage.split(',', 1) | |
| binary_data2 = base64.b64decode(encoded2) | |
| with open("id_card_image.png", "wb") as id_card_file: | |
| id_card_file.write(binary_data1) # Save as PNG (or use correct extension based on header) | |
| with open("profile_image.png", "wb") as profile_file: | |
| profile_file.write(binary_data2) # Save as PNG (or use correct extension based on header) | |
| id_card_abs_path = os.path.abspath("id_card_image.png") | |
| profile_image_abs_path = os.path.abspath("profile_image.png") | |
| output = face_compare.compare_faces(id_card_abs_path, profile_image_abs_path) | |
| if output > 0.6: | |
| return {"message": "Valid"} | |
| else: | |
| return {"message": "Not Valid"} | |
| app.include_router(router) | |
| app.mount("/", StaticFiles(directory="front/dist", html=True), name="static") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |