Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
print("
|
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 |
-
response =
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
response.
|
91 |
-
response
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
response =
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
response.
|
123 |
-
response
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
response.
|
177 |
-
response
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
response =
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
response.
|
209 |
-
response
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|