Face Recognition, Face Liveness Detection, Face Anti-Spoofing, Face Detection, Face Landmarks, Face Compare, Face Matching, Face Pose, Face Expression, Face Attributes, Face Templates Extraction, Face Landmarks
Python deep-learningface-detectionface-landmark-detectionface-recognitionidentity-verificationmachine-learningopen-sourcepython
add missing dependencies
andyfaceplugin8c47081e790c
diff --git a/face_feature/irn50_pytorch.npy b/face_feature/irn50_pytorch.npynew file mode 100644index 0000000..7475468Binary files /dev/null and b/face_feature/irn50_pytorch.npy differdiff --git a/face_landmark/GetLandmark.py b/face_landmark/GetLandmark.pynew file mode 100644index 0000000..16fc7c0--- /dev/null+++ b/face_landmark/GetLandmark.py@@ -0,0 +1,62 @@++import cv2+import numpy as np+import torch+from face_landmark.MobileFaceNet import MobileFaceNet++model_landmark = MobileFaceNet(input_size=64, embedding_size=136)+model_landmark.load_state_dict(torch.load("./face_landmark/vfl_1.02_578_6.734591484069824.pth.tar", map_location=torch.device('cpu'))['state_dict'])+model_landmark.eval()++def get_face_landmark(gray_img, bounding_box):+ image = gray_img+ box = bounding_box++ nHeight, nWidth = image.shape++ rLeftMargin = 0.05+ rTopMargin = 0.00+ rRightMargin = 0.05+ rBottomMargin = 0.10++ rW = box[2] - box[0]+ rH = box[3] - box[1]+ cx = (box[0] + box[2]) / 2+ cy = (box[1] + box[3]) / 2+ sz = pow(rW * rH, 0.5)+ rX = cx - sz / 2+ rY = cy - sz / 2+ rW = sz+ rH = sz++ #get image range to get face landmark from face rect+ iExFaceX = int(rX - rLeftMargin * rW)+ iExFaceY = int(rY - rTopMargin * rH)+ iExFaceW = int((1 + (rLeftMargin + rRightMargin)) * rW)+ iExFaceH = int((1 + (rTopMargin + rBottomMargin)) * rH)++ iExFaceX = np.clip(iExFaceX, 0, nWidth - 1)+ iExFaceY = np.clip(iExFaceY, 0, nHeight - 1)+ iExFaceW = np.clip(iExFaceX + iExFaceW, 0, nWidth - 1) - iExFaceX+ iExFaceH = np.clip(iExFaceY + iExFaceH, 0, nHeight - 1) - iExFaceY++ #crop face image in range to face landmark+ image = image[iExFaceY:iExFaceY+iExFaceH, iExFaceX:iExFaceX+iExFaceW]+ #normalize croped face image+ image = cv2.resize(image, (64, 64), cv2.INTER_LINEAR)+ # cv2.imwrite("D:/crop.png", image)+ image = image / 256+ image = torch.from_numpy(image.astype(np.float32))+ #convert mask_align_image from type [n,n] to [1,1,n,n]+ image = image.unsqueeze(0).unsqueeze(0)++ #get landmark fron croped face image+ landmark = model_landmark(image)+ #reshape face landmark and convert to image coordinates+ landmark = landmark.reshape(68, 2)+ landmark[:,0] = landmark[:,0] * iExFaceW + iExFaceX+ landmark[:,1] = landmark[:,1] * iExFaceH + iExFaceY++ landmark = landmark.reshape(-1)++ return landmark\ No newline at end of filediff --git a/face_landmark/vfl_1.02_578_6.734591484069824.pth.tar b/face_landmark/vfl_1.02_578_6.734591484069824.pth.tarnew file mode 100644index 0000000..02e1953Binary files /dev/null and b/face_landmark/vfl_1.02_578_6.734591484069824.pth.tar differdiff --git a/face_pose/GetPose.py b/face_pose/GetPose.pynew file mode 100644index 0000000..b8c2ee3--- /dev/null+++ b/face_pose/GetPose.py@@ -0,0 +1,21 @@+import numpy as np+from numpy.ctypeslib import as_ctypes_type+from face_util.faceutil import getPose68+import ctypes++def get_face_pose(box, landmark):+ fw = box[2] - box[0]+ fh = box[3] - box[1]+ sz = pow(fw * fh, 0.5)+ cx = (box[2] + box[0]) / 2+ cy = (box[3] + box[1]) / 2+ x1 = cx - sz / 2+ y1 = cy - sz / 2++ rx = ctypes.c_float(0)+ ry = ctypes.c_float(0)+ rz = ctypes.c_float(0)++ landmark_vec = (ctypes.c_float * len(landmark))(*landmark)+ getPose68(x1, y1, sz, landmark_vec, 68, ctypes.pointer(rx), ctypes.pointer(ry), ctypes.pointer(rz))+ return np.array([rx.value, ry.value, rz.value], dtype=np.float32)diff --git a/face_util/C/FaceUtil.dll b/face_util/C/FaceUtil.dllnew file mode 100644index 0000000..1b164ccBinary files /dev/null and b/face_util/C/FaceUtil.dll differdiff --git a/face_util/C/libFaceUtil.so b/face_util/C/libFaceUtil.sonew file mode 100644index 0000000..f9cbbafBinary files /dev/null and b/face_util/C/libFaceUtil.so differdiff --git a/face_util/faceutil.py b/face_util/faceutil.pynew file mode 100644index 0000000..c75bf8f--- /dev/null+++ b/face_util/faceutil.py@@ -0,0 +1,27 @@+import ctypes, ctypes.util+from ctypes import cdll+from numpy.ctypeslib import ndpointer+import sys+import os++if sys.platform.startswith('linux'):+ dll_path = os.path.abspath(os.path.dirname(__file__)) + '/C/libFaceUtil.so'+ face_util_engine = cdll.LoadLibrary(dll_path)+elif sys.platform.startswith('win32'):+ dll_path = os.path.abspath(os.path.dirname(__file__)) + '/C/FaceUtil.dll'+ face_util_engine = ctypes.CDLL(dll_path)+else:+ print("Unable to find the appropriate OS version.")+ sys.exit()++align_vertical = face_util_engine.align_vertical+align_vertical.argtypes = [ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ctypes.c_int32, ctypes.c_int32, ndpointer(ctypes.c_ubyte, flags='C_CONTIGUOUS'), ctypes.c_int32, ctypes.c_int32, ctypes.c_int32, ctypes.POINTER(ctypes.c_float), ctypes.c_float, ctypes.c_float, ctypes.c_float]+align_vertical.restype = ctypes.c_int32++getPose = face_util_engine.getPose+getPose.argtypes = [ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.POINTER(ctypes.c_float), ctypes.c_int32, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float)]+getPose.restype = ctypes.c_int32++getPose68 = face_util_engine.getPose68+getPose68.argtypes = [ctypes.c_float, ctypes.c_float, ctypes.c_float, ctypes.POINTER(ctypes.c_float), ctypes.c_int32, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float)]+getPose68.restype = ctypes.c_int32diff --git a/requirements.txt b/requirements.txtindex f8e67a9..ead27d2 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,5 +1,5 @@numpy==1.26.4-opencv-python==3.4.13.47+opencv-pythonpillow==10.4.0torch==2.4.1torchaudio==2.4.1