|
from flask import Flask, request, redirect, render_template, session, url_for |
|
from flask_wtf import CSRFProtect |
|
import requests |
|
import uuid |
|
import json |
|
import os |
|
|
|
app = Flask(__name__, static_folder='static') |
|
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' |
|
|
|
csrf = CSRFProtect(app) |
|
app.config['WTF_CSRF_CHECK_DEFAULT'] = False |
|
app.config['SESSION_COOKIE_SECURE'] = True |
|
app.config['SESSION_COOKIE_SAMESITE'] = "None" |
|
|
|
SESSION_DICT = {} |
|
|
|
def add_to_session(key, value): |
|
uid = uuid.uuid4() |
|
SESSION_DICT[uid] = value |
|
session[key] = uid |
|
print('ADD SESSION', key, uid) |
|
|
|
def get_session_data(key): |
|
print('GET FROM SESSION', session, SESSION_DICT) |
|
if key not in session or session[key] not in SESSION_DICT: |
|
return None |
|
return SESSION_DICT[session[key]] |
|
|
|
def send_rapidapi(url, payload): |
|
headers = { |
|
"content-type": "application/json", |
|
"X-RapidAPI-Key": os.environ.get("API_KEY") |
|
} |
|
response = requests.post(url, json=payload, headers=headers) |
|
return response.json() |
|
|
|
def id_recognition(id_payload): |
|
url = 'https://id-document-recognition2.p.rapidapi.com/api/iddoc_base64' |
|
resp = send_rapidapi(url, id_payload) |
|
return resp |
|
|
|
def face_liveness(face_payload): |
|
url = 'https://face-liveness-detection3.p.rapidapi.com/api/liveness_base64' |
|
resp = send_rapidapi(url, face_payload) |
|
return resp |
|
|
|
def face_recognition(face_payload): |
|
url = 'https://face-recognition26.p.rapidapi.com/api/face_compare_base64' |
|
resp = send_rapidapi(url, face_payload) |
|
return resp |
|
|
|
@app.route("/") |
|
def index(): |
|
return redirect(url_for('id_type')) |
|
|
|
@app.route("/id_type/", methods=['GET']) |
|
def id_type(): |
|
return render_template('id_type.html') |
|
|
|
@app.route("/id_type/", methods=['POST']) |
|
def id_type_done(): |
|
print("Request Come here", request.values['ocr_type']) |
|
add_to_session('id_type', request.values['ocr_type']) |
|
print("All OK") |
|
return redirect(url_for('id_file')) |
|
|
|
@app.route("/id_file/", methods=['GET']) |
|
def id_file(): |
|
id_type = get_session_data('id_type') |
|
if id_type is None: |
|
return redirect(url_for('id_type')) |
|
|
|
id_file = get_session_data('id_file') |
|
if id_type == 'ic': |
|
if id_file is not None and id_file['image2'] == "": |
|
return render_template('id_file.html', id_type = 'ic_back') |
|
else: |
|
return render_template('id_file.html', id_type = 'ic_front') |
|
else: |
|
return render_template('id_file.html', id_type = 'passport') |
|
|
|
@app.route("/id_file/", methods=['POST']) |
|
def id_file_done(): |
|
id_type = get_session_data('id_type') |
|
id_file = get_session_data('id_file') |
|
id_base64 = request.values['ocr_file'] |
|
|
|
if id_type == 'ic': |
|
if id_file is not None and id_file['image2'] == "": |
|
id_file['image2'] = id_base64 |
|
add_to_session('id_file', id_file) |
|
return redirect(url_for('id_detail')) |
|
else: |
|
add_to_session('id_file', { |
|
"image": id_base64, |
|
"image2": "" |
|
}) |
|
return redirect(url_for('id_file')) |
|
else: |
|
add_to_session('id_file', { |
|
"image": id_base64, |
|
"image2": "" |
|
}) |
|
return redirect(url_for('id_detail')) |
|
|
|
@app.route("/id_detail/", methods=['GET']) |
|
def id_detail(): |
|
sess_id_file = get_session_data('id_file') |
|
if sess_id_file is not None: |
|
res = id_recognition(sess_id_file) |
|
if 'data' in res and 'ocr' in res['data'] and 'name' in res['data']['ocr']: |
|
add_to_session('id_name', res['data']['ocr']['name']) |
|
|
|
if 'data' in res and 'image' in res['data']: |
|
if 'portrait' in res['data']['image']: |
|
add_to_session('id_face', res['data']['image']['portrait']) |
|
if 'documentFrontSide' in res['data']['image']: |
|
add_to_session('id_front', res['data']['image']['documentFrontSide']) |
|
|
|
add_to_session('id_liveness', res['authenticity_liveness']) |
|
add_to_session('id_forge', res['authenticity_meta']) |
|
|
|
return render_template('id_detail.html', ocr_result = json.dumps(res)) |
|
else: |
|
return redirect(url_for('id_type')) |
|
|
|
@app.route("/id_detail/", methods=['POST']) |
|
def id_detail_done(): |
|
return redirect(url_for('fr_file')) |
|
|
|
@app.route("/fr_file/", methods=['GET']) |
|
def fr_file(): |
|
return render_template('fr_file.html') |
|
|
|
@app.route("/fr_file/", methods=['POST']) |
|
def fr_file_done(): |
|
fr_base64 = request.values['fr_file'] |
|
add_to_session('fr_selfie', fr_base64) |
|
return redirect(url_for('fr_detail')) |
|
|
|
@app.route("/fr_detail/", methods=['GET']) |
|
def fr_detail(): |
|
sess_id_front = get_session_data('id_front') |
|
if sess_id_front is None: |
|
return redirect(url_for('id_type')) |
|
|
|
sess_fr_selfie = get_session_data('fr_selfie') |
|
if sess_fr_selfie is not None: |
|
liveness_api_resp = face_liveness({ |
|
"image": sess_fr_selfie |
|
}) |
|
|
|
if 'data' in liveness_api_resp and 'result' in liveness_api_resp['data']: |
|
liveness_res = liveness_api_resp['data']['result'] |
|
|
|
print("Liveness Result:", liveness_api_resp) |
|
sess_id_face = get_session_data('id_face') |
|
if sess_id_face is not None: |
|
payload = { |
|
"image1": sess_id_face, |
|
"image2": sess_fr_selfie |
|
} |
|
recog_api_resp = face_recognition(payload) |
|
|
|
if 'data' in recog_api_resp and 'result' in recog_api_resp['data']: |
|
recog_res = recog_api_resp['data']['result'] |
|
recog_similarity = recog_api_resp['data']['similarity'] |
|
|
|
print("Matching Result", recog_api_resp) |
|
return render_template('fr_detail.html', |
|
id_name = get_session_data('id_name'), |
|
id_liveness = get_session_data('id_liveness').upper(), |
|
id_forge = get_session_data('id_forge').upper(), |
|
id_front = 'data:image/png;base64,' + sess_id_front, |
|
id_face = 'data:image/png;base64,' + sess_id_face, |
|
selfie = 'data:image/png;base64,' + sess_fr_selfie, |
|
liveness_res = liveness_res.upper(), |
|
recog_res = recog_res.upper(), |
|
recog_similarity = recog_similarity * 100) |
|
else: |
|
return redirect(url_for('fr_file')) |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860) |