Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,42 +1,42 @@
|
|
1 |
-
from fastapi import FastAPI,UploadFile
|
2 |
-
from PIL import Image
|
3 |
-
|
4 |
-
import mediapipe as mp
|
5 |
-
import numpy as np
|
6 |
-
import pandas as pd
|
7 |
-
from io import BytesIO
|
8 |
-
import onnxruntime as ort
|
9 |
-
from Logs.detectnameanddistance import render
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
FacesEmbedding=pd.read_csv("./Models/FacesMeanEmbeddings.csv",index_col=0)
|
14 |
-
persons=list(FacesEmbedding.columns)
|
15 |
-
model_path="./Models/FaceModelV5.onnx"
|
16 |
-
EP_list = [ 'CPUExecutionProvider']
|
17 |
-
Session = ort.InferenceSession(model_path,providers=EP_list)
|
18 |
-
input_name = Session.get_inputs()[0].name
|
19 |
-
output_name=Session.get_outputs()[0].name
|
20 |
-
MediapipeModelPath=
|
21 |
-
BaseOptions=mp.tasks.BaseOptions
|
22 |
-
FaceLandMarker=mp.tasks.vision.FaceLandmarker
|
23 |
-
FaceLandMarkerOptions=mp.tasks.vision.FaceLandmarkerOptions
|
24 |
-
VisionRunningMode=mp.tasks.vision.RunningMode
|
25 |
-
FaceLandMarkerResult=mp.tasks.vision.FaceLandmarkerResult
|
26 |
-
options=FaceLandMarkerOptions(base_options=BaseOptions(model_asset_path=MediapipeModelPath),running_mode=VisionRunningMode.IMAGE)
|
27 |
-
landmarker= FaceLandMarker.create_from_options(options)
|
28 |
-
|
29 |
-
App=FastAPI()
|
30 |
-
|
31 |
-
@App.post("/upload")
|
32 |
-
async def detect(img:UploadFile):
|
33 |
-
|
34 |
-
image=np.array(Image.open(BytesIO(img.file.read())))
|
35 |
-
mp_img=mp.Image(image_format=mp.ImageFormat.SRGB,data=image)
|
36 |
-
result=landmarker.detect(mp_img)
|
37 |
-
|
38 |
-
if len(result.face_landmarks)==0:
|
39 |
-
return {"state":False,"message":"No Face Found","distance":0,"name":"null","x1":0,"x2":0,"y1":0,"y2":0}
|
40 |
-
x1,y1,x2,y2,name,distance=render(Session,input_name,output_name,FacesEmbedding,result,mp_img.numpy_view(),persons)
|
41 |
-
return {"state":True,"message":"null","distance":distance,"name":name,"x1":x1,"x2":x2,"y1":y1,"y2":y2}
|
42 |
-
|
|
|
1 |
+
from fastapi import FastAPI,UploadFile
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
import mediapipe as mp
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
from io import BytesIO
|
8 |
+
import onnxruntime as ort
|
9 |
+
from Logs.detectnameanddistance import render
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
FacesEmbedding=pd.read_csv("./Models/FacesMeanEmbeddings.csv",index_col=0)
|
14 |
+
persons=list(FacesEmbedding.columns)
|
15 |
+
model_path="./Models/FaceModelV5.onnx"
|
16 |
+
EP_list = [ 'CPUExecutionProvider']
|
17 |
+
Session = ort.InferenceSession(model_path,providers=EP_list)
|
18 |
+
input_name = Session.get_inputs()[0].name
|
19 |
+
output_name=Session.get_outputs()[0].name
|
20 |
+
MediapipeModelPath="./Models/face_landmarker.task"
|
21 |
+
BaseOptions=mp.tasks.BaseOptions
|
22 |
+
FaceLandMarker=mp.tasks.vision.FaceLandmarker
|
23 |
+
FaceLandMarkerOptions=mp.tasks.vision.FaceLandmarkerOptions
|
24 |
+
VisionRunningMode=mp.tasks.vision.RunningMode
|
25 |
+
FaceLandMarkerResult=mp.tasks.vision.FaceLandmarkerResult
|
26 |
+
options=FaceLandMarkerOptions(base_options=BaseOptions(model_asset_path=MediapipeModelPath),running_mode=VisionRunningMode.IMAGE)
|
27 |
+
landmarker= FaceLandMarker.create_from_options(options)
|
28 |
+
|
29 |
+
App=FastAPI()
|
30 |
+
|
31 |
+
@App.post("/upload")
|
32 |
+
async def detect(img:UploadFile):
|
33 |
+
|
34 |
+
image=np.array(Image.open(BytesIO(img.file.read())))
|
35 |
+
mp_img=mp.Image(image_format=mp.ImageFormat.SRGB,data=image)
|
36 |
+
result=landmarker.detect(mp_img)
|
37 |
+
|
38 |
+
if len(result.face_landmarks)==0:
|
39 |
+
return {"state":False,"message":"No Face Found","distance":0,"name":"null","x1":0,"x2":0,"y1":0,"y2":0}
|
40 |
+
x1,y1,x2,y2,name,distance=render(Session,input_name,output_name,FacesEmbedding,result,mp_img.numpy_view(),persons)
|
41 |
+
return {"state":True,"message":"null","distance":distance,"name":name,"x1":x1,"x2":x2,"y1":y1,"y2":y2}
|
42 |
+
|