-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (57 loc) · 2.03 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
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
import io
import pickle
import uuid
import cv2
import matplotlib.pyplot as plt
import numpy as np
import uvicorn
from bson.binary import Binary
from fastapi import Depends, FastAPI, File, Form, Request, UploadFile
from fastapi.responses import FileResponse
from fastapi.templating import Jinja2Templates
from keras.models import load_model
from keras_facenet import FaceNet
from mtcnn.mtcnn import MTCNN
from PIL import Image as PImage
from pydantic import BaseModel
from pymongo import MongoClient
import faiss_utils
import utils
app = FastAPI()
embedder = FaceNet()
client = MongoClient()
mongodb = client.face_recognition
templates = Jinja2Templates(directory="templates")
class Input(BaseModel):
base64str: str
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
@app.get("/")
def home(request: Request):
return templates.TemplateResponse("dashboard.html", {"request": request})
@app.post('/registerFace')
async def register_face(name: str, refImage: UploadFile = File(...)):
face = utils.extract_face(await refImage.read())
mongodb.embeddings.insert_one({"faceName": name, "embedding": Binary(
pickle.dumps(utils.get_embedding(embedder.model, face), protocol=2), subtype=128)})
return {"Success": "Face is registered successfully"}
# ,mainImage: UploadFile= File(...),
@app.post('/recognizeFace')
async def recognize_Face(mainImage: UploadFile = File(...)):
# Extract Face
face = utils.extract_face(await mainImage.read())
# Generate Embedding
embedding = utils.get_embedding(embedder.model, face)
# get orginal embedding
distance, identityIndex = faiss_utils.searchEmbedding(
np.expand_dims(embedding, axis=0))
if(distance < 0.8):
prediction = mongodb.embeddings.find()[identityIndex]["faceName"]
else:
prediction = "Unknown"
print(prediction)
print(distance)
return {
"Prediction": str(prediction),
"L2Distance": float(distance)
}