hassan526 commited on
Commit
ca7a857
·
verified ·
1 Parent(s): 314eec3

Update fl/engine/header.py

Browse files
Files changed (1) hide show
  1. fl/engine/header.py +96 -96
fl/engine/header.py CHANGED
@@ -1,96 +1,96 @@
1
- import os
2
- import sys
3
- import numpy as np
4
- import ctypes, ctypes.util
5
- from enum import Enum
6
- from ctypes import *
7
- from numpy.ctypeslib import ndpointer
8
-
9
- def print_log(fmt): print("[LOG] \033[98m{}\033[00m" .format(fmt))
10
- def print_info(fmt): print("[INFO] \033[92m{}\033[00m" .format(fmt))
11
- def print_error(fmt): print("[ERR] \033[91m{}\033[00m" .format(fmt))
12
- def print_warning(fmt): print("[WARNING] \033[93m{}\033[00m" .format(fmt))
13
-
14
- class ENGINE_CODE(Enum):
15
- E_NO_FACE = 0
16
- E_ACTIVATION_ERROR = -1
17
- E_ENGINE_INIT_ERROR = -2
18
-
19
- class LIVENESS_CODE(Enum):
20
- L_TOO_SMALL_FACE = -100
21
- L_BORDERLINE_FACE = -200
22
- L_TOO_TURNED_FACE = -300
23
- L_COVERED_FACE = -400
24
- L_MULTIPLE_FACE = -500
25
- L_DEEP_FAKE = -600
26
-
27
- lib_path = os.path.abspath(os.path.dirname(__file__)) + '/libliveness_v7.so'
28
- lib = cdll.LoadLibrary(lib_path)
29
-
30
- get_version = lib.ttv_version
31
- get_version.argtypes = []
32
- get_version.restype = ctypes.c_char_p
33
-
34
- get_deviceid = lib.ttv_get_hwid
35
- get_deviceid.argtypes = []
36
- get_deviceid.restype = ctypes.c_char_p
37
-
38
- init_sdk = lib.ttv_init
39
- init_sdk.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
40
- init_sdk.restype = ctypes.c_int32
41
-
42
- init_sdk_offline = lib.ttv_init_offline
43
- init_sdk_offline.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
44
- init_sdk_offline.restype = ctypes.c_int32
45
-
46
-
47
- detect_face_rgb = lib.ttv_detect_face
48
- detect_face_rgb.argtypes = [ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ctypes.c_int32, ctypes.c_int32, ndpointer(ctypes.c_int32, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_double, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_double, flags='C_CONTIGUOUS')]
49
- detect_face_rgb.restype = ctypes.c_int32
50
-
51
- DEFAULT_THRESHOLD = 0
52
- def check_liveness(image_mat, spoof_threshold = DEFAULT_THRESHOLD):
53
- result = ""
54
- score = 0
55
-
56
- if image_mat is None:
57
- result = "Failed to open image"
58
- return result, None, None, None
59
-
60
- face_rect = np.zeros([4], dtype=np.int32)
61
- liveness_score = np.zeros([1], dtype=np.double)
62
- angles = np.zeros([3], dtype=np.double)
63
-
64
- width = image_mat.shape[1]
65
- height = image_mat.shape[0]
66
-
67
- ret = detect_face_rgb(image_mat, width, height, face_rect, liveness_score, angles)
68
-
69
- if ret <= 0:
70
- if ret == ENGINE_CODE.E_ACTIVATION_ERROR.value:
71
- result = "ACTIVATION ERROR"
72
- elif ret == ENGINE_CODE.E_ENGINE_INIT_ERROR.value:
73
- result = "ENGINE INIT ERROR"
74
- elif ret == ENGINE_CODE.E_NO_FACE.value:
75
- result = "NO FACE"
76
- return result, None, None, None
77
-
78
- score = liveness_score[0]
79
- if score == LIVENESS_CODE.L_TOO_SMALL_FACE.value:
80
- result = "TOO SMALL FACE"
81
- elif score == LIVENESS_CODE.L_BORDERLINE_FACE.value:
82
- result = "FACE CUT OFF"
83
- elif score == LIVENESS_CODE.L_TOO_TURNED_FACE.value:
84
- result = "TOO TURNED FACE"
85
- elif score == LIVENESS_CODE.L_COVERED_FACE.value:
86
- result = "COVERED FACE"
87
- elif score == LIVENESS_CODE.L_MULTIPLE_FACE.value:
88
- result = "MULTIPLE FACES"
89
- elif score == LIVENESS_CODE.L_DEEP_FAKE.value:
90
- result = "DEEP FAKE DETECTED"
91
- elif score > spoof_threshold:
92
- result = "REAL"
93
- else:
94
- result = "SPOOF"
95
-
96
- return result, face_rect, score, angles
 
1
+ import os
2
+ import sys
3
+ import numpy as np
4
+ import ctypes, ctypes.util
5
+ from enum import Enum
6
+ from ctypes import *
7
+ from numpy.ctypeslib import ndpointer
8
+
9
+ def print_log(fmt): print("[LOG] \033[98m{}\033[00m" .format(fmt))
10
+ def print_info(fmt): print("[INFO] \033[92m{}\033[00m" .format(fmt))
11
+ def print_error(fmt): print("[ERR] \033[91m{}\033[00m" .format(fmt))
12
+ def print_warning(fmt): print("[WARNING] \033[93m{}\033[00m" .format(fmt))
13
+
14
+ class ENGINE_CODE(Enum):
15
+ E_NO_FACE = 0
16
+ E_ACTIVATION_ERROR = -1
17
+ E_ENGINE_INIT_ERROR = -2
18
+
19
+ class LIVENESS_CODE(Enum):
20
+ L_TOO_SMALL_FACE = -100
21
+ L_BORDERLINE_FACE = -200
22
+ L_TOO_TURNED_FACE = -300
23
+ L_COVERED_FACE = -400
24
+ L_MULTIPLE_FACE = -500
25
+ L_DEEP_FAKE = -600
26
+
27
+ lib_path = os.path.abspath(os.path.dirname(__file__)) + '/libliveness_v7.so'
28
+ lib = cdll.LoadLibrary(lib_path)
29
+
30
+ get_version = lib.ttv_version
31
+ get_version.argtypes = []
32
+ get_version.restype = ctypes.c_char_p
33
+
34
+ get_deviceid = lib.ttv_get_hwid
35
+ get_deviceid.argtypes = []
36
+ get_deviceid.restype = ctypes.c_char_p
37
+
38
+ init_sdk = lib.ttv_init
39
+ init_sdk.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
40
+ init_sdk.restype = ctypes.c_int32
41
+
42
+ init_sdk_offline = lib.ttv_init_offline
43
+ init_sdk_offline.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
44
+ init_sdk_offline.restype = ctypes.c_int32
45
+
46
+
47
+ detect_face_rgb = lib.ttv_detect_face
48
+ detect_face_rgb.argtypes = [ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ctypes.c_int32, ctypes.c_int32, ndpointer(ctypes.c_int32, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_double, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_double, flags='C_CONTIGUOUS')]
49
+ detect_face_rgb.restype = ctypes.c_int32
50
+
51
+ DEFAULT_THRESHOLD = 0.5
52
+ def check_liveness(image_mat, spoof_threshold = DEFAULT_THRESHOLD):
53
+ result = ""
54
+ score = 0
55
+
56
+ if image_mat is None:
57
+ result = "Failed to open image"
58
+ return result, None, None, None
59
+
60
+ face_rect = np.zeros([4], dtype=np.int32)
61
+ liveness_score = np.zeros([5], dtype=np.double) # [0]: liveness score [1]: Quality [2]: Blurness [3]: Occlusion [4]: Eye Distance
62
+ angles = np.zeros([3], dtype=np.double)
63
+
64
+ width = image_mat.shape[1]
65
+ height = image_mat.shape[0]
66
+
67
+ ret = detect_face_rgb(image_mat, width, height, face_rect, liveness_score, angles)
68
+
69
+ if ret == ENGINE_CODE.E_ACTIVATION_ERROR.value:
70
+ result = "ACTIVATION ERROR"
71
+ elif ret == ENGINE_CODE.E_ENGINE_INIT_ERROR.value:
72
+ result = "ENGINE INIT ERROR"
73
+ elif ret == ENGINE_CODE.E_NO_FACE.value:
74
+ result = "NO FACE"
75
+ face_rect = None
76
+ angles = None
77
+ liveness_score[0] = 0
78
+ elif ret > 1:
79
+ result = "MULTIPLE FACES"
80
+ face_rect = None
81
+ angles = None
82
+ liveness_score[0] = 0
83
+ elif liveness_score[4] < 70:
84
+ result = "TOO SMALL FACE"
85
+ elif liveness_score[3] > 0.85:
86
+ result = "COVERED FACE"
87
+ elif face_rect[0] <= 0 or face_rect[1] <= 0 or face_rect[2] >= image_mat.shape[1] or face_rect[3] >= image_mat.shape[0]:
88
+ result = "TOO CLOSE TO BORDERS"
89
+ elif angles[0] > 25 or angles[1] > 25 or angles[2] > 25:
90
+ result = "TOO TURNED FACE"
91
+ elif liveness_score[0] > spoof_threshold:
92
+ result = "REAL"
93
+ else:
94
+ result = "SPOOF"
95
+
96
+ return result, face_rect, liveness_score[0], angles