Update flask/app.py
Browse files- flask/app.py +52 -25
flask/app.py
CHANGED
|
@@ -7,15 +7,12 @@ import json
|
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
from time import gmtime, strftime
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
from engine.header import
|
| 13 |
-
from engine.header import get_deviceid
|
| 14 |
-
from engine.header import init_sdk
|
| 15 |
-
from engine.header import init_sdk_offline
|
| 16 |
-
|
| 17 |
-
from engine.header import check_liveness
|
| 18 |
-
from engine.header import print_log, print_error, print_info, print_warning
|
| 19 |
|
| 20 |
file_path = os.path.abspath(__file__)
|
| 21 |
dir_path = os.path.dirname(file_path)
|
|
@@ -42,7 +39,6 @@ def activate_sdk():
|
|
| 42 |
if online_key is None:
|
| 43 |
print_warning("Liveness online license key not found!")
|
| 44 |
else:
|
| 45 |
-
print_info(f"FL_LICENSE_KEY: {online_key}")
|
| 46 |
ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8'))
|
| 47 |
|
| 48 |
if ret == 0:
|
|
@@ -89,16 +85,25 @@ def generate_response(result, face_rect, score, angles):
|
|
| 89 |
"pitch": angles[2]
|
| 90 |
}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
try:
|
| 100 |
-
|
| 101 |
-
image_mat = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 102 |
except:
|
| 103 |
response = generate_response("Failed to open file!", None, None, None)
|
| 104 |
return response
|
|
@@ -107,12 +112,24 @@ def check_liveness_api():
|
|
| 107 |
response = generate_response(result, face_rect, score, angles)
|
| 108 |
return response
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
try:
|
| 113 |
-
|
| 114 |
-
imageBase64 = content['image']
|
| 115 |
-
image_mat = cv2.imdecode(np.frombuffer(base64.b64decode(imageBase64), dtype=np.uint8), cv2.IMREAD_COLOR)
|
| 116 |
except:
|
| 117 |
response = generate_response("Failed to open file!", None, None, None)
|
| 118 |
return response
|
|
@@ -125,5 +142,15 @@ if __name__ == '__main__':
|
|
| 125 |
ret = activate_sdk()
|
| 126 |
if ret != 0:
|
| 127 |
exit(-1)
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
from time import gmtime, strftime
|
| 10 |
+
from pydantic import BaseModel
|
| 11 |
+
from fastapi import FastAPI, File, UploadFile
|
| 12 |
+
from fastapi.responses import JSONResponse
|
| 13 |
+
from typing import Dict
|
| 14 |
|
| 15 |
+
from engine.header import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
file_path = os.path.abspath(__file__)
|
| 18 |
dir_path = os.path.dirname(file_path)
|
|
|
|
| 39 |
if online_key is None:
|
| 40 |
print_warning("Liveness online license key not found!")
|
| 41 |
else:
|
|
|
|
| 42 |
ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8'))
|
| 43 |
|
| 44 |
if ret == 0:
|
|
|
|
| 85 |
"pitch": angles[2]
|
| 86 |
}
|
| 87 |
|
| 88 |
+
return JSONResponse(content=data, status_code=200)
|
| 89 |
+
|
| 90 |
+
@app.get("/")
|
| 91 |
+
def read_root():
|
| 92 |
+
return {"status": "API is running"}
|
| 93 |
+
|
| 94 |
+
def read_image(file: UploadFile) -> np.ndarray:
|
| 95 |
+
# Read the image file and convert it to OpenCV format
|
| 96 |
+
image_bytes = file.file.read()
|
| 97 |
+
image_np = np.frombuffer(image_bytes, np.uint8)
|
| 98 |
+
image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
|
| 99 |
+
return image
|
| 100 |
+
|
| 101 |
+
@app.post("/api/check_liveness")
|
| 102 |
+
async def check_liveness_api(
|
| 103 |
+
image: UploadFile = File(...)
|
| 104 |
+
) -> JSONResponse:
|
| 105 |
try:
|
| 106 |
+
image_mat = read_image(image)
|
|
|
|
| 107 |
except:
|
| 108 |
response = generate_response("Failed to open file!", None, None, None)
|
| 109 |
return response
|
|
|
|
| 112 |
response = generate_response(result, face_rect, score, angles)
|
| 113 |
return response
|
| 114 |
|
| 115 |
+
def decode_base64_image(base64_string: str) -> np.ndarray:
|
| 116 |
+
try:
|
| 117 |
+
image_data = base64.b64decode(base64_string)
|
| 118 |
+
image_np = np.frombuffer(image_data, np.uint8)
|
| 119 |
+
image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
|
| 120 |
+
if image is None:
|
| 121 |
+
raise ValueError("Decoded image is None")
|
| 122 |
+
return image
|
| 123 |
+
except Exception as e:
|
| 124 |
+
raise ValueError(f"Failed to decode base64 image: {str(e)}")
|
| 125 |
+
|
| 126 |
+
class CheckLivenessRequest(BaseModel):
|
| 127 |
+
image: str
|
| 128 |
+
|
| 129 |
+
@app.post("/api/check_liveness_base64")
|
| 130 |
+
async def check_liveness_base64_api(request: CheckLivenessRequest) -> JSONResponse:
|
| 131 |
try:
|
| 132 |
+
image_mat = decode_base64_image(request.image)
|
|
|
|
|
|
|
| 133 |
except:
|
| 134 |
response = generate_response("Failed to open file!", None, None, None)
|
| 135 |
return response
|
|
|
|
| 142 |
ret = activate_sdk()
|
| 143 |
if ret != 0:
|
| 144 |
exit(-1)
|
| 145 |
+
|
| 146 |
+
dummy_interface = gr.Interface(
|
| 147 |
+
fn=lambda x: "API ready.",
|
| 148 |
+
inputs=gr.Textbox(label="Info"),
|
| 149 |
+
outputs=gr.Textbox(label="Response"),
|
| 150 |
+
allow_flagging="never" # 🚫 disables writing to `flagged/`
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
gr_app = gr.mount_gradio_app(app, dummy_interface, path="/gradio")
|
| 154 |
+
|
| 155 |
+
import uvicorn
|
| 156 |
+
uvicorn.run(gr_app, host="0.0.0.0", port=7860)
|