Skip to main content

SafeAI face detection and face retrieval library using milvus vector DB

Project description

SafeAI Face Detection & Face Retrieval

SafeAI face detection and face retrieval library


Installation

pip install safeai-face

Functions

Face Detection

The [ face_detection ] function is used to detect faces in an image using a YOLO-based model. It supports both simple face detection and tracking over multiple frames if needed. The function returns a list of detected faces, each containing the bounding box coordinates, tracking ID (if enabled), and confidence score.

Example Usage

import cv2
from safevision_face import face_detection

image = cv2.imread("image_path")
detection = face_detection(image, do_track=True)

#output : [{'box': (56, 16, 169, 167), 'track_id': 1, 'score': 0.8751850724220276}]

Parameters

  • image_bgr(np.ndarray)

    The input image in BGR format. This is typically read using OpenCV (cv2.imread).

  • conf(float)

    The confidence threshold for face detection. Detections with confidence scores below this value are ignored.

    Default: 0.4.

  • iou(float)

    The confidence threshold for face detection. Detections with confidence scores below this value are ignored.

    Default: 0.4.

  • do_track(bool)

    A boolean flag indicating whether to enable tracking. If True, the function will use a tracker to assign unique IDs to detected faces across frames.

    Default: False.

  • tracker_config(str)

    The configuration file for the tracker, used when do_track is set to True.

    Default: "bytetrack.yaml".


Face Extraction

The [ face_extraction ] function extracts a feature embedding vector from a given face image using the EdgeFace model. This embedding is a numerical representation of the face, which can be used for tasks like face recognition, clustering, or similarity comparison.

Example Usage

from safevision_face import face_extraction

vec = face_extraction(image)

#output : a vector of 512 dimensions

Parameters

  • image_bgr(np.ndarray)

    The input image in BGR format. Typically, this is a cropped face image obtained from a face detection model.


Database Init

The [ db_set ] function initializes a connection to a Milvus database and creates a collection for storing vector data if it does not already exist. Milvus is a vector database commonly used for managing embeddings for similarity search and machine learning tasks.

Example Usage

from safevision_face import db_set

client = db_set(
    db_path="your_db_path/db_name.db", 
    collection_name="your_collection_name",
    dimension=512,
    metric_type="COSINE"
)

#output : 

Parameters

  • db_path(str)

    The URI of the Milvus database.

  • collection_name(str)

    The name of the collection in the Milvus database.

  • dimension(int)

    The dimensionality of the vectors to be stored in the collection. This should match the dimension of the embeddings being used.

    Default: 512

  • metric_type(str)

    The distance metric used for similarity searches in the collection.

    Default: COSINE

  • auto_id(bool)

    Whether to enable automatic ID generation for records in the collection.

    Default: True

  • enable_dynamic_field(bool)

    Whether to allow dynamic fields in the collection. Dynamic fields let you store non-fixed schema attributes.

    Default: True


Database Insert

The [ db_insert ] function is used to add a record into a specific collection in a Milvus database. The record contains both a vector (embedding) and associated metadata, enabling vector-based similarity searches while preserving contextual information about the stored data.

from safevision_face import db_insert

db_insert(
    client,
    collection_name="your_collection_name",
    vector=embedding_vector,
    orig_path="/some/orig_path.jpg",
    crop_path="/some/crop_path.jpg",
    timestamp="20250101_120000",
    tracking_id=123,
    cam_id="cam_number"
)

#output : The function returns True after successfully inserting the record into the collection.

Parameters

  • client(MilvusCLient)

    An instance of the MilvusClient connected to the database.

  • collection_name(str)

    The name of the collection in which the data will be inserted.

  • vector(np.ndarray)

    A vector (embedding) to be stored in the database. This represents the numerical representation of data, such as facial embeddings for similarity search.

  • orig_path(str)

    The file path of the original image associated with the vector.

  • crop_path(str)

    The file path of the cropped image associated with the vector.

  • timestamp(str)

    A timestamp indicating when the data was generated or captured.

  • tracking_id(int)

    An ID used for tracking individuals or objects across frames or locations.

  • cam_id(str)

    The identifier of the camera or device from which the data was captured.


Database Search

The [ db_search ] function performs a similarity search in a Milvus collection by comparing a query image's embedding (vector) against stored vectors. The results include the top matches that meet a specified similarity threshold.

from safevision_face import db_search

results = db_search(
    client,
    collection_name="face_collection",
    query_image=image_bgr,
    top_k=1,
    threshold=0.4,
    extractor_func=face_extraction
)

#output : [{'score': 0.7510387301445007, 'entity': {'orig_path': '/some/orig_path.jpg', 'crop_path': '/some/crop_path.jpg', 'timestamp': '20250101_120000', 'tracking_id': 123, 'cam_id': 'cam102'}}]

Parameters

  • client(MilvusCLient)

    An instance of the MilvusClient connected to the Milvus database, enabling search operations on a specific collection.

  • collection_name(str)

    The name of the collection in the Milvus database where the search will be performed.

  • query_image(np.ndarray)

    The image (in numpy array format) for which the similarity search is conducted. This image will be converted into an embedding using the provided extractor_func.

  • top_k(int, default: 5)

    The maximum number of top matches to retrieve from the database.

  • threshold(float, default: 0.4)

    The minimum similarity score for a match to be considered valid. Matches with a score below this value will be filtered out.

  • extractor_func

    A function to extract the vector (embedding) from the query image. The function should take an image (numpy array) as input and return its embedding.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeai_face-0.0.2.1.tar.gz (51.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeai_face-0.0.2.1-py3-none-any.whl (51.1 MB view details)

Uploaded Python 3

File details

Details for the file safeai_face-0.0.2.1.tar.gz.

File metadata

  • Download URL: safeai_face-0.0.2.1.tar.gz
  • Upload date:
  • Size: 51.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for safeai_face-0.0.2.1.tar.gz
Algorithm Hash digest
SHA256 6e112c71011f15c83842742a0cc221419614dee954a074b3c462b9e8480c92fe
MD5 d711bee2e270267d9700a11b0154fd6b
BLAKE2b-256 15ac7122d20da8b4cdd182da5e1ccbd7f7a39d263c885495b30016bd0fac196f

See more details on using hashes here.

File details

Details for the file safeai_face-0.0.2.1-py3-none-any.whl.

File metadata

  • Download URL: safeai_face-0.0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 51.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for safeai_face-0.0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a6a58a5948bb1672f4509c20fa5fd6683a63e338b4b48cd4e4371a227e64fcc
MD5 286102c4bb5622c67159065fe99fda05
BLAKE2b-256 e428fe040c118fd0ece3f6b376e4e63dbdba2d7e1d9ae00a5b30f63ffea8de0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page