Spaces:
Sleeping
Sleeping
Update flask/app.py
Browse files- flask/app.py +60 -38
flask/app.py
CHANGED
|
@@ -6,8 +6,12 @@ import base64
|
|
| 6 |
import json
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
|
|
|
| 9 |
from flask import Flask, request, jsonify
|
| 10 |
from time import gmtime, strftime
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
from engine.header import *
|
| 13 |
|
|
@@ -17,9 +21,7 @@ root_path = os.path.dirname(dir_path)
|
|
| 17 |
|
| 18 |
MATCH_THRESHOLD = 0.67
|
| 19 |
|
| 20 |
-
app =
|
| 21 |
-
app.config['SITE'] = "http://0.0.0.0:8000/"
|
| 22 |
-
app.config['DEBUG'] = False
|
| 23 |
|
| 24 |
version = get_version().decode('utf-8')
|
| 25 |
print_info('\t <Recognito Face Recognition> \t version {}'.format(version))
|
|
@@ -36,7 +38,6 @@ def activate_sdk():
|
|
| 36 |
if online_key is None:
|
| 37 |
print_warning("Recognition online license key not found!")
|
| 38 |
else:
|
| 39 |
-
print_info(f"FR_LICENSE_KEY: {online_key}")
|
| 40 |
ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8'))
|
| 41 |
|
| 42 |
if ret == 0:
|
|
@@ -86,48 +87,58 @@ def generate_response(result, similarity=None, face_bboxes=None, face_features=N
|
|
| 86 |
data["data"]["image1"] = images[0]
|
| 87 |
data["data"]["image2"] = images[1]
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
@app.route('/api/compare_face', methods=['POST'])
|
| 95 |
-
def compare_face_api():
|
| 96 |
-
try:
|
| 97 |
-
file1 = request.files['image1']
|
| 98 |
-
image_mat1 = cv2.imdecode(np.frombuffer(file1.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 99 |
-
except:
|
| 100 |
-
response = generate_response("Failed to open image1")
|
| 101 |
-
return response
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
try:
|
| 104 |
-
|
| 105 |
-
image_mat2 =
|
| 106 |
-
except:
|
| 107 |
-
response = generate_response("Failed to open image2")
|
| 108 |
-
return response
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, MATCH_THRESHOLD)
|
| 111 |
response = generate_response(result, score, face_bboxes, face_features)
|
| 112 |
return response
|
| 113 |
|
| 114 |
-
|
| 115 |
-
@app.route('/api/compare_face_base64', methods=['POST'])
|
| 116 |
-
def coompare_face_base64_api():
|
| 117 |
-
content = request.get_json()
|
| 118 |
-
|
| 119 |
try:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
try:
|
| 127 |
-
|
| 128 |
-
image_mat2 =
|
| 129 |
except:
|
| 130 |
-
response = generate_response("Failed to open
|
| 131 |
return response
|
| 132 |
|
| 133 |
result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, MATCH_THRESHOLD)
|
|
@@ -138,5 +149,16 @@ if __name__ == '__main__':
|
|
| 138 |
ret = activate_sdk()
|
| 139 |
if ret != 0:
|
| 140 |
exit(-1)
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import json
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
+
import gradio as gr
|
| 10 |
from flask import Flask, request, jsonify
|
| 11 |
from time import gmtime, strftime
|
| 12 |
+
from fastapi import FastAPI, File, UploadFile
|
| 13 |
+
from fastapi.responses import JSONResponse
|
| 14 |
+
from typing import Dict
|
| 15 |
|
| 16 |
from engine.header import *
|
| 17 |
|
|
|
|
| 21 |
|
| 22 |
MATCH_THRESHOLD = 0.67
|
| 23 |
|
| 24 |
+
app = FastAPI()
|
|
|
|
|
|
|
| 25 |
|
| 26 |
version = get_version().decode('utf-8')
|
| 27 |
print_info('\t <Recognito Face Recognition> \t version {}'.format(version))
|
|
|
|
| 38 |
if online_key is None:
|
| 39 |
print_warning("Recognition online license key not found!")
|
| 40 |
else:
|
|
|
|
| 41 |
ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8'))
|
| 42 |
|
| 43 |
if ret == 0:
|
|
|
|
| 87 |
data["data"]["image1"] = images[0]
|
| 88 |
data["data"]["image2"] = images[1]
|
| 89 |
|
| 90 |
+
return JSONResponse(content=data, status_code=200)
|
| 91 |
+
|
| 92 |
+
@app.get("/")
|
| 93 |
+
def read_root():
|
| 94 |
+
return {"status": "API is running"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
def read_image(file: UploadFile) -> np.ndarray:
|
| 97 |
+
# Read the image file and convert it to OpenCV format
|
| 98 |
+
image_bytes = file.file.read()
|
| 99 |
+
image_np = np.frombuffer(image_bytes, np.uint8)
|
| 100 |
+
image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
|
| 101 |
+
return image
|
| 102 |
+
|
| 103 |
+
@app.post("/api/compare_face")
|
| 104 |
+
async def compare_face_api(
|
| 105 |
+
image1: UploadFile = File(...),
|
| 106 |
+
image2: UploadFile = File(...)
|
| 107 |
+
) -> JSONResponse:
|
| 108 |
try:
|
| 109 |
+
image_mat1 = read_image(image1)
|
| 110 |
+
image_mat2 = read_image(image2)
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
except Exception as e:
|
| 113 |
+
response = generate_response("Failed to open image")
|
| 114 |
+
return response
|
| 115 |
+
|
| 116 |
result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, MATCH_THRESHOLD)
|
| 117 |
response = generate_response(result, score, face_bboxes, face_features)
|
| 118 |
return response
|
| 119 |
|
| 120 |
+
def decode_base64_image(base64_string: str) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
try:
|
| 122 |
+
image_data = base64.b64decode(base64_string)
|
| 123 |
+
image_np = np.frombuffer(image_data, np.uint8)
|
| 124 |
+
image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
|
| 125 |
+
if image is None:
|
| 126 |
+
raise ValueError("Decoded image is None")
|
| 127 |
+
return image
|
| 128 |
+
except Exception as e:
|
| 129 |
+
raise ValueError(f"Failed to decode base64 image: {str(e)}")
|
| 130 |
+
|
| 131 |
+
@app.post("/api/compare_face_base64")
|
| 132 |
+
async def compare_face_base64_api(
|
| 133 |
+
image1: str,
|
| 134 |
+
image2: str
|
| 135 |
+
) -> JSONResponse:
|
| 136 |
+
|
| 137 |
try:
|
| 138 |
+
image_mat1 = decode_base64_image(image1)
|
| 139 |
+
image_mat2 = decode_base64_image(image2)
|
| 140 |
except:
|
| 141 |
+
response = generate_response("Failed to open image")
|
| 142 |
return response
|
| 143 |
|
| 144 |
result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, MATCH_THRESHOLD)
|
|
|
|
| 149 |
ret = activate_sdk()
|
| 150 |
if ret != 0:
|
| 151 |
exit(-1)
|
| 152 |
+
|
| 153 |
+
dummy_interface = gr.Interface(
|
| 154 |
+
fn=lambda x: "API ready.",
|
| 155 |
+
inputs=gr.Textbox(label="Info"),
|
| 156 |
+
outputs=gr.Textbox(label="Response"),
|
| 157 |
+
allow_flagging="never" # 🚫 disables writing to `flagged/`
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
gr_app = gr.mount_gradio_app(app, dummy_interface, path="/gradio")
|
| 161 |
+
|
| 162 |
+
import uvicorn
|
| 163 |
+
uvicorn.run(gr_app, host="0.0.0.0", port=8000)
|
| 164 |
+
|