Update InspireFace to 1.1.6

This commit is contained in:
tunm
2024-07-17 08:53:50 +08:00
parent b9c1d8bdba
commit 3a8dd7710f
30 changed files with 1278 additions and 426 deletions

View File

@@ -1424,31 +1424,62 @@ if _libs[_LIBRARY_FILENAME].has("HFFaceQualityDetect", "cdecl"):
HFFaceQualityDetect.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
class struct_HFFaceIntereactionResult(Structure):
class struct_HFFaceIntereactionState(Structure):
pass
struct_HFFaceIntereactionResult.__slots__ = [
struct_HFFaceIntereactionState.__slots__ = [
'num',
'leftEyeStatusConfidence',
'rightEyeStatusConfidence',
]
struct_HFFaceIntereactionResult._fields_ = [
struct_HFFaceIntereactionState._fields_ = [
('num', HInt32),
('leftEyeStatusConfidence', HPFloat),
('rightEyeStatusConfidence', HPFloat),
]
HFFaceIntereactionResult = struct_HFFaceIntereactionResult# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
HFFaceIntereactionState = struct_HFFaceIntereactionState# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
PHFFaceIntereactionResult = POINTER(struct_HFFaceIntereactionResult)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
PHFFaceIntereactionState = POINTER(struct_HFFaceIntereactionState)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 647
if _libs[_LIBRARY_FILENAME].has("HFGetFaceIntereactionResult", "cdecl"):
HFGetFaceIntereactionResult = _libs[_LIBRARY_FILENAME].get("HFGetFaceIntereactionResult", "cdecl")
HFGetFaceIntereactionResult.argtypes = [HFSession, PHFFaceIntereactionResult]
HFGetFaceIntereactionResult.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 652
if _libs[_LIBRARY_FILENAME].has("HFGetFaceIntereactionStateResult", "cdecl"):
HFGetFaceIntereactionStateResult = _libs[_LIBRARY_FILENAME].get("HFGetFaceIntereactionStateResult", "cdecl")
HFGetFaceIntereactionStateResult.argtypes = [HFSession, PHFFaceIntereactionState]
HFGetFaceIntereactionStateResult.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 675
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 661
class struct_HFFaceIntereactionsActions(Structure):
pass
struct_HFFaceIntereactionsActions.__slots__ = [
'num',
'normal',
'shake',
'jawOpen',
'headRiase',
'blink',
]
struct_HFFaceIntereactionsActions._fields_ = [
('num', HInt32),
('normal', HPInt32),
('shake', HPInt32),
('jawOpen', HPInt32),
('headRiase', HPInt32),
('blink', HPInt32),
]
HFFaceIntereactionsActions = struct_HFFaceIntereactionsActions# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 661
PHFFaceIntereactionsActions = POINTER(struct_HFFaceIntereactionsActions)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 661
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 663
if _libs[_LIBRARY_FILENAME].has("HFGetFaceIntereactionActionsResult", "cdecl"):
HFGetFaceIntereactionActionsResult = _libs[_LIBRARY_FILENAME].get("HFGetFaceIntereactionActionsResult", "cdecl")
HFGetFaceIntereactionActionsResult.argtypes = [HFSession, PHFFaceIntereactionsActions]
HFGetFaceIntereactionActionsResult.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 691
class struct_HFFaceAttributeResult(Structure):
pass
@@ -1465,17 +1496,17 @@ struct_HFFaceAttributeResult._fields_ = [
('ageBracket', HPInt32),
]
HFFaceAttributeResult = struct_HFFaceAttributeResult# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 675
HFFaceAttributeResult = struct_HFFaceAttributeResult# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 691
PHFFaceAttributeResult = POINTER(struct_HFFaceAttributeResult)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 675
PHFFaceAttributeResult = POINTER(struct_HFFaceAttributeResult)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 691
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 687
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 703
if _libs[_LIBRARY_FILENAME].has("HFGetFaceAttributeResult", "cdecl"):
HFGetFaceAttributeResult = _libs[_LIBRARY_FILENAME].get("HFGetFaceAttributeResult", "cdecl")
HFGetFaceAttributeResult.argtypes = [HFSession, PHFFaceAttributeResult]
HFGetFaceAttributeResult.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 701
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 717
class struct_HFInspireFaceVersion(Structure):
pass
@@ -1490,51 +1521,51 @@ struct_HFInspireFaceVersion._fields_ = [
('patch', c_int),
]
HFInspireFaceVersion = struct_HFInspireFaceVersion# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 701
HFInspireFaceVersion = struct_HFInspireFaceVersion# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 717
PHFInspireFaceVersion = POINTER(struct_HFInspireFaceVersion)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 701
PHFInspireFaceVersion = POINTER(struct_HFInspireFaceVersion)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 717
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 711
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 727
if _libs[_LIBRARY_FILENAME].has("HFQueryInspireFaceVersion", "cdecl"):
HFQueryInspireFaceVersion = _libs[_LIBRARY_FILENAME].get("HFQueryInspireFaceVersion", "cdecl")
HFQueryInspireFaceVersion.argtypes = [PHFInspireFaceVersion]
HFQueryInspireFaceVersion.restype = HResult
enum_HFLogLevel = c_int# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
enum_HFLogLevel = c_int# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HF_LOG_NONE = 0# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HF_LOG_NONE = 0# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HF_LOG_DEBUG = (HF_LOG_NONE + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HF_LOG_DEBUG = (HF_LOG_NONE + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HF_LOG_INFO = (HF_LOG_DEBUG + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HF_LOG_INFO = (HF_LOG_DEBUG + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HF_LOG_WARN = (HF_LOG_INFO + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HF_LOG_WARN = (HF_LOG_INFO + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HF_LOG_ERROR = (HF_LOG_WARN + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HF_LOG_ERROR = (HF_LOG_WARN + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HF_LOG_FATAL = (HF_LOG_ERROR + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HF_LOG_FATAL = (HF_LOG_ERROR + 1)# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
HFLogLevel = enum_HFLogLevel# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 723
HFLogLevel = enum_HFLogLevel# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 739
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 728
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 744
if _libs[_LIBRARY_FILENAME].has("HFSetLogLevel", "cdecl"):
HFSetLogLevel = _libs[_LIBRARY_FILENAME].get("HFSetLogLevel", "cdecl")
HFSetLogLevel.argtypes = [HFLogLevel]
HFSetLogLevel.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 733
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 749
if _libs[_LIBRARY_FILENAME].has("HFLogDisable", "cdecl"):
HFLogDisable = _libs[_LIBRARY_FILENAME].get("HFLogDisable", "cdecl")
HFLogDisable.argtypes = []
HFLogDisable.restype = HResult
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 746
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 762
if _libs[_LIBRARY_FILENAME].has("HFDeBugImageStreamImShow", "cdecl"):
HFDeBugImageStreamImShow = _libs[_LIBRARY_FILENAME].get("HFDeBugImageStreamImShow", "cdecl")
HFDeBugImageStreamImShow.argtypes = [HFImageStream]
HFDeBugImageStreamImShow.restype = None
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 757
# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 773
if _libs[_LIBRARY_FILENAME].has("HFDeBugImageStreamDecodeSave", "cdecl"):
HFDeBugImageStreamDecodeSave = _libs[_LIBRARY_FILENAME].get("HFDeBugImageStreamDecodeSave", "cdecl")
HFDeBugImageStreamDecodeSave.argtypes = [HFImageStream, HPath]
@@ -1618,11 +1649,13 @@ HFFaceMaskConfidence = struct_HFFaceMaskConfidence# /Users/tunm/work/InspireFace
HFFaceQualityConfidence = struct_HFFaceQualityConfidence# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 611
HFFaceIntereactionResult = struct_HFFaceIntereactionResult# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
HFFaceIntereactionState = struct_HFFaceIntereactionState# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 645
HFFaceAttributeResult = struct_HFFaceAttributeResult# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 675
HFFaceIntereactionsActions = struct_HFFaceIntereactionsActions# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 661
HFInspireFaceVersion = struct_HFInspireFaceVersion# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 701
HFFaceAttributeResult = struct_HFFaceAttributeResult# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 691
HFInspireFaceVersion = struct_HFInspireFaceVersion# /Users/tunm/work/InspireFace/cpp/inspireface/c_api/inspireface.h: 717
# No inserted files

View File

@@ -150,6 +150,11 @@ class FaceExtended:
quality_confidence: float
left_eye_status_confidence: float
right_eye_status_confidence: float
action_normal: int
action_jaw_open: int
action_shake: int
action_blink: int
action_head_raise: int
race: int
gender: int
age_bracket: int
@@ -356,7 +361,6 @@ class InspireFaceSession(object):
if ret != 0:
logger.error(f"Set filter minimum face pixel size error: {ret}")
def face_pipeline(self, image, faces: List[FaceInformation], exec_param) -> List[FaceExtended]:
"""
Processes detected faces to extract additional attributes based on the provided execution parameters.
@@ -387,7 +391,7 @@ class InspireFaceSession(object):
logger.error(f"Face pipeline error: {ret}")
return []
extends = [FaceExtended(-1.0, -1.0, -1.0, -1.0, -1.0, -1, -1, -1) for _ in range(len(faces))]
extends = [FaceExtended(-1.0, -1.0, -1.0, -1.0, -1.0, 0, 0, 0, 0, 0, -1, -1, -1) for _ in range(len(faces))]
self._update_mask_confidence(exec_param, flag, extends)
self._update_rgb_liveness_confidence(exec_param, flag, extends)
self._update_face_quality_confidence(exec_param, flag, extends)
@@ -456,14 +460,25 @@ class InspireFaceSession(object):
def _update_face_interact_confidence(self, exec_param, flag, extends):
if (flag == "object" and exec_param.enable_interaction_liveness) or (
flag == "bitmask" and exec_param & HF_ENABLE_INTERACTION):
results = HFFaceIntereactionResult()
ret = HFGetFaceIntereactionResult(self._sess, PHFFaceIntereactionResult(results))
results = HFFaceIntereactionState()
ret = HFGetFaceIntereactionStateResult(self._sess, PHFFaceIntereactionState(results))
if ret == 0:
for i in range(results.num):
extends[i].left_eye_status_confidence = results.leftEyeStatusConfidence[i]
extends[i].right_eye_status_confidence = results.rightEyeStatusConfidence[i]
else:
logger.error(f"Get face interact result error: {ret}")
actions = HFFaceIntereactionsActions()
ret = HFGetFaceIntereactionActionsResult(self._sess, PHFFaceIntereactionsActions(actions))
if ret == 0:
for i in range(results.num):
extends[i].action_normal = actions.normal[i]
extends[i].action_shake = actions.shake[i]
extends[i].action_jaw_open = actions.jawOpen[i]
extends[i].action_head_raise = actions.headRiase[i]
extends[i].action_blink = actions.blink[i]
else:
logger.error(f"Get face action result error: {ret}")
def _update_rgb_liveness_confidence(self, exec_param, flag, extends: List[FaceExtended]):
if (flag == "object" and exec_param.enable_liveness) or (

View File

@@ -1,14 +1,41 @@
import time
import click
import cv2
import inspireface as ifac
from inspireface.param import *
import numpy as np
def generate_color(id):
"""
Generate a bright color based on the given integer ID. Ensures 50 unique colors.
Args:
id (int): The ID for which to generate a color.
Returns:
tuple: A tuple representing the color in BGR format.
"""
max_id = 50 # Number of unique colors
id = id % max_id
# Generate HSV color
hue = int((id * 360 / max_id) % 360) # Distribute hue values equally
saturation = 200 + (55 * id) % 55 # High saturation for bright colors
value = 200 + (55 * id) % 55 # High value for bright colors
hsv_color = np.uint8([[[hue, saturation, value]]])
rgb_color = cv2.cvtColor(hsv_color, cv2.COLOR_HSV2BGR)[0][0]
return (int(rgb_color[0]), int(rgb_color[1]), int(rgb_color[2]))
@click.command()
@click.argument("resource_path")
@click.argument('source')
@click.option('--show', is_flag=True, help='Display the video stream or video file in a window.')
def case_face_tracker_from_video(resource_path, source, show):
@click.option('--out', type=str, default=None, help='Path to save the processed video.')
def case_face_tracker_from_video(resource_path, source, show, out):
"""
Launch a face tracking process from a video source. The 'source' can either be a webcam index (0, 1, ...)
or a path to a video file. Use the --show option to display the video.
@@ -17,6 +44,7 @@ def case_face_tracker_from_video(resource_path, source, show):
resource_path (str): Path to the resource directory for face tracking algorithms.
source (str): Webcam index or path to the video file.
show (bool): If set, the video will be displayed in a window.
out (str): Path to save the processed video.
"""
# Initialize the face tracker or other resources.
print(f"Initializing with resources from: {resource_path}")
@@ -25,9 +53,9 @@ def case_face_tracker_from_video(resource_path, source, show):
assert ret, "Launch failure. Please ensure the resource path is correct."
# Optional features, loaded during session creation based on the modules specified.
opt = HF_ENABLE_NONE
session = ifac.InspireFaceSession(opt, HF_DETECT_MODE_LIGHT_TRACK) # Use video mode
opt = HF_ENABLE_NONE | HF_ENABLE_INTERACTION
session = ifac.InspireFaceSession(opt, HF_DETECT_MODE_ALWAYS_DETECT, max_detect_num=25, detect_pixel_level=320) # Use video mode
session.set_filter_minimum_face_pixel_size(0)
# Determine if the source is a digital webcam index or a video file path.
try:
source_index = int(source) # Try to convert source to an integer.
@@ -42,6 +70,15 @@ def case_face_tracker_from_video(resource_path, source, show):
print("Error: Could not open video source.")
return
# VideoWriter to save the processed video if out is provided.
if out:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = cap.get(cv2.CAP_PROP_FPS) if cap.get(cv2.CAP_PROP_FPS) > 0 else 30
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out_video = cv2.VideoWriter(out, fourcc, fps, (frame_width, frame_height))
print(f"Saving video to: {out}")
# Main loop to process video frames.
while True:
ret, frame = cap.read()
@@ -50,43 +87,55 @@ def case_face_tracker_from_video(resource_path, source, show):
# Process frame here (e.g., face detection/tracking).
faces = session.face_detection(frame)
for idx, face in enumerate(faces):
print(f"{'==' * 20}")
print(f"idx: {idx}")
# Print Euler angles of the face.
print(f"roll: {face.roll}, yaw: {face.yaw}, pitch: {face.pitch}")
exts = session.face_pipeline(frame, faces, HF_ENABLE_INTERACTION)
print(exts)
for idx, face in enumerate(faces):
# Get face bounding box
x1, y1, x2, y2 = face.location
# Calculate center, size, and angle
center = ((x1 + x2) / 2, (y1 + y2) / 2)
size = (x2 - x1, y2 - y1)
angle = face.roll
# Get rotation matrix
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
angle = face.roll
# Apply rotation to the bounding box corners
rect = ((center[0], center[1]), (size[0], size[1]), angle)
box = cv2.boxPoints(rect)
box = box.astype(int)
color = generate_color(face.track_id)
# Draw the rotated bounding box
cv2.drawContours(frame, [box], 0, (100, 180, 29), 2)
cv2.drawContours(frame, [box], 0, color, 4)
# Draw landmarks
lmk = session.get_face_dense_landmark(face)
for x, y in lmk.astype(int):
cv2.circle(frame, (x, y), 0, (220, 100, 0), 2)
cv2.circle(frame, (x, y), 0, color, 4)
# Draw track ID at the top of the bounding box
text = f"ID: {face.track_id}"
text_size, _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
text_x = min(box[:, 0])
text_y = min(box[:, 1]) - 10
if text_y < 0:
text_y = min(box[:, 1]) + text_size[1] + 10
cv2.putText(frame, text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
if show:
cv2.imshow("Face Tracker", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break # Exit loop if 'q' is pressed.
if out:
out_video.write(frame)
# Cleanup: release video capture and close any open windows.
cap.release()
if out:
out_video.release()
cv2.destroyAllWindows()
print("Released all resources and closed windows.")