Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
136 lines (103 loc) · 3.39 KB
/
Copy pathapp.py
File metadata and controls
136 lines (103 loc) · 3.39 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""Face Liveness HTTP API — Flask + sdk.py."""
from __future__ import annotations
import json
import os
from pathlib import Path
from flask import Flask, Response, request
from werkzeug.exceptions import HTTPException
import http_body
import license_ux
import sdk
ROOT = Path(__file__).resolve().parent
app = Flask(__name__)
def envelope(success: bool, code: int, message: str, data=None, status: int = 200):
return Response(
json.dumps(
{
"success": success,
"code": code,
"message": message,
"request_id": None,
"data": data,
}
),
status=status,
mimetype="application/json",
)
def sdk_json(result):
raw = result if isinstance(result, (bytes, bytearray)) else str(result).encode()
return Response(raw, mimetype="application/json")
@app.after_request
def cors(resp):
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
resp.headers["Access-Control-Allow-Headers"] = "Content-Type"
return resp
@app.route("/api/<path:_unused>", methods=["OPTIONS"])
def options(_unused):
return Response(status=204)
@app.get("/api/health")
def health():
return envelope(True, 0, "OK", {"status": "ok"})
@app.get("/api/machinecode")
def machinecode():
return envelope(True, 0, "OK", {"machinecode": sdk.get_machine_code()})
@app.get("/api/licenseStatus")
def license_status():
return envelope(True, 0, "OK", sdk.get_license_status())
@app.get("/api/backend")
def backend():
return envelope(
True,
0,
"OK",
{"product": "FaceLiveness", "sdk_version": "1.0.0", "backend": "cpu"},
)
@app.post("/api/activate")
def activate():
raw = http_body.activate_license_bytes()
if not raw:
return envelope(False, -1, "Empty license", None)
path = license_ux.save_license(ROOT, raw)
res = sdk.activate(str(path))
mc = sdk.get_machine_code()
if res != 0:
return envelope(
False, -1, "Invalid license", {"activated": False, "machinecode": mc}
)
sdk.init_sdk()
return envelope(
True, 0, "Successfully activated", {"activated": True, "machinecode": mc}
)
@app.post("/api/liveness")
@app.post("/api/check_liveness")
def liveness():
data = http_body.parse_body()
image = data.get("image")
if not image:
return envelope(False, -1, "image required", None)
return sdk_json(sdk.liveness(image))
@app.errorhandler(HTTPException)
def http_error(ex: HTTPException):
if ex.code == 404:
return envelope(False, -10, "Not found", None, 404)
return envelope(False, -19, ex.description or str(ex), None, ex.code or 500)
@app.errorhandler(Exception)
def fail(ex: Exception):
if isinstance(ex, HTTPException):
return http_error(ex)
return envelope(False, -19, str(ex), None, 500)
def main() -> None:
license_ux.bootstrap(
ROOT,
get_machine_code=sdk.get_machine_code,
activate=sdk.activate,
init_sdk=sdk.init_sdk,
)
port = int(os.environ.get("PORT", os.environ.get("FACESDK_PORT", "8084")))
host = os.environ.get("FACESDK_BIND_HOST", "0.0.0.0")
print("API listening on http://{0}:{1}".format(host, port))
app.run(host=host, port=port, threaded=True)
if __name__ == "__main__":
main()