Update app.py
Browse files
app.py
CHANGED
@@ -1,132 +1,133 @@
|
|
1 |
-
import sys
|
2 |
-
sys.path.append('.')
|
3 |
-
|
4 |
-
import os
|
5 |
-
import numpy as np
|
6 |
-
import base64
|
7 |
-
import json
|
8 |
-
import io
|
9 |
-
|
10 |
-
from PIL import Image, ExifTags
|
11 |
-
from flask import Flask, request, jsonify
|
12 |
-
from idlivesdk import getHWID
|
13 |
-
from idlivesdk import setLicenseKey
|
14 |
-
from idlivesdk import initSDK
|
15 |
-
from idlivesdk import processImage
|
16 |
-
|
17 |
-
licenseKeyPath = "license.txt"
|
18 |
-
license = os.environ.get("LICENSE_KEY")
|
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 |
-
response.
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
response.
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
response.
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
response.
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
response.
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
response.
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
132 |
app.run(host='0.0.0.0', port=port)
|
|
|
1 |
+
import sys
|
2 |
+
sys.path.append('.')
|
3 |
+
|
4 |
+
import os
|
5 |
+
import numpy as np
|
6 |
+
import base64
|
7 |
+
import json
|
8 |
+
import io
|
9 |
+
|
10 |
+
from PIL import Image, ExifTags
|
11 |
+
from flask import Flask, request, jsonify
|
12 |
+
from idlivesdk import getHWID
|
13 |
+
from idlivesdk import setLicenseKey
|
14 |
+
from idlivesdk import initSDK
|
15 |
+
from idlivesdk import processImage
|
16 |
+
|
17 |
+
licenseKeyPath = "license.txt"
|
18 |
+
license = os.environ.get("LICENSE_KEY")
|
19 |
+
print("License Key: ", license)
|
20 |
+
|
21 |
+
if license is None:
|
22 |
+
try:
|
23 |
+
with open(licenseKeyPath, 'r') as file:
|
24 |
+
license = file.read().strip()
|
25 |
+
except IOError as exc:
|
26 |
+
print("failed to open license.txt: ", exc.errno)
|
27 |
+
print("License Key: ", license)
|
28 |
+
|
29 |
+
hwid = getHWID()
|
30 |
+
print("HWID: ", hwid.decode('utf-8'))
|
31 |
+
|
32 |
+
ret = setLicenseKey(license.encode('utf-8'))
|
33 |
+
print("Set License: ", ret)
|
34 |
+
|
35 |
+
ret = initSDK("model".encode('utf-8'))
|
36 |
+
print("Init: ", ret)
|
37 |
+
|
38 |
+
app = Flask(__name__)
|
39 |
+
|
40 |
+
def apply_exif_rotation(image):
|
41 |
+
try:
|
42 |
+
exif = image._getexif()
|
43 |
+
if exif is not None:
|
44 |
+
for orientation in ExifTags.TAGS.keys():
|
45 |
+
if ExifTags.TAGS[orientation] == 'Orientation':
|
46 |
+
break
|
47 |
+
|
48 |
+
# Get the orientation value
|
49 |
+
orientation = exif.get(orientation, None)
|
50 |
+
|
51 |
+
# Apply the appropriate rotation based on the orientation
|
52 |
+
if orientation == 3:
|
53 |
+
image = image.rotate(180, expand=True)
|
54 |
+
elif orientation == 6:
|
55 |
+
image = image.rotate(270, expand=True)
|
56 |
+
elif orientation == 8:
|
57 |
+
image = image.rotate(90, expand=True)
|
58 |
+
|
59 |
+
except AttributeError:
|
60 |
+
print("No EXIF data found")
|
61 |
+
|
62 |
+
return image
|
63 |
+
|
64 |
+
|
65 |
+
@app.route('/process_image', methods=['POST'])
|
66 |
+
def process_image():
|
67 |
+
file = request.files['image']
|
68 |
+
|
69 |
+
try:
|
70 |
+
image = apply_exif_rotation(Image.open(file)).convert('RGB')
|
71 |
+
except:
|
72 |
+
result = "Failed to open file"
|
73 |
+
response = jsonify({"resultCode": "Error", "result": result})
|
74 |
+
|
75 |
+
response.status_code = 200
|
76 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
77 |
+
return response
|
78 |
+
|
79 |
+
image_np = np.asarray(image)
|
80 |
+
result = processImage(image_np, image_np.shape[1], image_np.shape[0])
|
81 |
+
|
82 |
+
if result is None:
|
83 |
+
result = "Failed to process image"
|
84 |
+
response = jsonify({"resultCode": "Error", "result": result})
|
85 |
+
|
86 |
+
response.status_code = 200
|
87 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
88 |
+
return response
|
89 |
+
else:
|
90 |
+
result_dict = json.loads(result.decode('utf-8'))
|
91 |
+
response = jsonify({"resultCode": "Ok", "result": result_dict})
|
92 |
+
|
93 |
+
response.status_code = 200
|
94 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
95 |
+
return response
|
96 |
+
|
97 |
+
@app.route('/process_image_base64', methods=['POST'])
|
98 |
+
def process_image_base64():
|
99 |
+
try:
|
100 |
+
content = request.get_json()
|
101 |
+
base64_image = content['base64']
|
102 |
+
|
103 |
+
image_data = base64.b64decode(base64_image)
|
104 |
+
image = apply_exif_rotation(Image.open(io.BytesIO(image_data))).convert("RGB")
|
105 |
+
except:
|
106 |
+
result = "Failed to parse base64"
|
107 |
+
response = jsonify({"resultCode": "Error", "result": result})
|
108 |
+
|
109 |
+
response.status_code = 200
|
110 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
111 |
+
return response
|
112 |
+
|
113 |
+
image_np = np.asarray(image)
|
114 |
+
result = processImage(image_np, image_np.shape[1], image_np.shape[0])
|
115 |
+
|
116 |
+
if result is None:
|
117 |
+
result = "Failed to process image"
|
118 |
+
response = jsonify({"resultCode": "Error", "result": result})
|
119 |
+
|
120 |
+
response.status_code = 200
|
121 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
122 |
+
return response
|
123 |
+
else:
|
124 |
+
result_dict = json.loads(result.decode('utf-8'))
|
125 |
+
response = jsonify({"resultCode": "Ok", "result": result_dict})
|
126 |
+
|
127 |
+
response.status_code = 200
|
128 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
129 |
+
return response
|
130 |
+
|
131 |
+
if __name__ == '__main__':
|
132 |
+
port = int(os.environ.get("PORT", 9000))
|
133 |
app.run(host='0.0.0.0', port=port)
|