ImagesVectorDB class provides vector database operations for storing and searching face embeddings using ObjectBox with HNSW (Hierarchical Navigable Small World) indexing.
Constructor
The class is annotated with@Single and managed by Koin dependency injection.
FaceImageRecord entities with vector search capabilities.
Methods
addFaceImageRecord
Adds a new face embedding record to the database.Face image record containing person ID, name, and 512-dimensional embedding vector
getNearestEmbeddingPersonName
Finds the most similar face embedding in the database using either ANN (Approximate Nearest Neighbor) or linear search.Query embedding vector (512 dimensions) to search for
true: Use linear search for precise results (slower, exhaustive search)false: Use ObjectBox HNSW index for approximate results (faster)
The face record with the highest cosine similarity, or null if no records exist
removeFaceRecordsWithPersonID
Deletes all face embeddings associated with a specific person.Person ID to remove all face records for
Search algorithms
HNSW approximate search
WhenflatSearch = false, uses ObjectBox’s HNSW index for fast approximate nearest neighbor search.
Maximum number of candidates to retrieve. Also serves as the “ef” HNSW parameter for search quality. Higher values improve quality at the cost of performance
Uses cosine distance for similarity measurement, configured in
FaceImageRecordLinear flat search
WhenflatSearch = true, performs exhaustive linear search across all records with parallel processing.
Number of parallel threads for batch processing
Splits all records into batches, processes in parallel using coroutines on
Dispatchers.Default, then returns the record with maximum cosine similarityCosine distance calculation
Calculates cosine similarity between two embedding vectors:- 1 = identical vectors
- 0 = orthogonal vectors
- -1 = opposite vectors
Usage example
Performance considerations
HNSW search (flatSearch = false)
- Faster: O(log n) average case
- Approximate results
- Suitable for real-time recognition
- Quality can be tuned with maxResultCount parameter
Linear search (flatSearch = true)
- Slower: O(n) complexity
- Exact results
- Uses parallel processing with 4 threads
- Better for smaller databases or when precision is critical
data/ImagesVectorDB.kt:10