HyperInspire / InspireFace

InspireFace is a cross-platform face recognition SDK developed in C/C++, supporting multiple operating systems and various backend types for inference, such as CPU, GPU, and NPU.
72 stars 13 forks source link

How to save and load face feature database to disk in python? #111

Open nauyeskhan-monosoft opened 1 week ago

nauyeskhan-monosoft commented 1 week ago

I am using InspireFace’s FeatureHub in Python to handle face recognition features, and I would like to save the extracted features for millions of images in a persistent database file. My goal is to avoid reprocessing and reinserting each feature on every run.

Currently, I’ve configured FeatureHub as follows:

feature_hub_config = ifac.FeatureHubConfiguration(
    feature_block_num=20,
    enable_use_db=True,
    db_path="face_features.db",
    search_threshold=-1,
    search_mode=ifac.HF_SEARCH_MODE_EXHAUSTIVE,
)
ret = ifac.feature_hub_enable(feature_hub_config)
assert ret, "Failed to enable FeatureHub."

for idx, image_name in tqdm(enumerate(os.listdir(origin_database_path))):
    image_path = os.path.join(origin_database_path, image_name)
    if image_path.endswith((".jpg", ".jpeg", ".png")):
        image = cv2.imread(image_path)
        assert image is not None, f"Failed to load image {image_path}"
        faces = session.face_detection(image)
        if faces:
            face = faces[0]  # Use the first detected face
            feature = session.face_feature_extract(image, face)
            identity = ifac.FaceIdentity(feature, custom_id=idx, tag=image_name)
            ret = ifac.feature_hub_face_insert(identity)
            assert ret, f"Failed to insert face for {image_name}"

Despite setting enable_use_db=True and specifying db_path, I don’t see any database file saved at the specified location. Is there an option or additional method to save the FeatureHub to disk, so that it can be loaded directly in a new session without re-insertion? If so, could you provide guidance on how to do this in Python?

tunmx commented 1 week ago

Thanks for your feedback. Theoretically, if you specify db_path, it should be stored. If not, there may be some problems, I will test the latest version later and give you feedback.

nauyeskhan-monosoft commented 5 days ago

Hello!

@tunmx, any updates?

tunmx commented 5 days ago

Sorry, I haven't updated it yet, maybe it needs to be on the weekend.

tunmx commented 14 hours ago

Hi, I've fixed this issue and added Python test cases. It occurred in the Python native interface, so you just need to pull the code - no need to recompile the dynamic library. #112

import os
import cv2
import inspireface as ifac
from inspireface.param import *
import numpy as np
import os

def case_feature_hub():
    db_path = "test.db"
    if os.path.exists(db_path):
        os.remove(db_path)
    # Configure the feature management system.
    feature_hub_config = ifac.FeatureHubConfiguration(
        feature_block_num=10,
        enable_use_db=True,
        db_path=db_path,
        search_threshold=0.48,
        search_mode=HF_SEARCH_MODE_EAGER,
    )
    ret = ifac.feature_hub_enable(feature_hub_config)
    assert ret, "Failed to enable FeatureHub."
    print(ifac.feature_hub_get_face_count())
    for i in range(10):
        feature = ifac.FaceIdentity(np.random.rand(512), i, "test")
        ifac.feature_hub_face_insert(feature)
    print(ifac.feature_hub_get_face_count())

    assert os.path.exists(db_path), "FeatureHub database file not found."

if __name__ == "__main__":
    case_feature_hub()