Mohammedallyl's picture
Upload 27 files
c4dc0b3 verified
raw
history blame
3.74 kB
from fastapi import APIRouter,UploadFile,Form,File
from PIL import Image
import mediapipe as mp
from numpy import sqrt, array,abs
from io import BytesIO
from os.path import exists
from passlib.context import CryptContext
from sqlite3 import connect
from datetime import datetime
MediapipeModelPath="./Models/face_landmarker.task"
BaseOptions=mp.tasks.BaseOptions
FaceLandMarker=mp.tasks.vision.FaceLandmarker
FaceLandMarkerOptions=mp.tasks.vision.FaceLandmarkerOptions
VisionRunningMode=mp.tasks.vision.RunningMode
FaceLandMarkerResult=mp.tasks.vision.FaceLandmarkerResult
options=FaceLandMarkerOptions(base_options=BaseOptions(model_asset_path=MediapipeModelPath),running_mode=VisionRunningMode.IMAGE)
landmarker= FaceLandMarker.create_from_options(options)
# class DataType(BaseModel):
# Images:UploadFile
# Email:EmailStr
# Password:str
# Name:str
# Type:str
# def asform(Images:UploadFile=File(...),Email:str=Form(...),Password:str=Form(...),Name:str=Form(...),Type:str=Form(...))->DataType:
# return DataType(Images=Images,Email=Email,Password=Password,Name=Name,Type=Type)
UploaderRouter=APIRouter(prefix="/Uploader")
@UploaderRouter.post("/ImageUpload")
async def SpeachToTextEndPoint(Images:UploadFile=File(...),Email:str=Form(...),Password:str=Form(...),Name:str=Form(...),Type:str=Form(...)):
try:
State=False
pwd_context=CryptContext(schemes=["bcrypt"],deprecated="auto")
con=connect("./DataBase/DataBase.bd")
cursor=con.execute(f'''
SELECT UserId,Password FROM Users where Email='{Email}'
''')
Data=cursor.fetchall()
if len(Data) !=0 :
if Data[0][0]==None:
return {"Status":True,"Message":"Email or Password Is Incorrect"}
HasedPassword=Data[0][1]
UserId=Data[0][0]
State=pwd_context.verify(Password,HasedPassword)
con.close()
if not State:
return {"Status":False,"Message":"Email or Password is not correct"}
image_array=array(Image.open(BytesIO(Images.file.read())))
mp_img=mp.Image(image_format=mp.ImageFormat.SRGB,data=image_array)
result=landmarker.detect(mp_img)
State,ExtractedImage=render(results=result,FaceImage=image_array)
if exists(f"./FaceRecognition/ExtactedFaces/{UserId}/{Type}/{Name}") and State:
Image.fromarray(ExtractedImage.astype("uint8")).save(f"./FaceRecognition/ExtactedFaces/{UserId}/{Type}/{Name}/{str(datetime.now()).split('.')[-1]}-{Images.filename}","PNG")
else:
return {"Status":False,"Message":"Cant Find That Person"}
return {"Satus":True}
except Exception as e:
print(e)
return {"Satus":False}
def render(results,FaceImage):
res = results.face_landmarks[0]
x_=int(res[145].x*FaceImage.shape[1])
y_=int(res[145].y*FaceImage.shape[0])
x2_=int(res[374].x*FaceImage.shape[1])
y2_=int(res[374].y*FaceImage.shape[0])
w=sqrt((x_-x2_)**2+(y_-y2_)**2)
W=6.3
f = 840
d = (W * f) / w
if d >=100:
return False,None
x=int(res[356].x*FaceImage.shape[1])
y=int(res[152].y*FaceImage.shape[0])
x2=int(res[162].x*FaceImage.shape[1])
y2=int(res[338].y*FaceImage.shape[0])
if x<FaceImage.shape[1]-10:
x+=10
if y>FaceImage.shape[0]-10:
y+=10
if x2>10:
x2-=10
if y2>10:
y2-=10
ExtractedFace=FaceImage[abs(y2):abs(y),abs(x2):abs(x)]
return True,ExtractedFace