File size: 3,859 Bytes
5d2426e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
sys.path.append('.')

import os
import base64
import json

from flask import Flask, request, jsonify
from idsdk import getMachineCode
from idsdk import setActivation
from idsdk import initSDK
from idsdk import idcardRecognition

licensePath = "license.txt"
license = ""

# Get a specific environment variable by name
license = os.environ.get("LICENSE")

# Check if the variable exists
if license is not None:
    print("Value of LICENSE:", license)
else:
    license = ""
    try:
        with open(licensePath, 'r') as file:
            license = file.read().strip()
    except IOError as exc:
        print("failed to open license.txt: ", exc.errno)
    print("license: ", license)

machineCode = getMachineCode()
print("machineCode: ", machineCode.decode('utf-8'))

ret = setActivation(license.encode('utf-8'))
print("activation: ", ret)

ret = initSDK()
print("init: ", ret)

app = Flask(__name__) 

@app.route('/idcard_recognition', methods=['POST'])
def idcard_recognition():
    try:
        file = request.files['file']

        base64_image = base64.b64encode(file.read()).decode('utf-8')
        ret = idcardRecognition(base64_image.encode('utf-8'), "".encode('utf-8'))

        if ret != None:
            j = json.loads(ret)
            j.update({"Status": "Ok"})
            response = jsonify(j)
        else:
            response = jsonify({"Status": "Error"})
    except:
        response = jsonify({"Status": "Error"})

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

@app.route('/idcard_recognition_multi_page', methods=['POST'])
def idcard_recognition_multi_page():
    try:
        file1 = request.files['file1']
        file2 = request.files['file2']

        base64_image1 = base64.b64encode(file1.read()).decode('utf-8')
        base64_image2 = base64.b64encode(file2.read()).decode('utf-8')
        ret = idcardRecognition(base64_image1.encode('utf-8'), base64_image2.encode('utf-8'))

        if ret != None:
            j = json.loads(ret)
            j.update({"Status": "Ok"})
            response = jsonify(j)
        else:
            response = jsonify({"Status": "Error"})
    except:
        response = jsonify({"Status": "Error"})

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

@app.route('/idcard_recognition_base64', methods=['POST'])
def idcard_recognition_base64():
    try:
        content = request.get_json()
        base64_image = content['base64']

        ret = idcardRecognition(base64_image.encode('utf-8'), "".encode('utf-8'))

        if ret != None:
            j = json.loads(ret)
            j.update({"Status": "Ok"})
            response = jsonify(j)
        else:
            response = jsonify({"Status": "Error"})
    except:
        response = jsonify({"Status": "Error"})

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

@app.route('/idcard_recognition_base64_multi_page', methods=['POST'])
def idcard_recognition_base64_multi_page():
    try:
        content = request.get_json()
        base64_image1 = content['base64_1']
        base64_image2 = content['base64_2']

        ret = idcardRecognition(base64_image1.encode('utf-8'), base64_image2.encode('utf-8'))

        if ret != None:
            j = json.loads(ret)
            j.update({"Status": "Ok"})
            response = jsonify(j)
        else:
            response = jsonify({"Status": "Error"})
    except:
        response = jsonify({"Status": "Error"})

    response.status_code = 200
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 8080))
    app.run(host='0.0.0.0', port=port)