hassan526/Face_Recognition_API
0
1import os
2import sys
3import numpy as np
4import ctypes, ctypes.util
5from enum import Enum
6from ctypes import *
7from numpy.ctypeslib import ndpointer
8
9def print_log(fmt): print("[LOG] \033[98m{}\033[00m" .format(fmt))
10def print_info(fmt): print("[INFO] \033[92m{}\033[00m" .format(fmt))
11def print_error(fmt): print("[ERR] \033[91m{}\033[00m" .format(fmt))
12def print_warning(fmt): print("[WARNING] \033[93m{}\033[00m" .format(fmt))
13
14class ENGINE_CODE(Enum):
15 E_NO_FACE = 0
16 E_ACTIVATION_ERROR = -1
17 E_ENGINE_INIT_ERROR = -2
18
19lib_path = os.path.abspath(os.path.dirname(__file__)) + '/librecognition_v6.so'
20lib = cdll.LoadLibrary(lib_path)
21
22get_version = lib.ttv_version
23get_version.argtypes = []
24get_version.restype = ctypes.c_char_p
25
26get_deviceid = lib.ttv_get_hwid
27get_deviceid.argtypes = []
28get_deviceid.restype = ctypes.c_char_p
29
30init_sdk = lib.ttv_init
31init_sdk.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
32init_sdk.restype = ctypes.c_int32
33
34init_sdk_offline = lib.ttv_init_offline
35init_sdk_offline.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
36init_sdk_offline.restype = ctypes.c_int32
37
38extract_template = lib.ttv_extract_feature
39extract_template.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')]
40extract_template.restype = ctypes.c_int
41
42calculate_similarity = lib.ttv_compare_feature
43calculate_similarity.argtypes = [ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS')]
44calculate_similarity.restype = ctypes.c_double
45
46DEFAULT_THRESHOLD = 0.67
47def compare_face(image_mat1, image_mat2, match_threshold=DEFAULT_THRESHOLD):
48 result = ""
49 if image_mat1 is None:
50 result = "Failed to open image1"
51 return result, None, None, None
52
53 if image_mat2 is None:
54 result = "Failed to open image2"
55 return result, None, None, None
56
57 face_bbox_1 = np.zeros([4], dtype=np.int32)
58 template_1 = np.zeros([2048], dtype=np.uint8)
59 template_len_1 = np.zeros([1], dtype=np.int32)
60 width_1 = image_mat1.shape[1]
61 height_1 = image_mat1.shape[0]
62
63 ret = extract_template(image_mat1, width_1, height_1, face_bbox_1, template_1, template_len_1)
64 if ret <= 0:
65 if ret == ENGINE_CODE.E_ACTIVATION_ERROR.value:
66 result = "ACTIVATION ERROR"
67 elif ret == ENGINE_CODE.E_ENGINE_INIT_ERROR.value:
68 result = "ENGINE INIT ERROR"
69 elif ret == ENGINE_CODE.E_NO_FACE.value:
70 result = "NO FACE in image1"
71 return result, None, None, None
72
73
74 face_bbox_2 = np.zeros([4], dtype=np.int32)
75 template_2 = np.zeros([2048], dtype=np.uint8)
76 template_len_2 = np.zeros([1], dtype=np.int32)
77 width_2 = image_mat2.shape[1]
78 height_2 = image_mat2.shape[0]
79
80 ret = extract_template(image_mat2, width_2, height_2, face_bbox_2, template_2, template_len_2)
81 if ret <= 0:
82 if ret == ENGINE_CODE.E_ACTIVATION_ERROR.value:
83 result = "ACTIVATION ERROR"
84 elif ret == ENGINE_CODE.E_ENGINE_INIT_ERROR.value:
85 result = "ENGINE INIT ERROR"
86 elif ret == ENGINE_CODE.E_NO_FACE.value:
87 result = "NO FACE in image2"
88 return result, None, None, None
89
90 match_score = calculate_similarity(template_1, template_2)
91 if match_score > match_threshold:
92 result = "SAME PERSON"
93 else:
94 result = "DIFFERENT PERSON"
95
96 return result, match_score, [face_bbox_1, face_bbox_2], [template_1, template_2]
97 