Zhu-FaceOnLive commited on
Commit
4563550
·
verified ·
1 Parent(s): ba351d8

Update app.py

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