justin2341 commited on
Commit
c596d2f
·
verified ·
1 Parent(s): 3ded187

Upload 9 files

Browse files
Files changed (10) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +50 -0
  3. app.py +234 -0
  4. handtool-0.2.1-py3-none-any.whl +0 -0
  5. libhand.so +3 -0
  6. libopencv.zip +3 -0
  7. license.txt +5 -0
  8. requirements.txt +6 -0
  9. roi.py +92 -0
  10. run.sh +5 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ libhand.so filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:20.04
2
+
3
+ # Install system dependencies, including Python and pip
4
+ RUN apt-get update -y && \
5
+ apt-get install -y --no-install-recommends \
6
+ python3.8 \
7
+ python3-pip \
8
+ libjpeg8 \
9
+ libwebp6 \
10
+ libpng16-16 \
11
+ libtbb2 \
12
+ libtiff5 \
13
+ libtbb-dev \
14
+ unzip \
15
+ libopenexr-dev \
16
+ libgl1-mesa-glx \
17
+ libglib2.0-0 \
18
+ && apt-get clean \
19
+ && rm -rf /var/lib/apt/lists/*
20
+
21
+ # Ensure pip is installed
22
+ RUN python3.8 -m pip install --upgrade pip
23
+
24
+ # Set up working directory
25
+ RUN mkdir -p /root/kby-ai-palmprint
26
+ WORKDIR /root/kby-ai-palmprint
27
+
28
+ # Copy shared libraries and application files
29
+ COPY ./libhand.so /usr/local/lib/
30
+ COPY ./libopencv.zip .
31
+ RUN unzip libopencv.zip
32
+ RUN cp -f libopencv/* /usr/local/lib/
33
+ RUN ldconfig
34
+
35
+ # Copy Python and application files
36
+ COPY ./handtool-0.2.1-py3-none-any.whl .
37
+ COPY ./app.py .
38
+ COPY ./roi.py .
39
+ COPY ./requirements.txt .
40
+ COPY ./run.sh .
41
+ COPY ./img ./img
42
+
43
+ # Install Python dependencies
44
+ RUN pip3 install --no-cache-dir -r requirements.txt
45
+ RUN chmod +x ./run.sh
46
+ # Set up entrypoint
47
+ CMD ["/root/kby-ai-palmprint/run.sh"]
48
+
49
+ # Expose ports
50
+ EXPOSE 8080 9000
app.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append('.')
3
+
4
+ import os
5
+ import base64
6
+ import json
7
+ import handtool
8
+ import cv2
9
+ import numpy as np
10
+ from roi import get_roi, mat_to_bytes
11
+ from flask import Flask, request, jsonify
12
+
13
+ threshold = 0.15
14
+
15
+ licensePath = "license.txt"
16
+ license = ""
17
+
18
+ config = handtool.EncoderConfig(29, 5, 5, 10)
19
+ encoder = handtool.create_encoder(config)
20
+
21
+ machineCode = encoder.getMachineCode()
22
+ print("\nmachineCode: ", machineCode.decode('utf-8'))
23
+
24
+ try:
25
+ with open(licensePath, 'r') as file:
26
+ license = file.read()
27
+ except IOError as exc:
28
+ print("failed to open license.txt: ", exc.errno)
29
+ print("\nlicense: ", license)
30
+
31
+ ret = encoder.setActivation(license.encode('utf-8'))
32
+ print("\nactivation: ", ret)
33
+
34
+ _ = encoder.init()
35
+ print("\ninit: ", ret)
36
+
37
+ app = Flask(__name__)
38
+
39
+ @app.route('/compare_palmprint', methods=['POST'])
40
+ def compare_palmprint():
41
+ result = "None"
42
+ similarity = -1
43
+
44
+ palm1 = None
45
+ palm2 = None
46
+
47
+ file1 = request.files['file1']
48
+ file2 = request.files['file2']
49
+
50
+ try:
51
+ image1 = cv2.imdecode(np.frombuffer(file1.read(), np.uint8), cv2.IMREAD_COLOR)
52
+
53
+ except:
54
+ result = "Failed to open file1"
55
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
56
+
57
+ response.status_code = 200
58
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
59
+ return response
60
+
61
+
62
+ try:
63
+ image2 = cv2.imdecode(np.frombuffer(file2.read(), np.uint8), cv2.IMREAD_COLOR)
64
+ except:
65
+ result = "Failed to open file2"
66
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
67
+
68
+ response.status_code = 200
69
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
70
+ return response
71
+
72
+ img1 = mat_to_bytes(image1)
73
+ img2 = mat_to_bytes(image2)
74
+ hand_type_1, x11, y11, x12, y12, detect_state_1 = encoder.detect_using_bytes(img1)
75
+ hand_type_2, x21, y21, x22, y22, detect_state_2 = encoder.detect_using_bytes(img2)
76
+
77
+ palm1 = {"hand_type": hand_type_1, "x1": x11, "y1": y11, "x2": x12, "y2": y12}
78
+ palm2 = {"hand_type": hand_type_2, "x1": x21, "y1": y21, "x2": x22, "y2": y22}
79
+
80
+ if hand_type_1 != hand_type_2:
81
+ result = "Different hand"
82
+ # print(f"\n 2 images are from the different hand\n similarity: 0.0")
83
+ similarity = 0.0
84
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
85
+ response.status_code = 200
86
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
87
+ return response
88
+
89
+ if detect_state_1 == 0 or detect_state_2 == 0:
90
+ if ret != 0:
91
+ result = "Activation failed"
92
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
93
+ response.status_code = 200
94
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
95
+ return response
96
+
97
+ # print(f"\n hand detection failed !\n plesae make sure that input hand image is valid or not.")
98
+ result = "hand detection failed !\nPlesae make sure that input hand image is valid or not."
99
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
100
+ response.status_code = 200
101
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
102
+ return response
103
+
104
+ if detect_state_1 >= 2 or detect_state_2 >= 2:
105
+ # print(f"\n multi-hand detected !\n plesae put one hand image, not multiple hand.")
106
+ result = "multi-hand detected !\nPlesae try on image with one hand , not multiple hand."
107
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
108
+ response.status_code = 200
109
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
110
+ return response
111
+
112
+ roi1 = mat_to_bytes(get_roi(image1, hand_type_1, x11, y11, x12, y12))
113
+ roi2 = mat_to_bytes(get_roi(image2, hand_type_2, x21, y21, x22, y22))
114
+
115
+ one_palmprint_code = encoder.encode_using_bytes(roi1)
116
+ another_palmprint_code = encoder.encode_using_bytes(roi2)
117
+ score = one_palmprint_code.compare_to(another_palmprint_code)
118
+
119
+ if score >= threshold:
120
+ result = "Same hand"
121
+ similarity = score
122
+ # print(f"\n 2 images are from the same hand\n similarity: {score}")
123
+ else:
124
+ result = "Different hand"
125
+ similarity = score
126
+ # print(f"\n 2 images are from the different hand\n similarity: {score}")
127
+
128
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
129
+
130
+ response.status_code = 200
131
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
132
+ return response
133
+
134
+ @app.route('/compare_palmprint_base64', methods=['POST'])
135
+ def compare_palmprint_base64():
136
+ result = "None"
137
+ similarity = -1
138
+
139
+ palm1 = None
140
+ palm2 = None
141
+
142
+ content = request.get_json()
143
+
144
+ try:
145
+ imageBase64_1 = content['base64_1']
146
+ image_data1 = base64.b64decode(imageBase64_1)
147
+ np_array = np.frombuffer(image_data1, np.uint8)
148
+ image1 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
149
+ except:
150
+ result = "Failed to open file1"
151
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
152
+
153
+ response.status_code = 200
154
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
155
+ return response
156
+
157
+ try:
158
+ imageBase64_2 = content['base64_2']
159
+ image_data2 = base64.b64decode(imageBase64_2)
160
+ np_array = np.frombuffer(image_data2, np.uint8)
161
+ image2 = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
162
+ except IOError as exc:
163
+ result = "Failed to open file2"
164
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
165
+
166
+ response.status_code = 200
167
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
168
+ return response
169
+
170
+ img1 = mat_to_bytes(image1)
171
+ img2 = mat_to_bytes(image2)
172
+ hand_type_1, x11, y11, x12, y12, detect_state_1 = encoder.detect_using_bytes(img1)
173
+ hand_type_2, x21, y21, x22, y22, detect_state_2 = encoder.detect_using_bytes(img2)
174
+
175
+ palm1 = {"hand_type": hand_type_1, "x1": x11, "y1": y11, "x2": x12, "y2": y12}
176
+ palm2 = {"hand_type": hand_type_2, "x1": x21, "y1": y21, "x2": x22, "y2": y22}
177
+
178
+ if hand_type_1 != hand_type_2:
179
+ result = "Different hand"
180
+ # print(f"\n 2 images are from the different hand\n similarity: 0.0")
181
+ similarity = 0.0
182
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
183
+ response.status_code = 200
184
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
185
+ return response
186
+
187
+ if detect_state_1 == 0 or detect_state_2 == 0:
188
+ if ret != 0:
189
+ result = "Activation failed"
190
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
191
+ response.status_code = 200
192
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
193
+ return response
194
+
195
+ # print(f"\n hand detection failed !\n plesae make sure that input hand image is valid or not.")
196
+ result = "hand detection failed !\nPlesae make sure that input hand image is valid or not."
197
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
198
+ response.status_code = 200
199
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
200
+ return response
201
+
202
+ if detect_state_1 >= 2 or detect_state_2 >= 2:
203
+ # print(f"\n multi-hand detected !\n plesae put one hand image, not multiple hand.")
204
+ result = "multi-hand detected !\nPlesae try on image with one hand , not multiple hand."
205
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
206
+ response.status_code = 200
207
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
208
+ return response
209
+
210
+ roi1 = mat_to_bytes(get_roi(image1, hand_type_1, x11, y11, x12, y12))
211
+ roi2 = mat_to_bytes(get_roi(image2, hand_type_2, x21, y21, x22, y22))
212
+
213
+ one_palmprint_code = encoder.encode_using_bytes(roi1)
214
+ another_palmprint_code = encoder.encode_using_bytes(roi2)
215
+ score = one_palmprint_code.compare_to(another_palmprint_code)
216
+
217
+ if score >= threshold:
218
+ result = "Same hand"
219
+ similarity = score
220
+ # print(f"\n 2 images are from the same hand\n similarity: {score}")
221
+ else:
222
+ result = "Different hand"
223
+ similarity = score
224
+ # print(f"\n 2 images are from the different hand\n similarity: {score}")
225
+
226
+ response = jsonify({"compare_result": result, "compare_similarity": similarity, "palm1": palm1, "palm2": palm2})
227
+
228
+ response.status_code = 200
229
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
230
+ return response
231
+
232
+ if __name__ == '__main__':
233
+ port = int(os.environ.get("PORT", 8080))
234
+ app.run(host='0.0.0.0', port=port)
handtool-0.2.1-py3-none-any.whl ADDED
Binary file (7.48 kB). View file
 
libhand.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0073f5a06452296d583f8154c4964376fef08bc305c17f54445f131b50cbc7b8
3
+ size 7627752
libopencv.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8845c1412c45c484e054235269944e2ac43c90a148ce3444215fe52049cf7479
3
+ size 61014815
license.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ sJBJyD6qMa5QrFncFtWmTa6pe1nWGDPknhDsUNl6NCgcj7/Cd/t7qEngL/arKSDLYXYe2JJT8EFz
2
+ 3jDmSNAReSQOEsm+vwcN7yEDaJyRS6BCmoCTkQpfLfoviTJ6LhhOQZ197gvd0FaJGkP+0R2RVlcJ
3
+ qCZPuKhF8JY4FI58Fu3cXQT1W4fJ8f1YHZ3xsg4JJ7cowE9PT3+5VEVnU8HjQM8+If8n3fWDm5cf
4
+ T83pntPeSXKaHtiP3eOGzVTR+Wl6B3cUMYEjJ0RQI0Q66hrju9t/7BehTBO3Sct2PGUmWAeKJdaV
5
+ m8DEZWvJ/UDLGx9TepF2Hh1hpFdCvTe7iQWOUQ==
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ gradio==3.50.2
4
+ datadog_api_client
5
+ opencv-python
6
+ handtool-0.2.1-py3-none-any.whl
roi.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ roi_bad_pixel_number = 500 # the number of pixel where 3 values are zero in RGB channel on ROI image
5
+ roi_aspect_ratio_threshold = 200 # the passable aspect ratio threshold of ROI image
6
+ cupped_pose_threshold = 3 # this value determines if hand is cupped or not
7
+ frame_margin = 300 # the extra marine size of the frame inputted into Google Mediapipe Graph (this value must be a
8
+ # multiple of two)
9
+ roi_size_threshold = 0.23
10
+
11
+ def mat_to_bytes(mat):
12
+ """
13
+ Convert cv::Mat image data (NumPy array in Python) to raw bytes.
14
+ """
15
+ # Encode cv::Mat as PNG bytes
16
+ is_success, buffer = cv2.imencode(".png", mat)
17
+ if not is_success:
18
+ raise ValueError("Failed to encode cv::Mat image")
19
+ return buffer.tobytes()
20
+
21
+ def img_crop(img_original, x2, x1, y2, y1, label):
22
+
23
+ h, w, _ = img_original.shape
24
+ img = np.zeros((h + 20, w + 20, 3), np.uint8)
25
+ img[10:-10, 10:-10, :] = img_original
26
+
27
+ if label == "Left":
28
+ v1 = np.array([x2, y2])
29
+ v2 = np.array([x1, y1])
30
+ else:
31
+ v2 = np.array([x2, y2])
32
+ v1 = np.array([x1, y1])
33
+
34
+ theta = np.arctan2((v2 - v1)[1], (v2 - v1)[0]) * 180 / np.pi
35
+ R = cv2.getRotationMatrix2D(tuple([int(v2[0]), int(v2[1])]), theta, 1)
36
+
37
+ v1 = (R[:, :2] @ v1 + R[:, -1]).astype(int)
38
+ v2 = (R[:, :2] @ v2 + R[:, -1]).astype(int)
39
+ img_r = cv2.warpAffine(img, R, (w, h))
40
+
41
+ if 1:
42
+ ux = int(v1[0] - (v2 - v1)[0] * 0.05)
43
+ uy = int(v1[1] + (v2 - v1)[0] * 0.05)
44
+ lx = int(v2[0] + (v2 - v1)[0] * 0.05)
45
+ ly = int(v2[1] + (v2 - v1)[0] * 1)
46
+ else:
47
+ ux = int(v1[0] - (v2 - v1)[0] * 0.1)
48
+ uy = int(v1[1] + (v2 - v1)[0] * 0.1)
49
+ lx = int(v2[0] + (v2 - v1)[0] * 0.1)
50
+ ly = int(v2[1] + (v2 - v1)[0] * 1.2)
51
+
52
+ # delta_y is movement value in y ward
53
+ delta_y = (ly - uy) * 0.15
54
+
55
+ ly = int(ly - delta_y)
56
+ uy = int(uy - delta_y)
57
+
58
+ delta_x = (lx - ux) * 0.01
59
+ lx = int(lx + delta_x)
60
+ ux = int(ux + delta_x)
61
+
62
+ if label == "Right":
63
+ delta_x = (lx - ux) * 0.05
64
+ lx = int(lx + delta_x)
65
+ ux = int(ux + delta_x)
66
+ # roi = img_r
67
+ roi = img_r[uy:ly, ux:lx]
68
+ if roi.shape[0] == 0 or roi.shape[1] == 0:
69
+ print("error 1")
70
+ return None, 3
71
+ if abs(roi.shape[0] - roi.shape[1]) > roi_aspect_ratio_threshold:
72
+ print("error 2", abs(roi.shape[0] - roi.shape[1]))
73
+ return None, 4
74
+ if roi.shape[1] / w < roi_size_threshold:
75
+ print("error 3", roi.shape[1] / w)
76
+ return None, 7
77
+
78
+ n_zeros = np.count_nonzero(roi == 0)
79
+ # if n_zeros > roi_bad_pixel_number:
80
+ # print("error 4", n_zeros)
81
+ # return None, 5
82
+ return roi, 0
83
+
84
+ def get_roi(img, hand_type, x1, y1, x2, y2):
85
+
86
+ if hand_type == 0:
87
+ label = "Left"
88
+ else:
89
+ label = "Right"
90
+ roi, _ = img_crop(img, x1, x2, y1, y2, label)
91
+ cv2.imwrite('test.jpg', roi)
92
+ return roi
run.sh ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ cd /root/kby-ai-palmprint
4
+ # exec python3 demo.py &
5
+ exec python3 app.py