Spaces:
Running
Running
Commit
·
00855e0
1
Parent(s):
6e6d84b
Upload 15 files
Browse files- Dockerfile +17 -0
- app.py +132 -0
- demo.py +119 -0
- idcard_examples/1.jpg +0 -0
- idcard_examples/2.jpg +0 -0
- idcard_examples/3.jpg +0 -0
- idsdk.py +23 -0
- libidsdk.so +0 -0
- libimutils.so +0 -0
- libimutils.so_for_ubuntu22 +0 -0
- requirements.txt +4 -0
- run.sh +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.8-slim
|
| 2 |
+
RUN apt-get update -y
|
| 3 |
+
RUN apt-get install -y libpcsclite-dev psmisc libcurl4-openssl-dev libssl-dev
|
| 4 |
+
RUN mkdir -p /root/kby-ai-idcard
|
| 5 |
+
WORKDIR /root/kby-ai-idcard
|
| 6 |
+
COPY ./libidsdk.so .
|
| 7 |
+
COPY ./libimutils.so_for_ubuntu22 /usr/lib/libimutils.so
|
| 8 |
+
COPY ./idsdk.py .
|
| 9 |
+
COPY ./app.py .
|
| 10 |
+
COPY ./demo.py .
|
| 11 |
+
COPY ./requirements.txt .
|
| 12 |
+
COPY ./run.sh .
|
| 13 |
+
COPY ./idcard_examples ./idcard_examples
|
| 14 |
+
COPY ./data ./data
|
| 15 |
+
RUN pip3 install -r requirements.txt
|
| 16 |
+
CMD ["./run.sh"]
|
| 17 |
+
EXPOSE 8080 9000
|
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
sys.path.append('.')
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import base64
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
from flask import Flask, request, jsonify
|
| 9 |
+
from idsdk import getMachineCode
|
| 10 |
+
from idsdk import setActivation
|
| 11 |
+
from idsdk import initSDK
|
| 12 |
+
from idsdk import idcardRecognition
|
| 13 |
+
|
| 14 |
+
licensePath = "license.txt"
|
| 15 |
+
license = ""
|
| 16 |
+
|
| 17 |
+
# Get a specific environment variable by name
|
| 18 |
+
license = os.environ.get("LICENSE")
|
| 19 |
+
|
| 20 |
+
# Check if the variable exists
|
| 21 |
+
if license is not None:
|
| 22 |
+
print("Value of LICENSE:", license)
|
| 23 |
+
else:
|
| 24 |
+
license = ""
|
| 25 |
+
try:
|
| 26 |
+
with open(licensePath, 'r') as file:
|
| 27 |
+
license = file.read().strip()
|
| 28 |
+
except IOError as exc:
|
| 29 |
+
print("failed to open license.txt: ", exc.errno)
|
| 30 |
+
print("license: ", license)
|
| 31 |
+
|
| 32 |
+
machineCode = getMachineCode()
|
| 33 |
+
print("machineCode: ", machineCode.decode('utf-8'))
|
| 34 |
+
|
| 35 |
+
ret = setActivation(license.encode('utf-8'))
|
| 36 |
+
print("activation: ", ret)
|
| 37 |
+
|
| 38 |
+
ret = initSDK()
|
| 39 |
+
print("init: ", ret)
|
| 40 |
+
|
| 41 |
+
app = Flask(__name__)
|
| 42 |
+
|
| 43 |
+
@app.route('/idcard_recognition', methods=['POST'])
|
| 44 |
+
def idcard_recognition():
|
| 45 |
+
try:
|
| 46 |
+
file = request.files['file']
|
| 47 |
+
|
| 48 |
+
base64_image = base64.b64encode(file.read()).decode('utf-8')
|
| 49 |
+
ret = idcardRecognition(base64_image.encode('utf-8'), "".encode('utf-8'))
|
| 50 |
+
|
| 51 |
+
if ret != None:
|
| 52 |
+
j = json.loads(ret)
|
| 53 |
+
j.update({"Status": "Ok"})
|
| 54 |
+
response = jsonify(j)
|
| 55 |
+
else:
|
| 56 |
+
response = jsonify({"Status": "Error"})
|
| 57 |
+
except:
|
| 58 |
+
response = jsonify({"Status": "Error"})
|
| 59 |
+
|
| 60 |
+
response.status_code = 200
|
| 61 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
| 62 |
+
return response
|
| 63 |
+
|
| 64 |
+
@app.route('/idcard_recognition_multi_page', methods=['POST'])
|
| 65 |
+
def idcard_recognition_multi_page():
|
| 66 |
+
try:
|
| 67 |
+
file1 = request.files['file1']
|
| 68 |
+
file2 = request.files['file2']
|
| 69 |
+
|
| 70 |
+
base64_image1 = base64.b64encode(file1.read()).decode('utf-8')
|
| 71 |
+
base64_image2 = base64.b64encode(file2.read()).decode('utf-8')
|
| 72 |
+
ret = idcardRecognition(base64_image1.encode('utf-8'), base64_image2.encode('utf-8'))
|
| 73 |
+
|
| 74 |
+
if ret != None:
|
| 75 |
+
j = json.loads(ret)
|
| 76 |
+
j.update({"Status": "Ok"})
|
| 77 |
+
response = jsonify(j)
|
| 78 |
+
else:
|
| 79 |
+
response = jsonify({"Status": "Error"})
|
| 80 |
+
except:
|
| 81 |
+
response = jsonify({"Status": "Error"})
|
| 82 |
+
|
| 83 |
+
response.status_code = 200
|
| 84 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
| 85 |
+
return response
|
| 86 |
+
|
| 87 |
+
@app.route('/idcard_recognition_base64', methods=['POST'])
|
| 88 |
+
def idcard_recognition_base64():
|
| 89 |
+
try:
|
| 90 |
+
content = request.get_json()
|
| 91 |
+
base64_image = content['base64']
|
| 92 |
+
|
| 93 |
+
ret = idcardRecognition(base64_image.encode('utf-8'), "".encode('utf-8'))
|
| 94 |
+
|
| 95 |
+
if ret != None:
|
| 96 |
+
j = json.loads(ret)
|
| 97 |
+
j.update({"Status": "Ok"})
|
| 98 |
+
response = jsonify(j)
|
| 99 |
+
else:
|
| 100 |
+
response = jsonify({"Status": "Error"})
|
| 101 |
+
except:
|
| 102 |
+
response = jsonify({"Status": "Error"})
|
| 103 |
+
|
| 104 |
+
response.status_code = 200
|
| 105 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
| 106 |
+
return response
|
| 107 |
+
|
| 108 |
+
@app.route('/idcard_recognition_base64_multi_page', methods=['POST'])
|
| 109 |
+
def idcard_recognition_base64_multi_page():
|
| 110 |
+
try:
|
| 111 |
+
content = request.get_json()
|
| 112 |
+
base64_image1 = content['base64_1']
|
| 113 |
+
base64_image2 = content['base64_2']
|
| 114 |
+
|
| 115 |
+
ret = idcardRecognition(base64_image1.encode('utf-8'), base64_image2.encode('utf-8'))
|
| 116 |
+
|
| 117 |
+
if ret != None:
|
| 118 |
+
j = json.loads(ret)
|
| 119 |
+
j.update({"Status": "Ok"})
|
| 120 |
+
response = jsonify(j)
|
| 121 |
+
else:
|
| 122 |
+
response = jsonify({"Status": "Error"})
|
| 123 |
+
except:
|
| 124 |
+
response = jsonify({"Status": "Error"})
|
| 125 |
+
|
| 126 |
+
response.status_code = 200
|
| 127 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
| 128 |
+
return response
|
| 129 |
+
|
| 130 |
+
if __name__ == '__main__':
|
| 131 |
+
port = int(os.environ.get("PORT", 8080))
|
| 132 |
+
app.run(host='0.0.0.0', port=port)
|
demo.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import datadog_api_client
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def idcard_recognition(frame):
|
| 8 |
+
url = "http://127.0.0.1:8080/idcard_recognition"
|
| 9 |
+
files = {'file': open(frame, 'rb')}
|
| 10 |
+
|
| 11 |
+
r = requests.post(url=url, files=files)
|
| 12 |
+
|
| 13 |
+
html = None
|
| 14 |
+
images = None
|
| 15 |
+
mrz = None
|
| 16 |
+
|
| 17 |
+
status = r.json().get('Status')
|
| 18 |
+
table_value = ""
|
| 19 |
+
|
| 20 |
+
if r.json().get('MRZ') is not None:
|
| 21 |
+
mrz = r.json().get('MRZ')
|
| 22 |
+
|
| 23 |
+
for key, value in r.json().items():
|
| 24 |
+
if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
|
| 25 |
+
continue
|
| 26 |
+
|
| 27 |
+
mrz_value = ''
|
| 28 |
+
if mrz is not None and mrz.get(key) is not None:
|
| 29 |
+
mrz_value = mrz[key]
|
| 30 |
+
del mrz[key]
|
| 31 |
+
|
| 32 |
+
row_value = ("<tr>"
|
| 33 |
+
"<td>{key}</td>"
|
| 34 |
+
"<td>{value}</td>"
|
| 35 |
+
"<td>{mrz_value}</td>"
|
| 36 |
+
"</tr>".format(key=key, value=value, mrz_value=mrz_value))
|
| 37 |
+
table_value = table_value + row_value
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if mrz is not None:
|
| 41 |
+
for key, value in mrz.items():
|
| 42 |
+
if key == 'MRZ':
|
| 43 |
+
value = value.replace('<', '<')
|
| 44 |
+
value = value.replace(',', '<p>')
|
| 45 |
+
|
| 46 |
+
row_value = ("<tr>"
|
| 47 |
+
"<td>{key}</td>"
|
| 48 |
+
"<td>{value}</td>"
|
| 49 |
+
"<td>{mrz_value}</td>"
|
| 50 |
+
"</tr>".format(key=key, value='', mrz_value=value))
|
| 51 |
+
table_value = table_value + row_value
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
html = ("<table>"
|
| 55 |
+
"<tr>"
|
| 56 |
+
"<th style=""width:20%"">Field</th>"
|
| 57 |
+
"<th style=""width:40%"">Value</th>"
|
| 58 |
+
"<th style=""width:40%"">MRZ</th>"
|
| 59 |
+
"</tr>"
|
| 60 |
+
"<tr>"
|
| 61 |
+
"<td>Status</td>"
|
| 62 |
+
"<td>{status}</td>"
|
| 63 |
+
"<td></td>"
|
| 64 |
+
"</tr>"
|
| 65 |
+
"{table_value}"
|
| 66 |
+
"</table>".format(status=status, table_value=table_value))
|
| 67 |
+
|
| 68 |
+
table_value = ""
|
| 69 |
+
for key, value in r.json().items():
|
| 70 |
+
if key == 'Images':
|
| 71 |
+
for image_key, image_value in value.items():
|
| 72 |
+
row_value = ("<tr>"
|
| 73 |
+
"<td>{key}</td>"
|
| 74 |
+
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
|
| 75 |
+
"</tr>".format(key=image_key, base64_image=image_value))
|
| 76 |
+
table_value = table_value + row_value
|
| 77 |
+
|
| 78 |
+
images = ("<table>"
|
| 79 |
+
"<tr>"
|
| 80 |
+
"<th>Field</th>"
|
| 81 |
+
"<th>Image</th>"
|
| 82 |
+
"</tr>"
|
| 83 |
+
"{table_value}"
|
| 84 |
+
"</table>".format(table_value=table_value))
|
| 85 |
+
|
| 86 |
+
return [html, images]
|
| 87 |
+
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown(
|
| 90 |
+
"""
|
| 91 |
+
# KBY-AI
|
| 92 |
+
We offer SDKs for Face Recognition, Face Liveness Detection(Face Anti-Spoofing), and ID Card Recognition.<br/>
|
| 93 |
+
Besides that, we can provide several AI models and development services in machine learning.
|
| 94 |
+
|
| 95 |
+
## Simple Installation & Simple API
|
| 96 |
+
```
|
| 97 |
+
sudo docker pull kbyai/idcard-recognition:latest
|
| 98 |
+
sudo docker run -e LICENSE="xxxxx" -p 8082:8080 -p 9002:9000 kbyai/idcard-recognition:latest
|
| 99 |
+
```
|
| 100 |
+
## KYC Verification Demo
|
| 101 |
+
https://github.com/kby-ai/KYC-Verification
|
| 102 |
+
"""
|
| 103 |
+
)
|
| 104 |
+
with gr.TabItem("ID Card Recognition"):
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=3):
|
| 107 |
+
id_image_input = gr.Image(type='filepath')
|
| 108 |
+
gr.Examples(['idcard_examples/1.jpg', 'idcard_examples/2.jpg', 'idcard_examples/3.jpg'],
|
| 109 |
+
inputs=id_image_input)
|
| 110 |
+
id_recognition_button = gr.Button("ID Card Recognition")
|
| 111 |
+
with gr.Column(scale=5):
|
| 112 |
+
id_result_output = gr.HTML()
|
| 113 |
+
|
| 114 |
+
with gr.Column(scale=2):
|
| 115 |
+
image_result_output = gr.HTML()
|
| 116 |
+
|
| 117 |
+
id_recognition_button.click(idcard_recognition, inputs=id_image_input, outputs=[id_result_output, image_result_output])
|
| 118 |
+
|
| 119 |
+
demo.launch(server_name="0.0.0.0", server_port=9000)
|
idcard_examples/1.jpg
ADDED
|
idcard_examples/2.jpg
ADDED
|
idcard_examples/3.jpg
ADDED
|
idsdk.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from ctypes import *
|
| 4 |
+
|
| 5 |
+
libPath = os.path.abspath(os.path.dirname(__file__)) + '/libidsdk.so'
|
| 6 |
+
idsdk = cdll.LoadLibrary(libPath)
|
| 7 |
+
|
| 8 |
+
getMachineCode = idsdk.getMachineCode
|
| 9 |
+
getMachineCode.argtypes = []
|
| 10 |
+
getMachineCode.restype = c_char_p
|
| 11 |
+
|
| 12 |
+
setActivation = idsdk.setActivation
|
| 13 |
+
setActivation.argtypes = [c_char_p]
|
| 14 |
+
setActivation.restype = c_int32
|
| 15 |
+
|
| 16 |
+
initSDK = idsdk.initSDK
|
| 17 |
+
initSDK.argtypes = []
|
| 18 |
+
initSDK.restype = c_int32
|
| 19 |
+
|
| 20 |
+
idcardRecognition = idsdk.idcardRecognition
|
| 21 |
+
idcardRecognition.argtypes = [c_char_p, c_char_p]
|
| 22 |
+
idcardRecognition.restype = c_char_p
|
| 23 |
+
|
libidsdk.so
ADDED
|
Binary file (334 kB). View file
|
|
|
libimutils.so
ADDED
|
Binary file (412 kB). View file
|
|
|
libimutils.so_for_ubuntu22
ADDED
|
Binary file (412 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
+
gradio==3.50.2
|
| 4 |
+
datadog_api_client
|
run.sh
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
cd /root/kby-ai-idcard
|
| 4 |
+
exec python3 demo.py &
|
| 5 |
+
exec python3 app.py
|