Skip to content

Commit

Permalink
The initial version has implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
justin200914 committed Jun 12, 2023
1 parent 1e54a7b commit 1aa2e3c
Show file tree
Hide file tree
Showing 37 changed files with 114,394 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

__pycache__/facebox.cpython-38.pyc
__pycache__/facesdk.cpython-38.pyc
*.bin
lib/facesdk1.dll
lib/facesdk2.dll
Binary file added face_examples/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added face_examples/9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions facebox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ctypes import *

class FaceBox(Structure):
_fields_ = [("x1", c_int32), ("y1", c_int32), ("x2", c_int32), ("y2", c_int32),
("yaw", c_float), ("roll", c_float), ("pitch", c_float),
("face_quality", c_float), ("face_luminance", c_float), ("eye_dist", c_float),
("left_eye_closed", c_float), ("right_eye_closed", c_float),
("face_occlusion", c_float), ("mouth_opened", c_float),
("landmark_68", c_float * 136),
("templates", c_ubyte * 2048)
]
32 changes: 32 additions & 0 deletions facesdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

from ctypes import *
from numpy.ctypeslib import ndpointer
from facebox import FaceBox

libPath = os.path.abspath(os.path.dirname(__file__)) + '/lib/facesdk2.dll'
facesdk = cdll.LoadLibrary(libPath)

getMachineCode = facesdk.getMachineCode
getMachineCode.argtypes = []
getMachineCode.restype = c_char_p

setActivation = facesdk.setActivation
setActivation.argtypes = [c_char_p]
setActivation.restype = c_int32

initSDK = facesdk.initSDK
initSDK.argtypes = [c_char_p]
initSDK.restype = c_int32

faceDetection = facesdk.faceDetection
faceDetection.argtypes = [ndpointer(c_ubyte, flags='C_CONTIGUOUS'), c_int32, c_int32, POINTER(FaceBox), c_int32]
faceDetection.restype = c_int32

templateExtraction = facesdk.templateExtraction
templateExtraction.argtypes = [ndpointer(c_ubyte, flags='C_CONTIGUOUS'), c_int32, c_int32, POINTER(FaceBox)]
templateExtraction.restype = c_int32

similarityCalculation = facesdk.similarityCalculation
similarityCalculation.argtypes = [c_ubyte * 2048, c_ubyte * 2048]
similarityCalculation.restype = c_float
62 changes: 62 additions & 0 deletions header/facesdk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

enum SDK_ERROR
{
SDK_SUCCESS = 0,
SDK_LICENSE_KEY_ERROR = -1,
SDK_LICENSE_APPID_ERROR = -2,
SDK_LICENSE_EXPIRED = -3,
SDK_NO_ACTIVATED = -4,
SDK_INIT_ERROR = -5,
};

typedef struct _tagFaceBox
{
int x1, y1, x2, y2;
float yaw, roll, pitch;
float face_quality, face_luminance, eye_dist;
float left_eye_closed, right_eye_closed, face_occlusion, mouth_opened;
float landmark_68[68 * 2];
unsigned char templates[2048];
} FaceBox;

/*
* Get the machine code for SDK activation
*/
const char* getMachineCode();

/*
* Activate the SDK using the provided license
*/

int setActivation(char* license);

/*
* Initialize the SDK with the specified model path
*/
int initSDK(char* modelPath);

/*
* Detect faces, perform liveness detection, determine face orientation (yaw, roll, pitch),
* assess face quality, detect facial occlusion, eye closure, mouth opening, and identify facial landmarks.
*/
int faceDetection(unsigned char* rgbData, int width, int height, FaceBox* faceBoxes, int faceBoxCount);

/*
* Create templates for face verification
*/

int templateExtraction(unsigned char* rgbData, int width, int height, FaceBox* faceBox);

/*
* Calculate the similarity between two templates
*/
float similarityCalculation(unsigned char* templates1, unsigned char* templates2);

#ifdef __cplusplus
}
#endif
114,134 changes: 114,134 additions & 0 deletions lib/cache.json

Large diffs are not rendered by default.

Binary file added lib/facesdk2.lib
Binary file not shown.
Binary file added lib/gna.dll
Binary file not shown.
Binary file added lib/ncnn.dll
Binary file not shown.
Binary file added lib/openvino.dll
Binary file not shown.
Binary file added lib/openvino_auto_batch_plugin.dll
Binary file not shown.
Binary file added lib/openvino_auto_plugin.dll
Binary file not shown.
Binary file added lib/openvino_c.dll
Binary file not shown.
Binary file added lib/openvino_gapi_preproc.dll
Binary file not shown.
Binary file added lib/openvino_hetero_plugin.dll
Binary file not shown.
Binary file added lib/openvino_intel_cpu_plugin.dll
Binary file not shown.
Binary file added lib/openvino_intel_gna_plugin.dll
Binary file not shown.
Binary file added lib/openvino_intel_gpu_plugin.dll
Binary file not shown.
Binary file added lib/openvino_ir_frontend.dll
Binary file not shown.
Binary file added lib/openvino_onnx_frontend.dll
Binary file not shown.
Binary file added lib/openvino_paddle_frontend.dll
Binary file not shown.
Binary file added lib/openvino_tensorflow_frontend.dll
Binary file not shown.
24 changes: 24 additions & 0 deletions lib/plugins.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ie>
<plugins>
<plugin name="AUTO" location="openvino_auto_plugin.dll">
</plugin>
<plugin name="BATCH" location="openvino_auto_batch_plugin.dll">
</plugin>
<plugin name="CPU" location="openvino_intel_cpu_plugin.dll">
</plugin>
<plugin name="GNA" location="openvino_intel_gna_plugin.dll">
</plugin>
<plugin name="GPU" location="openvino_intel_gpu_plugin.dll">
</plugin>
<plugin name="HETERO" location="openvino_hetero_plugin.dll">
</plugin>
<plugin name="MULTI" location="openvino_auto_plugin.dll">
</plugin>
<plugin name="MYRIAD" location="openvino_intel_myriad_plugin.dll">
</plugin>
<plugin name="HDDL" location="openvino_intel_hddl_plugin.dll">
</plugin>
<plugin name="VPUX" location="openvino_intel_vpux_plugin.dll">
</plugin>
</plugins>
</ie>
Binary file added lib/tbb.dll
Binary file not shown.
Binary file added lib/vulkan-1.dll
Binary file not shown.
6 changes: 6 additions & 0 deletions license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To obtain the license, please get in touch with us using the following contact information:

Email: [email protected]
Telegram: @kbyai
WhatsApp: +19092802609
Skype: live:.cid.66e2522354b1049b
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Pillow
numpy
117 changes: 117 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import sys
sys.path.append('.')

from PIL import Image
import numpy as np

from facesdk import getMachineCode
from facesdk import setActivation
from facesdk import faceDetection
from facesdk import initSDK
from facesdk import templateExtraction
from facesdk import similarityCalculation
from facebox import FaceBox

verifyThreshold = 0.7

maxFaceCount = 1

licensePath = "license.txt"
license = ""

machineCode = getMachineCode()
print("machineCode: ", machineCode.decode('utf-8'))

try:
with open(licensePath, 'r') as file:
license = file.read()
except IOError as exc:
print("failed to open license.txt: ", exc.errno)
print("license: ", license)


ret = setActivation(license.encode('utf-8'))
print("activation: ", ret)

ret = initSDK("D:/Temp/kby_face/github/FaceRecognition-Windows/data".encode('utf-8'))
print("init: ", ret)

def compare_face(file1, file2):
result = "None"
similarity = -1
face1 = None
face2 = None

try:
image1 = Image.open(file1)
except:
result = "Failed to open file1"
response = jsonify({"compare_result": result, "compare_similarity": similarity, "face1": face1, "face2": face2})

response.status_code = 200
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response


try:
image2 = Image.open(file2)
except:
result = "Failed to open file2"
response = jsonify({"compare_result": result, "compare_similarity": similarity, "face1": face1, "face2": face2})

response.status_code = 200
response.headers["Content-Type"] = "application/json; charset=utf-8"
return response

image_np1 = np.asarray(image1)
image_np2 = np.asarray(image2)

faceBoxes1 = (FaceBox * maxFaceCount)()
faceCount1 = faceDetection(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1, maxFaceCount)

faceBoxes2 = (FaceBox * maxFaceCount)()
faceCount2 = faceDetection(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2, maxFaceCount)

if faceCount1 == 1 and faceCount2 == 1:
templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes1[0])
templateExtraction(image_np2, image_np2.shape[1], image_np2.shape[0], faceBoxes2[0])
similarity = similarityCalculation(faceBoxes1[0].templates, faceBoxes2[0].templates)
if similarity > verifyThreshold:
result = "Same person"
else:
result = "Different person"
elif faceCount1 == 0:
result = "No face1"
elif faceCount2 == 0:
result = "No face2"

if faceCount1 == 1:
# landmark_68 = []
# for j in range(68):
# landmark_68.append({"x": faceBoxes1[0].landmark_68[j * 2], "y": faceBoxes1[0].landmark_68[j * 2 + 1]})

face1 = {"x1": faceBoxes1[0].x1, "y1": faceBoxes1[0].y1, "x2": faceBoxes1[0].x2, "y2": faceBoxes1[0].y2,
"yaw": faceBoxes1[0].yaw, "roll": faceBoxes1[0].roll, "pitch": faceBoxes1[0].pitch,
"face_quality": faceBoxes1[0].face_quality, "face_luminance": faceBoxes1[0].face_luminance, "eye_dist": faceBoxes1[0].eye_dist,
"left_eye_closed": faceBoxes1[0].left_eye_closed, "right_eye_closed": faceBoxes1[0].right_eye_closed,
"face_occlusion": faceBoxes1[0].face_occlusion, "mouth_opened": faceBoxes1[0].mouth_opened}
# "landmark_68": landmark_68}

if faceCount2 == 1:
# landmark_68 = []
# for j in range(68):
# landmark_68.append({"x": faceBoxes2[0].landmark_68[j * 2], "y": faceBoxes2[0].landmark_68[j * 2 + 1]})

face2 = {"x1": faceBoxes2[0].x1, "y1": faceBoxes2[0].y1, "x2": faceBoxes2[0].x2, "y2": faceBoxes2[0].y2,
"yaw": faceBoxes2[0].yaw, "roll": faceBoxes2[0].roll, "pitch": faceBoxes2[0].pitch,
"face_quality": faceBoxes2[0].face_quality, "face_luminance": faceBoxes2[0].face_luminance, "eye_dist": faceBoxes2[0].eye_dist,
"left_eye_closed": faceBoxes2[0].left_eye_closed, "right_eye_closed": faceBoxes2[0].right_eye_closed,
"face_occlusion": faceBoxes2[0].face_occlusion, "mouth_opened": faceBoxes2[0].mouth_opened}
# "landmark_68": landmark_68}

response = {"compare_result": result, "compare_similarity": similarity, "face1": face1, "face2": face2}
return response

if __name__ == "__main__":
ret = compare_face('D:/Temp/kby_face/github/FaceRecognition-Windows/face_examples/1.jpg', 'D:/Temp/kby_face/github/FaceRecognition-Windows/face_examples/2.jpg')
print(ret)

0 comments on commit 1aa2e3c

Please sign in to comment.