justin2341 commited on
Commit
d7f61be
·
verified ·
1 Parent(s): 31da89a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +220 -213
app.py CHANGED
@@ -1,214 +1,221 @@
1
- import sys
2
- sys.path.append('.')
3
-
4
- import os
5
- import base64
6
- import json
7
- from ctypes import *
8
- import cv2
9
- import numpy as np
10
- from flask import Flask, request, jsonify
11
- from veinsdk import *
12
- from roi import *
13
-
14
- licensePath = "license.txt"
15
- license = ""
16
-
17
- machineCode = getMachineCode()
18
- print("\nmachineCode: ", machineCode.decode('utf-8'))
19
-
20
- try:
21
- with open(licensePath, 'r') as file:
22
- license = file.read().strip()
23
- except IOError as exc:
24
- print("failed to open license.txt: ", exc.errno)
25
-
26
- print("\nlicense: ", license)
27
-
28
- ret = setActivation(license.encode('utf-8'))
29
- print("\nactivation: ", ret)
30
-
31
- ret = initSDK()
32
- print("init: ", ret)
33
-
34
- app = Flask(__name__)
35
-
36
- def mat_to_bytes(mat):
37
- """
38
- Convert cv::Mat image data (NumPy array in Python) to raw bytes.
39
- """
40
- # Encode cv::Mat as PNG bytes
41
- is_success, buffer = cv2.imencode(".png", mat)
42
- if not is_success:
43
- raise ValueError("Failed to encode cv::Mat image")
44
- return buffer.tobytes()
45
-
46
- @app.route('/palmvein', methods=['POST'])
47
- def palmvein():
48
- result = None
49
- score = None
50
-
51
- file1 = request.files['file1']
52
- file2 = request.files['file2']
53
-
54
- try:
55
- image1 = cv2.imdecode(np.frombuffer(file1.read(), np.uint8), cv2.IMREAD_COLOR)
56
- except:
57
- result = "Failed to open file1"
58
- response = jsonify({"result": result, "score": float(score)})
59
-
60
- response.status_code = 200
61
- response.headers["Content-Type"] = "application/json; charset=utf-8"
62
- return response
63
-
64
- try:
65
- image2 = cv2.imdecode(np.frombuffer(file2.read(), np.uint8), cv2.IMREAD_COLOR)
66
- except:
67
- result = "Failed to open file2"
68
- response = jsonify({"result": result, "score": float(score)})
69
-
70
- response.status_code = 200
71
- response.headers["Content-Type"] = "application/json; charset=utf-8"
72
- return response
73
-
74
- roi1, label1 = get_roi_image(cv2.flip(image1, 1))
75
- roi2, label2 = get_roi_image(cv2.flip(image2, 1))
76
-
77
- if label1 != label2:
78
- result = "2 images are from the different hand"
79
- score = 0.0
80
- response = jsonify({"result": result, "score": float(score)})
81
-
82
- response.status_code = 200
83
- response.headers["Content-Type"] = "application/json; charset=utf-8"
84
- return response
85
-
86
- if roi1 is None or roi2 is None:
87
- result = "\n hand detection failed !\n plesae make sure that input hand image is valid or not."
88
- response = jsonify({"result": result, "score": float(score)})
89
-
90
- response.status_code = 200
91
- response.headers["Content-Type"] = "application/json; charset=utf-8"
92
- return response
93
-
94
- roi_byte1 = mat_to_bytes(roi1)
95
- roi_byte2 = mat_to_bytes(roi2)
96
- feature_array1, feature_array2 = (c_float * 1024)(), (c_float * 1024)() # Assuming a maximum of 256 rectangles
97
- cnt1 = getFeature(roi_byte1, len(roi_byte1), feature_array1)
98
- cnt2 = getFeature(roi_byte2, len(roi_byte2), feature_array2)
99
-
100
- if cnt1 == 0 or cnt2 ==0:
101
- result = "feature extraction failed !"
102
- response = jsonify({"result": result, "score": float(score)})
103
-
104
- response.status_code = 200
105
- response.headers["Content-Type"] = "application/json; charset=utf-8"
106
- return response
107
-
108
- score = getScore(feature_array1, cnt1, feature_array2, cnt2)
109
- if score >= 0.65:
110
- result = "Same Hand !"
111
- # print(f"\n 2 images are from the same hand\n similarity: {score}")
112
- response = jsonify({"result": result, "score": float(score)})
113
-
114
- response.status_code = 200
115
- response.headers["Content-Type"] = "application/json; charset=utf-8"
116
- return response
117
- else:
118
- result = "Different Hand !"
119
- # print(f"\n 2 images are from the different hand\n similarity: {score}")
120
- response = jsonify({"result": result, "score": float(score)})
121
-
122
- response.status_code = 200
123
- response.headers["Content-Type"] = "application/json; charset=utf-8"
124
- return response
125
-
126
- @app.route('/palmvein_base64', methods=['POST'])
127
- def palmvein_base64():
128
-
129
- result = None
130
- score = None
131
-
132
- content = request.get_json()
133
-
134
- try:
135
- imageBase64 = content['base64_1']
136
- image_data = base64.b64decode(imageBase64)
137
- np_array = np.frombuffer(image_data, np.uint8)
138
- image1 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
139
- except:
140
- result = "Failed to open file1"
141
- response = jsonify({"result": result, "score": float(score)})
142
-
143
- response.status_code = 200
144
- response.headers["Content-Type"] = "application/json; charset=utf-8"
145
- return response
146
-
147
- try:
148
- imageBase64 = content['base64_2']
149
- image_data = base64.b64decode(imageBase64)
150
- np_array = np.frombuffer(image_data, np.uint8)
151
- image2 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
152
- except:
153
- result = "Failed to open file2"
154
- response = jsonify({"result": result, "score": float(score)})
155
-
156
- response.status_code = 200
157
- response.headers["Content-Type"] = "application/json; charset=utf-8"
158
- return response
159
-
160
- roi1, label1 = get_roi_image(cv2.flip(image1, 1))
161
- roi2, label2 = get_roi_image(cv2.flip(image2, 1))
162
-
163
- if label1 != label2:
164
- result = "2 images are from the different hand"
165
- score = 0.0
166
- response = jsonify({"result": result, "score": float(score)})
167
-
168
- response.status_code = 200
169
- response.headers["Content-Type"] = "application/json; charset=utf-8"
170
- return response
171
-
172
- if roi1 is None or roi2 is None:
173
- result = "\n hand detection failed !\n plesae make sure that input hand image is valid or not."
174
- response = jsonify({"result": result, "score": float(score)})
175
-
176
- response.status_code = 200
177
- response.headers["Content-Type"] = "application/json; charset=utf-8"
178
- return response
179
-
180
- roi_byte1 = mat_to_bytes(roi1)
181
- roi_byte2 = mat_to_bytes(roi2)
182
- feature_array1, feature_array2 = (c_float * 1024)(), (c_float * 1024)() # Assuming a maximum of 256 rectangles
183
- cnt1 = getFeature(roi_byte1, len(roi_byte1), feature_array1)
184
- cnt2 = getFeature(roi_byte2, len(roi_byte2), feature_array2)
185
-
186
- if cnt1 == 0 or cnt2 ==0:
187
- result = "feature extraction failed !"
188
- response = jsonify({"result": result, "score": float(score)})
189
-
190
- response.status_code = 200
191
- response.headers["Content-Type"] = "application/json; charset=utf-8"
192
- return response
193
-
194
- score = getScore(feature_array1, cnt1, feature_array2, cnt2)
195
- if score >= 0.65:
196
- result = "Same Hand !"
197
- # print(f"\n 2 images are from the same hand\n similarity: {score}")
198
- response = jsonify({"result": result, "score": float(score)})
199
-
200
- response.status_code = 200
201
- response.headers["Content-Type"] = "application/json; charset=utf-8"
202
- return response
203
- else:
204
- result = "Different Hand !"
205
- # print(f"\n 2 images are from the different hand\n similarity: {score}")
206
- response = jsonify({"result": result, "score": float(score)})
207
-
208
- response.status_code = 200
209
- response.headers["Content-Type"] = "application/json; charset=utf-8"
210
- return response
211
-
212
- if __name__ == '__main__':
213
- port = int(os.environ.get("PORT", 8080))
 
 
 
 
 
 
 
214
  app.run(host='0.0.0.0', port=port)
 
1
+ import sys
2
+ sys.path.append('.')
3
+
4
+ import os
5
+ import base64
6
+ import json
7
+ from ctypes import *
8
+ import cv2
9
+ import numpy as np
10
+ from flask import Flask, request, jsonify
11
+ from veinsdk import *
12
+ from roi import *
13
+
14
+ licensePath = "license.txt"
15
+ license = ""
16
+
17
+ machineCode = getMachineCode()
18
+ print("\nmachineCode: ", machineCode.decode('utf-8'))
19
+
20
+ # Get a specific environment variable by name
21
+ license = os.environ.get("LICENSE")
22
+
23
+ # Check if the variable exists
24
+ if license is not None:
25
+ print("Value of LICENSE:")
26
+ else:
27
+ license = ""
28
+ try:
29
+ with open(licensePath, 'r') as file:
30
+ license = file.read().strip()
31
+ except IOError as exc:
32
+ print("failed to open license.txt: ", exc.errno)
33
+ print("license: ", license)
34
+
35
+ ret = setActivation(license.encode('utf-8'))
36
+ print("\nactivation: ", ret)
37
+
38
+ ret = initSDK()
39
+ print("init: ", ret)
40
+
41
+ app = Flask(__name__)
42
+
43
+ def mat_to_bytes(mat):
44
+ """
45
+ Convert cv::Mat image data (NumPy array in Python) to raw bytes.
46
+ """
47
+ # Encode cv::Mat as PNG bytes
48
+ is_success, buffer = cv2.imencode(".png", mat)
49
+ if not is_success:
50
+ raise ValueError("Failed to encode cv::Mat image")
51
+ return buffer.tobytes()
52
+
53
+ @app.route('/palmvein', methods=['POST'])
54
+ def palmvein():
55
+ result = None
56
+ score = None
57
+
58
+ file1 = request.files['file1']
59
+ file2 = request.files['file2']
60
+
61
+ try:
62
+ image1 = cv2.imdecode(np.frombuffer(file1.read(), np.uint8), cv2.IMREAD_COLOR)
63
+ except:
64
+ result = "Failed to open file1"
65
+ response = jsonify({"result": result, "score": float(score)})
66
+
67
+ response.status_code = 200
68
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
69
+ return response
70
+
71
+ try:
72
+ image2 = cv2.imdecode(np.frombuffer(file2.read(), np.uint8), cv2.IMREAD_COLOR)
73
+ except:
74
+ result = "Failed to open file2"
75
+ response = jsonify({"result": result, "score": float(score)})
76
+
77
+ response.status_code = 200
78
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
79
+ return response
80
+
81
+ roi1, label1 = get_roi_image(cv2.flip(image1, 1))
82
+ roi2, label2 = get_roi_image(cv2.flip(image2, 1))
83
+
84
+ if label1 != label2:
85
+ result = "2 images are from the different hand"
86
+ score = 0.0
87
+ response = jsonify({"result": result, "score": float(score)})
88
+
89
+ response.status_code = 200
90
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
91
+ return response
92
+
93
+ if roi1 is None or roi2 is None:
94
+ result = "\n hand detection failed !\n plesae make sure that input hand image is valid or not."
95
+ response = jsonify({"result": result, "score": float(score)})
96
+
97
+ response.status_code = 200
98
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
99
+ return response
100
+
101
+ roi_byte1 = mat_to_bytes(roi1)
102
+ roi_byte2 = mat_to_bytes(roi2)
103
+ feature_array1, feature_array2 = (c_float * 1024)(), (c_float * 1024)() # Assuming a maximum of 256 rectangles
104
+ cnt1 = getFeature(roi_byte1, len(roi_byte1), feature_array1)
105
+ cnt2 = getFeature(roi_byte2, len(roi_byte2), feature_array2)
106
+
107
+ if cnt1 == 0 or cnt2 ==0:
108
+ result = "feature extraction failed !"
109
+ response = jsonify({"result": result, "score": float(score)})
110
+
111
+ response.status_code = 200
112
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
113
+ return response
114
+
115
+ score = getScore(feature_array1, cnt1, feature_array2, cnt2)
116
+ if score >= 0.65:
117
+ result = "Same Hand !"
118
+ # print(f"\n 2 images are from the same hand\n similarity: {score}")
119
+ response = jsonify({"result": result, "score": float(score)})
120
+
121
+ response.status_code = 200
122
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
123
+ return response
124
+ else:
125
+ result = "Different Hand !"
126
+ # print(f"\n 2 images are from the different hand\n similarity: {score}")
127
+ response = jsonify({"result": result, "score": float(score)})
128
+
129
+ response.status_code = 200
130
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
131
+ return response
132
+
133
+ @app.route('/palmvein_base64', methods=['POST'])
134
+ def palmvein_base64():
135
+
136
+ result = None
137
+ score = None
138
+
139
+ content = request.get_json()
140
+
141
+ try:
142
+ imageBase64 = content['base64_1']
143
+ image_data = base64.b64decode(imageBase64)
144
+ np_array = np.frombuffer(image_data, np.uint8)
145
+ image1 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
146
+ except:
147
+ result = "Failed to open file1"
148
+ response = jsonify({"result": result, "score": float(score)})
149
+
150
+ response.status_code = 200
151
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
152
+ return response
153
+
154
+ try:
155
+ imageBase64 = content['base64_2']
156
+ image_data = base64.b64decode(imageBase64)
157
+ np_array = np.frombuffer(image_data, np.uint8)
158
+ image2 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
159
+ except:
160
+ result = "Failed to open file2"
161
+ response = jsonify({"result": result, "score": float(score)})
162
+
163
+ response.status_code = 200
164
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
165
+ return response
166
+
167
+ roi1, label1 = get_roi_image(cv2.flip(image1, 1))
168
+ roi2, label2 = get_roi_image(cv2.flip(image2, 1))
169
+
170
+ if label1 != label2:
171
+ result = "2 images are from the different hand"
172
+ score = 0.0
173
+ response = jsonify({"result": result, "score": float(score)})
174
+
175
+ response.status_code = 200
176
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
177
+ return response
178
+
179
+ if roi1 is None or roi2 is None:
180
+ result = "\n hand detection failed !\n plesae make sure that input hand image is valid or not."
181
+ response = jsonify({"result": result, "score": float(score)})
182
+
183
+ response.status_code = 200
184
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
185
+ return response
186
+
187
+ roi_byte1 = mat_to_bytes(roi1)
188
+ roi_byte2 = mat_to_bytes(roi2)
189
+ feature_array1, feature_array2 = (c_float * 1024)(), (c_float * 1024)() # Assuming a maximum of 256 rectangles
190
+ cnt1 = getFeature(roi_byte1, len(roi_byte1), feature_array1)
191
+ cnt2 = getFeature(roi_byte2, len(roi_byte2), feature_array2)
192
+
193
+ if cnt1 == 0 or cnt2 ==0:
194
+ result = "feature extraction failed !"
195
+ response = jsonify({"result": result, "score": float(score)})
196
+
197
+ response.status_code = 200
198
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
199
+ return response
200
+
201
+ score = getScore(feature_array1, cnt1, feature_array2, cnt2)
202
+ if score >= 0.65:
203
+ result = "Same Hand !"
204
+ # print(f"\n 2 images are from the same hand\n similarity: {score}")
205
+ response = jsonify({"result": result, "score": float(score)})
206
+
207
+ response.status_code = 200
208
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
209
+ return response
210
+ else:
211
+ result = "Different Hand !"
212
+ # print(f"\n 2 images are from the different hand\n similarity: {score}")
213
+ response = jsonify({"result": result, "score": float(score)})
214
+
215
+ response.status_code = 200
216
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
217
+ return response
218
+
219
+ if __name__ == '__main__':
220
+ port = int(os.environ.get("PORT", 8080))
221
  app.run(host='0.0.0.0', port=port)