Spaces:
Running
Running
justin2341
commited on
Commit
•
4c8fc14
1
Parent(s):
2912dde
Upload 4 files
Browse files- .gitattributes +1 -0
- app.py +105 -78
- facesdk.py +7 -7
- libkbyai_facesdk2.so +3 -0
.gitattributes
CHANGED
@@ -34,3 +34,4 @@ saved_model/**/* 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 |
libfacesdk2.so 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 |
libfacesdk2.so filter=lfs diff=lfs merge=lfs -text
|
37 |
+
libkbyai_facesdk2.so filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
@@ -16,9 +16,7 @@ from facesdk import templateExtraction
|
|
16 |
from facesdk import similarityCalculation
|
17 |
from facebox import FaceBox
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
maxFaceCount = 1
|
22 |
|
23 |
licensePath = "license.txt"
|
24 |
license = ""
|
@@ -52,11 +50,6 @@ app = Flask(__name__)
|
|
52 |
|
53 |
@app.route('/compare_face', methods=['POST'])
|
54 |
def compare_face():
|
55 |
-
result = "None"
|
56 |
-
similarity = -1
|
57 |
-
face1 = None
|
58 |
-
face2 = None
|
59 |
-
|
60 |
file1 = request.files['file1']
|
61 |
file2 = request.files['file2']
|
62 |
|
@@ -64,7 +57,7 @@ def compare_face():
|
|
64 |
image1 = Image.open(file1).convert('RGB')
|
65 |
except:
|
66 |
result = "Failed to open file1"
|
67 |
-
response = jsonify({"
|
68 |
|
69 |
response.status_code = 200
|
70 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
@@ -75,7 +68,7 @@ def compare_face():
|
|
75 |
image2 = Image.open(file2).convert('RGB')
|
76 |
except:
|
77 |
result = "Failed to open file2"
|
78 |
-
response = jsonify({"
|
79 |
|
80 |
response.status_code = 200
|
81 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
@@ -90,56 +83,71 @@ def compare_face():
|
|
90 |
faceBoxes2 = (FaceBox * maxFaceCount)()
|
91 |
faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
if similarity > verifyThreshold:
|
98 |
-
result = "Same person"
|
99 |
-
else:
|
100 |
-
result = "Different person"
|
101 |
-
elif faceCount1 == 0:
|
102 |
-
result = "No face1"
|
103 |
-
elif faceCount2 == 0:
|
104 |
-
result = "No face2"
|
105 |
|
106 |
-
if faceCount1 == 1:
|
107 |
landmark_68 = []
|
108 |
for j in range(68):
|
109 |
-
landmark_68.append({"x": faceBoxes1[
|
110 |
|
111 |
-
|
112 |
-
"yaw": faceBoxes1[
|
113 |
-
"face_quality": faceBoxes1[
|
114 |
-
"left_eye_closed": faceBoxes1[
|
115 |
-
"face_occlusion": faceBoxes1[
|
116 |
"landmark_68": landmark_68}
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
-
if faceCount2 == 1:
|
119 |
landmark_68 = []
|
120 |
for j in range(68):
|
121 |
-
landmark_68.append({"x": faceBoxes2[
|
|
|
122 |
|
123 |
-
|
124 |
-
"yaw": faceBoxes2[
|
125 |
-
"face_quality": faceBoxes2[
|
126 |
-
"left_eye_closed": faceBoxes2[
|
127 |
-
"face_occlusion": faceBoxes2[
|
128 |
"landmark_68": landmark_68}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
|
|
|
|
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
|
|
|
136 |
@app.route('/compare_face_base64', methods=['POST'])
|
137 |
def compare_face_base64():
|
138 |
-
result = "None"
|
139 |
-
similarity = -1
|
140 |
-
face1 = None
|
141 |
-
face2 = None
|
142 |
-
|
143 |
content = request.get_json()
|
144 |
|
145 |
try:
|
@@ -148,7 +156,7 @@ def compare_face_base64():
|
|
148 |
image1 = Image.open(io.BytesIO(image_data1)).convert('RGB')
|
149 |
except:
|
150 |
result = "Failed to open file1"
|
151 |
-
response = jsonify({"
|
152 |
|
153 |
response.status_code = 200
|
154 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
@@ -159,8 +167,8 @@ def compare_face_base64():
|
|
159 |
image_data2 = base64.b64decode(imageBase64_2)
|
160 |
image2 = Image.open(io.BytesIO(image_data2)).convert('RGB')
|
161 |
except IOError as exc:
|
162 |
-
result = "Failed to open
|
163 |
-
response = jsonify({"
|
164 |
|
165 |
response.status_code = 200
|
166 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
@@ -175,48 +183,67 @@ def compare_face_base64():
|
|
175 |
faceBoxes2 = (FaceBox * maxFaceCount)()
|
176 |
faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
if similarity > verifyThreshold:
|
183 |
-
result = "Same person"
|
184 |
-
else:
|
185 |
-
result = "Different person"
|
186 |
-
elif faceCount1 == 0:
|
187 |
-
result = "No face1"
|
188 |
-
elif faceCount2 == 0:
|
189 |
-
result = "No face2"
|
190 |
|
191 |
-
if faceCount1 == 1:
|
192 |
landmark_68 = []
|
193 |
for j in range(68):
|
194 |
-
landmark_68.append({"x": faceBoxes1[
|
195 |
|
196 |
-
|
197 |
-
"yaw": faceBoxes1[
|
198 |
-
"face_quality": faceBoxes1[
|
199 |
-
"left_eye_closed": faceBoxes1[
|
200 |
-
"face_occlusion": faceBoxes1[
|
201 |
"landmark_68": landmark_68}
|
|
|
|
|
|
|
|
|
|
|
202 |
|
203 |
-
if faceCount2 == 1:
|
204 |
landmark_68 = []
|
205 |
for j in range(68):
|
206 |
-
landmark_68.append({"x": faceBoxes2[
|
|
|
207 |
|
208 |
-
|
209 |
-
"yaw": faceBoxes2[
|
210 |
-
"face_quality": faceBoxes2[
|
211 |
-
"left_eye_closed": faceBoxes2[
|
212 |
-
"face_occlusion": faceBoxes2[
|
213 |
"landmark_68": landmark_68}
|
|
|
|
|
214 |
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
221 |
if __name__ == '__main__':
|
222 |
port = int(os.environ.get("PORT", 8080))
|
|
|
16 |
from facesdk import similarityCalculation
|
17 |
from facebox import FaceBox
|
18 |
|
19 |
+
maxFaceCount = 8
|
|
|
|
|
20 |
|
21 |
licensePath = "license.txt"
|
22 |
license = ""
|
|
|
50 |
|
51 |
@app.route('/compare_face', methods=['POST'])
|
52 |
def compare_face():
|
|
|
|
|
|
|
|
|
|
|
53 |
file1 = request.files['file1']
|
54 |
file2 = request.files['file2']
|
55 |
|
|
|
57 |
image1 = Image.open(file1).convert('RGB')
|
58 |
except:
|
59 |
result = "Failed to open file1"
|
60 |
+
response = jsonify({"resultCode": result})
|
61 |
|
62 |
response.status_code = 200
|
63 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
68 |
image2 = Image.open(file2).convert('RGB')
|
69 |
except:
|
70 |
result = "Failed to open file2"
|
71 |
+
response = jsonify({"resultCode": result})
|
72 |
|
73 |
response.status_code = 200
|
74 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
83 |
faceBoxes2 = (FaceBox * maxFaceCount)()
|
84 |
faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
|
85 |
|
86 |
+
faces1_result = []
|
87 |
+
faces2_result = []
|
88 |
+
for i in range(faceCount1):
|
89 |
+
templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[i])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
|
|
91 |
landmark_68 = []
|
92 |
for j in range(68):
|
93 |
+
landmark_68.append({"x": faceBoxes1[i].landmark_68[j * 2], "y": faceBoxes1[i].landmark_68[j * 2 + 1]})
|
94 |
|
95 |
+
face = {"x1": faceBoxes1[i].x1, "y1": faceBoxes1[i].y1, "x2": faceBoxes1[i].x2, "y2": faceBoxes1[i].y2,
|
96 |
+
"yaw": faceBoxes1[i].yaw, "roll": faceBoxes1[i].roll, "pitch": faceBoxes1[i].pitch,
|
97 |
+
"face_quality": faceBoxes1[i].face_quality, "face_luminance": faceBoxes1[i].face_luminance, "eye_dist": faceBoxes1[i].eye_dist,
|
98 |
+
"left_eye_closed": faceBoxes1[i].left_eye_closed, "right_eye_closed": faceBoxes1[i].right_eye_closed,
|
99 |
+
"face_occlusion": faceBoxes1[i].face_occlusion, "mouth_opened": faceBoxes1[i].mouth_opened,
|
100 |
"landmark_68": landmark_68}
|
101 |
+
|
102 |
+
faces1_result.append(face)
|
103 |
+
|
104 |
+
for i in range(faceCount2):
|
105 |
+
templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[i])
|
106 |
|
|
|
107 |
landmark_68 = []
|
108 |
for j in range(68):
|
109 |
+
landmark_68.append({"x": faceBoxes2[i].landmark_68[j * 2], "y": faceBoxes2[i].landmark_68[j * 2 + 1]})
|
110 |
+
|
111 |
|
112 |
+
face = {"x1": faceBoxes2[i].x1, "y1": faceBoxes2[i].y1, "x2": faceBoxes2[i].x2, "y2": faceBoxes2[i].y2,
|
113 |
+
"yaw": faceBoxes2[i].yaw, "roll": faceBoxes2[i].roll, "pitch": faceBoxes2[i].pitch,
|
114 |
+
"face_quality": faceBoxes2[i].face_quality, "face_luminance": faceBoxes2[i].face_luminance, "eye_dist": faceBoxes2[i].eye_dist,
|
115 |
+
"left_eye_closed": faceBoxes2[i].left_eye_closed, "right_eye_closed": faceBoxes2[i].right_eye_closed,
|
116 |
+
"face_occlusion": faceBoxes2[i].face_occlusion, "mouth_opened": faceBoxes2[i].mouth_opened,
|
117 |
"landmark_68": landmark_68}
|
118 |
+
|
119 |
+
faces2_result.append(face)
|
120 |
+
|
121 |
+
|
122 |
+
if faceCount1 > 0 and faceCount2 > 0:
|
123 |
+
results = []
|
124 |
+
for i in range(faceCount1):
|
125 |
+
for j in range(faceCount2):
|
126 |
+
similarity = similarityCalculation(faceBoxes1[i].templates, faceBoxes2[j].templates)
|
127 |
+
match_result = {"face1": i, "face2": j, "similarity": similarity}
|
128 |
+
results.append(match_result)
|
129 |
+
|
130 |
+
response = jsonify({"resultCode": "Ok", "faces1": faces1_result, "faces2": faces2_result, "results": results})
|
131 |
+
|
132 |
+
response.status_code = 200
|
133 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
134 |
+
return response
|
135 |
+
elif faceCount1 == 0:
|
136 |
+
response = jsonify({"resultCode": "No face1", "faces1": faces1_result, "faces2": faces2_result})
|
137 |
|
138 |
+
response.status_code = 200
|
139 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
140 |
+
return response
|
141 |
+
elif faceCount2 == 0:
|
142 |
+
response = jsonify({"resultCode": "No face2", "faces1": faces1_result, "faces2": faces2_result})
|
143 |
|
144 |
+
response.status_code = 200
|
145 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
146 |
+
return response
|
147 |
|
148 |
+
|
149 |
@app.route('/compare_face_base64', methods=['POST'])
|
150 |
def compare_face_base64():
|
|
|
|
|
|
|
|
|
|
|
151 |
content = request.get_json()
|
152 |
|
153 |
try:
|
|
|
156 |
image1 = Image.open(io.BytesIO(image_data1)).convert('RGB')
|
157 |
except:
|
158 |
result = "Failed to open file1"
|
159 |
+
response = jsonify({"resultCode": result})
|
160 |
|
161 |
response.status_code = 200
|
162 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
167 |
image_data2 = base64.b64decode(imageBase64_2)
|
168 |
image2 = Image.open(io.BytesIO(image_data2)).convert('RGB')
|
169 |
except IOError as exc:
|
170 |
+
result = "Failed to open file1"
|
171 |
+
response = jsonify({"resultCode": result})
|
172 |
|
173 |
response.status_code = 200
|
174 |
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
183 |
faceBoxes2 = (FaceBox * maxFaceCount)()
|
184 |
faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)
|
185 |
|
186 |
+
faces1_result = []
|
187 |
+
faces2_result = []
|
188 |
+
for i in range(faceCount1):
|
189 |
+
templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[i])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
|
|
191 |
landmark_68 = []
|
192 |
for j in range(68):
|
193 |
+
landmark_68.append({"x": faceBoxes1[i].landmark_68[j * 2], "y": faceBoxes1[i].landmark_68[j * 2 + 1]})
|
194 |
|
195 |
+
face = {"x1": faceBoxes1[i].x1, "y1": faceBoxes1[i].y1, "x2": faceBoxes1[i].x2, "y2": faceBoxes1[i].y2,
|
196 |
+
"yaw": faceBoxes1[i].yaw, "roll": faceBoxes1[i].roll, "pitch": faceBoxes1[i].pitch,
|
197 |
+
"face_quality": faceBoxes1[i].face_quality, "face_luminance": faceBoxes1[i].face_luminance, "eye_dist": faceBoxes1[i].eye_dist,
|
198 |
+
"left_eye_closed": faceBoxes1[i].left_eye_closed, "right_eye_closed": faceBoxes1[i].right_eye_closed,
|
199 |
+
"face_occlusion": faceBoxes1[i].face_occlusion, "mouth_opened": faceBoxes1[i].mouth_opened,
|
200 |
"landmark_68": landmark_68}
|
201 |
+
|
202 |
+
faces1_result.append(face)
|
203 |
+
|
204 |
+
for i in range(faceCount2):
|
205 |
+
templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[i])
|
206 |
|
|
|
207 |
landmark_68 = []
|
208 |
for j in range(68):
|
209 |
+
landmark_68.append({"x": faceBoxes2[i].landmark_68[j * 2], "y": faceBoxes2[i].landmark_68[j * 2 + 1]})
|
210 |
+
|
211 |
|
212 |
+
face = {"x1": faceBoxes2[i].x1, "y1": faceBoxes2[i].y1, "x2": faceBoxes2[i].x2, "y2": faceBoxes2[i].y2,
|
213 |
+
"yaw": faceBoxes2[i].yaw, "roll": faceBoxes2[i].roll, "pitch": faceBoxes2[i].pitch,
|
214 |
+
"face_quality": faceBoxes2[i].face_quality, "face_luminance": faceBoxes2[i].face_luminance, "eye_dist": faceBoxes2[i].eye_dist,
|
215 |
+
"left_eye_closed": faceBoxes2[i].left_eye_closed, "right_eye_closed": faceBoxes2[i].right_eye_closed,
|
216 |
+
"face_occlusion": faceBoxes2[i].face_occlusion, "mouth_opened": faceBoxes2[i].mouth_opened,
|
217 |
"landmark_68": landmark_68}
|
218 |
+
|
219 |
+
faces2_result.append(face)
|
220 |
|
221 |
+
|
222 |
+
if faceCount1 > 0 and faceCount2 > 0:
|
223 |
+
results = []
|
224 |
+
for i in range(faceCount1):
|
225 |
+
for j in range(faceCount2):
|
226 |
+
similarity = similarityCalculation(faceBoxes1[i].templates, faceBoxes2[j].templates)
|
227 |
+
match_result = {"face1": i, "face2": j, "similarity": similarity}
|
228 |
+
results.append(match_result)
|
229 |
+
|
230 |
+
response = jsonify({"resultCode": "Ok", "faces1": faces1_result, "faces2": faces2_result, "results": results})
|
231 |
|
232 |
+
response.status_code = 200
|
233 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
234 |
+
return response
|
235 |
+
elif faceCount1 == 0:
|
236 |
+
response = jsonify({"resultCode": "No face1", "faces1": faces1_result, "faces2": faces2_result})
|
237 |
+
|
238 |
+
response.status_code = 200
|
239 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
240 |
+
return response
|
241 |
+
elif faceCount2 == 0:
|
242 |
+
response = jsonify({"resultCode": "No face2", "faces1": faces1_result, "faces2": faces2_result})
|
243 |
+
|
244 |
+
response.status_code = 200
|
245 |
+
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
246 |
+
return response
|
247 |
|
248 |
if __name__ == '__main__':
|
249 |
port = int(os.environ.get("PORT", 8080))
|
facesdk.py
CHANGED
@@ -4,29 +4,29 @@ from ctypes import *
|
|
4 |
from numpy.ctypeslib import ndpointer
|
5 |
from facebox import FaceBox
|
6 |
|
7 |
-
libPath = os.path.abspath(os.path.dirname(__file__)) + '/
|
8 |
facesdk = cdll.LoadLibrary(libPath)
|
9 |
|
10 |
-
getMachineCode = facesdk.
|
11 |
getMachineCode.argtypes = []
|
12 |
getMachineCode.restype = c_char_p
|
13 |
|
14 |
-
setActivation = facesdk.
|
15 |
setActivation.argtypes = [c_char_p]
|
16 |
setActivation.restype = c_int32
|
17 |
|
18 |
-
initSDK = facesdk.
|
19 |
initSDK.argtypes = [c_char_p]
|
20 |
initSDK.restype = c_int32
|
21 |
|
22 |
-
faceDetection = facesdk.
|
23 |
faceDetection.argtypes = [ndpointer(c_ubyte, flags='C_CONTIGUOUS'), c_int32, c_int32, POINTER(FaceBox), c_int32]
|
24 |
faceDetection.restype = c_int32
|
25 |
|
26 |
-
templateExtraction = facesdk.
|
27 |
templateExtraction.argtypes = [ndpointer(c_ubyte, flags='C_CONTIGUOUS'), c_int32, c_int32, POINTER(FaceBox)]
|
28 |
templateExtraction.restype = c_int32
|
29 |
|
30 |
-
similarityCalculation = facesdk.
|
31 |
similarityCalculation.argtypes = [c_ubyte * 2048, c_ubyte * 2048]
|
32 |
similarityCalculation.restype = c_float
|
|
|
4 |
from numpy.ctypeslib import ndpointer
|
5 |
from facebox import FaceBox
|
6 |
|
7 |
+
libPath = os.path.abspath(os.path.dirname(__file__)) + '/libkbyai_facesdk2.so'
|
8 |
facesdk = cdll.LoadLibrary(libPath)
|
9 |
|
10 |
+
getMachineCode = facesdk.kbyai_getMachineCode
|
11 |
getMachineCode.argtypes = []
|
12 |
getMachineCode.restype = c_char_p
|
13 |
|
14 |
+
setActivation = facesdk.kbyai_setActivation
|
15 |
setActivation.argtypes = [c_char_p]
|
16 |
setActivation.restype = c_int32
|
17 |
|
18 |
+
initSDK = facesdk.kbyai_initSDK
|
19 |
initSDK.argtypes = [c_char_p]
|
20 |
initSDK.restype = c_int32
|
21 |
|
22 |
+
faceDetection = facesdk.kbyai_faceDetection
|
23 |
faceDetection.argtypes = [ndpointer(c_ubyte, flags='C_CONTIGUOUS'), c_int32, c_int32, POINTER(FaceBox), c_int32]
|
24 |
faceDetection.restype = c_int32
|
25 |
|
26 |
+
templateExtraction = facesdk.kbyai_templateExtraction
|
27 |
templateExtraction.argtypes = [ndpointer(c_ubyte, flags='C_CONTIGUOUS'), c_int32, c_int32, POINTER(FaceBox)]
|
28 |
templateExtraction.restype = c_int32
|
29 |
|
30 |
+
similarityCalculation = facesdk.kbyai_similarityCalculation
|
31 |
similarityCalculation.argtypes = [c_ubyte * 2048, c_ubyte * 2048]
|
32 |
similarityCalculation.restype = c_float
|
libkbyai_facesdk2.so
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:44639279d342cb505feebaf9d2180ffe92d8dfe1886cea510e021dcee222e49e
|
3 |
+
size 4942024
|