-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
52 lines (40 loc) · 1.45 KB
/
server.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
import io
import uvicorn
import numpy as np
from argparse import ArgumentParser
from fastapi import FastAPI, Request
from facial_recogniser import FacialRecogniser
DEFAULT_REFERENCE_FACE_PATH = 'references'
SIMILARITY_THRESHOLD = 0.6
app = FastAPI()
recogniser = FacialRecogniser(DEFAULT_REFERENCE_FACE_PATH, threshold=SIMILARITY_THRESHOLD)
@app.get('/')
def index():
return {
'name': 'Facial recognition System',
'encoder_type': recogniser.face_encoder.encoder_type
}
@app.get('/config')
def get_configuration():
return {
'encoder_type': recogniser.face_encoder.encoder_type,
'similarity_threshold': recogniser.threshold,
'reference_image_directory': recogniser.face_dir
}
@app.post('/recognise')
async def run_facial_recognition(req: Request):
data = await req.body()
img = np.load(io.BytesIO(data), allow_pickle=True)
result = recogniser.recognise_faces(img, return_face_img=False)
if any([r['recognised'] is True for r in result]):
print(result)
return {'result': result}
def build_parser():
parser = ArgumentParser()
parser.add_argument('-i', '--ip', type=str, default='127.0.0.1', dest='ip', help='Server ip address')
parser.add_argument('-p', '--port', type=int, default=8000, dest='port', help='Server port')
return parser
if __name__ == '__main__':
arg_parser = build_parser()
args = arg_parser.parse_args()
uvicorn.run(app, host=args.ip, port=args.port)