Turing311 commited on
Commit
3996ddb
1 Parent(s): 2bb83e6

Upload 10 files

Browse files
.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
+ facewrapper/libs/libttvfaceengine6.so filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:20.04
2
+ RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone
3
+ RUN apt-get update -y
4
+ RUN apt-get install -y python3 python3-pip python3-opencv
5
+ RUN apt-get install -y libcurl4-openssl-dev libssl-dev
6
+ RUN mkdir -p /root/FaceOnLive_v6
7
+ RUN mkdir -p /root/FaceOnLive_v6/facewrapper
8
+ WORKDIR /root/FaceOnLive_v6
9
+ COPY ./facewrapper ./facewrapper
10
+ COPY ./facewrapper/libs/libimutils.so /usr/lib
11
+ COPY ./facewrapper/libs/openvino /usr/lib
12
+ COPY ./app.py ./app.py
13
+ COPY ./requirements.txt ./requirements.txt
14
+ RUN pip3 install -r requirements.txt
15
+ CMD [ "python3", "app.py"]
16
+ EXPOSE 8000
app.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ sys.path.append('.')
3
+
4
+ from flask import Flask, request, jsonify
5
+ from time import gmtime, strftime
6
+ import os
7
+ import base64
8
+ import json
9
+ import cv2
10
+ import numpy as np
11
+
12
+ from facewrapper.facewrapper import ttv_version
13
+ from facewrapper.facewrapper import ttv_get_hwid
14
+ from facewrapper.facewrapper import ttv_init
15
+ from facewrapper.facewrapper import ttv_init_offline
16
+ from facewrapper.facewrapper import ttv_extract_feature
17
+ from facewrapper.facewrapper import ttv_compare_feature
18
+
19
+ app = Flask(__name__)
20
+
21
+ app.config['SITE'] = "http://0.0.0.0:8000/"
22
+ app.config['DEBUG'] = False
23
+
24
+ licenseKey = "IBHTS-SIGHK-VVGCB-MGJYC"
25
+ licensePath = "license.txt"
26
+ modelFolder = os.path.abspath(os.path.dirname(__file__)) + '/facewrapper/dict'
27
+
28
+ version = ttv_version()
29
+ print("version: ", version.decode('utf-8'))
30
+
31
+ ret = ttv_init(modelFolder.encode('utf-8'), licenseKey.encode('utf-8'))
32
+ if ret != 0:
33
+ print(f"online init failed: {ret}");
34
+
35
+ hwid = ttv_get_hwid()
36
+ print("hwid: ", hwid.decode('utf-8'))
37
+
38
+ ret = ttv_init_offline(modelFolder.encode('utf-8'), licensePath.encode('utf-8'))
39
+ if ret != 0:
40
+ print(f"offline init failed: {ret}")
41
+ exit(-1)
42
+ else:
43
+ print(f"offline init ok")
44
+
45
+ else:
46
+ print(f"online init ok")
47
+
48
+ @app.route('/api/compare_face', methods=['POST'])
49
+ def compare_face():
50
+ file1 = request.files['image1']
51
+ image1 = cv2.imdecode(np.fromstring(file1.read(), np.uint8), cv2.IMREAD_COLOR)
52
+ if image1 is None:
53
+ result = "image1: is null!"
54
+ status = "ok"
55
+ response = jsonify({"status": status, "data": {"result": result}})
56
+ response.status_code = 200
57
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
58
+ return response
59
+
60
+ file2 = request.files['image2']
61
+ image2 = cv2.imdecode(np.fromstring(file2.read(), np.uint8), cv2.IMREAD_COLOR)
62
+ if image2 is None:
63
+ result = "image2: is null!"
64
+ status = "ok"
65
+ response = jsonify({"status": status, "data": {"result": result}})
66
+ response.status_code = 200
67
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
68
+ return response
69
+
70
+ faceRect1 = np.zeros([4], dtype=np.int32)
71
+ feature1 = np.zeros([2048], dtype=np.uint8)
72
+ featureSize1 = np.zeros([1], dtype=np.int32)
73
+
74
+ ret = ttv_extract_feature(image1, image1.shape[1], image1.shape[0], faceRect1, feature1, featureSize1)
75
+ if ret <= 0:
76
+ if ret == -1:
77
+ result = "license error!"
78
+ elif ret == -2:
79
+ result = "init error!"
80
+ elif ret == 0:
81
+ result = "image1: no face detected!"
82
+
83
+ status = "ok"
84
+ response = jsonify({"status": status, "data": {"result": result}})
85
+ response.status_code = 200
86
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
87
+ return response
88
+
89
+ faceRect2 = np.zeros([4], dtype=np.int32)
90
+ feature2 = np.zeros([2048], dtype=np.uint8)
91
+ featureSize2 = np.zeros([1], dtype=np.int32)
92
+
93
+ ret = ttv_extract_feature(image2, image2.shape[1], image2.shape[0], faceRect2, feature2, featureSize2)
94
+ if ret <= 0:
95
+ if ret == -1:
96
+ result = "license error!"
97
+ elif ret == -2:
98
+ result = "init error!"
99
+ elif ret == 0:
100
+ result = "image2: no face detected!"
101
+
102
+ status = "ok"
103
+ response = jsonify({"status": status, "data": {"result": result}})
104
+ response.status_code = 200
105
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
106
+ return response
107
+
108
+ similarity = ttv_compare_feature(feature1, feature2)
109
+ if similarity > 0.67:
110
+ result = "same person!"
111
+ else:
112
+ result = "different person!"
113
+
114
+ status = "ok"
115
+ response = jsonify({"status": status, "data": {"result": result, "similarity": float(similarity)}})
116
+ response.status_code = 200
117
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
118
+ return response
119
+
120
+
121
+ @app.route('/api/compare_face_base64', methods=['POST'])
122
+ def coompare_face_base64():
123
+ content = request.get_json()
124
+ imageBase641 = content['image1']
125
+ image1 = cv2.imdecode(np.frombuffer(base64.b64decode(imageBase641), dtype=np.uint8), cv2.IMREAD_COLOR)
126
+
127
+ if image1 is None:
128
+ result = "image1: is null!"
129
+ status = "ok"
130
+ response = jsonify({"status": status, "data": {"result": result}})
131
+ response.status_code = 200
132
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
133
+ return response
134
+
135
+ imageBase642 = content['image2']
136
+ image2 = cv2.imdecode(np.frombuffer(base64.b64decode(imageBase642), dtype=np.uint8), cv2.IMREAD_COLOR)
137
+
138
+ if image2 is None:
139
+ result = "image2: is null!"
140
+ status = "ok"
141
+ response = jsonify({"status": status, "data": {"result": result}})
142
+ response.status_code = 200
143
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
144
+ return response
145
+
146
+ faceRect1 = np.zeros([4], dtype=np.int32)
147
+ feature1 = np.zeros([2048], dtype=np.uint8)
148
+ featureSize1 = np.zeros([1], dtype=np.int32)
149
+
150
+ ret = ttv_extract_feature(image1, image1.shape[1], image1.shape[0], faceRect1, feature1, featureSize1)
151
+ if ret <= 0:
152
+ if ret == -1:
153
+ result = "license error!"
154
+ elif ret == -2:
155
+ result = "init error!"
156
+ elif ret == 0:
157
+ result = "image1: no face detected!"
158
+
159
+ status = "ok"
160
+ response = jsonify({"status": status, "data": {"result": result}})
161
+ response.status_code = 200
162
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
163
+ return response
164
+
165
+ faceRect2 = np.zeros([4], dtype=np.int32)
166
+ feature2 = np.zeros([2048], dtype=np.uint8)
167
+ featureSize2 = np.zeros([1], dtype=np.int32)
168
+
169
+ ret = ttv_extract_feature(image2, image2.shape[1], image2.shape[0], faceRect2, feature2, featureSize2)
170
+ if ret <= 0:
171
+ if ret == -1:
172
+ result = "license error!"
173
+ elif ret == -2:
174
+ result = "init error!"
175
+ elif ret == 0:
176
+ result = "image2: no face detected!"
177
+
178
+ status = "ok"
179
+ response = jsonify({"status": status, "data": {"result": result}})
180
+ response.status_code = 200
181
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
182
+ return response
183
+
184
+ similarity = ttv_compare_feature(feature1, feature2)
185
+ if similarity > 0.67:
186
+ result = "same person!"
187
+ else:
188
+ result = "different person!"
189
+
190
+ status = "ok"
191
+ response = jsonify({"status": status, "data": {"result": result, "similarity": float(similarity)}})
192
+ response.status_code = 200
193
+ response.headers["Content-Type"] = "application/json; charset=utf-8"
194
+ return response
195
+
196
+ if __name__ == '__main__':
197
+ port = int(os.environ.get("PORT", 8000))
198
+ app.run(host='0.0.0.0', port=port)
facewrapper/dict/data1.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36cf5fcc49345989a86839a53529314ec1fe5d621c377a1952bc7538d55e7f1b
3
+ size 16255630
facewrapper/dict/data2.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f25fb0cd3d70cb84c258e7109620f411c087e0875828d6ab86cc9c4838d49bec
3
+ size 11875339
facewrapper/dict/data3.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:06daf36a8545f59ac104415e8b2d38072d06abc027cb346fd4a6c6029fed55b4
3
+ size 90716260
facewrapper/facewrapper.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes, ctypes.util
2
+ from ctypes import *
3
+ from numpy.ctypeslib import ndpointer
4
+ import sys
5
+ import os
6
+
7
+ lib_path = os.path.abspath(os.path.dirname(__file__)) + '/libs/libttvfaceengine6.so'
8
+ liveness_engine = cdll.LoadLibrary(lib_path)
9
+
10
+ ttv_version = liveness_engine.ttv_version
11
+ ttv_version.argtypes = []
12
+ ttv_version.restype = ctypes.c_char_p
13
+
14
+ ttv_get_hwid = liveness_engine.ttv_get_hwid
15
+ ttv_get_hwid.argtypes = []
16
+ ttv_get_hwid.restype = ctypes.c_char_p
17
+
18
+ ttv_init = liveness_engine.ttv_init
19
+ ttv_init.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
20
+ ttv_init.restype = ctypes.c_int32
21
+
22
+ ttv_init_offline = liveness_engine.ttv_init_offline
23
+ ttv_init_offline.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
24
+ ttv_init_offline.restype = ctypes.c_int32
25
+
26
+ ttv_extract_feature = liveness_engine.ttv_extract_feature
27
+ ttv_extract_feature.argtypes = [ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ctypes.c_int32, ctypes.c_int32, ndpointer(ctypes.c_int32, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_int32, flags='C_CONTIGUOUS')]
28
+ ttv_extract_feature.restype = ctypes.c_int
29
+
30
+ ttv_compare_feature = liveness_engine.ttv_compare_feature
31
+ ttv_compare_feature.argtypes = [ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS')]
32
+ ttv_compare_feature.restype = ctypes.c_double
facewrapper/libs/libimutils.so ADDED
Binary file (412 kB). View file
 
facewrapper/libs/libimutils.so_for_ubuntu22 ADDED
Binary file (412 kB). View file
 
facewrapper/libs/libttvfaceengine6.so ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:47263ed3dae141d4640141d2a91c6ad72934771ff7101ff916f26df62c714952
3
+ size 1233040
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ opencv-python
2
+ flask
3
+ flask-cors