Skip to main content

Professional geometric face recognition library with AI-powered matching

Project description

GFRAM - Geometric Face Recognition and Matching

Python 3.8+ License: MIT PyPI version Documentation Downloads

Professional face recognition library based on geometric features and GeometricTransformer architecture โ€” lightweight, privacy-preserving, and highly accurate!

GFRAM Logo

๐ŸŒŸ Key Features

  • ๐Ÿ”ท Pure Geometric Approach: 150 geometric features from 478 3D facial landmarks
  • ๐Ÿค– Custom AI Architecture: GeometricTransformer with Multi-Head Self-Attention
  • โšก Lightweight: Only 1.3M parameters (50x smaller than ResNet-100)
  • ๐Ÿ”’ Privacy-Preserving: Only geometric vectors transmitted, not images
  • ๐Ÿš€ High Performance: 99.52% accuracy on LFW, <100ms on CPU
  • ๐Ÿ“ฆ Easy Installation: pip install gfram
  • ๐ŸŒ Cross-Platform: Windows, macOS, Linux, and mobile devices

๐ŸŽฏ Scientific Innovation

GFRAM introduces a novel hybrid approach combining geometric analysis with deep learning:

Innovation Description
478 3D Landmarks MediaPipe FaceMesh vs traditional 68 points (7x more data)
150 Geometric Features Distances, ratios, angles, areas, Delaunay, symmetry, Hu moments
GeometricTransformer Self-attention on landmark relationships (1.3M params)
Hybrid Matching 30% geometric + 70% semantic = 99.52% accuracy
Privacy by Design Impossible to reconstruct face from geometric vector

๐Ÿ“Š Performance Comparison

Model LFW Acc CFP-FP Params Speed (CPU) Model Size
ArcFace-R100 99.83% 98.27% 65M ~450ms 249 MB
MobileFaceNet 99.28% 96.12% 1M ~35ms 4.1 MB
GFRAM 99.52% 95.43% 1.3M <100ms 5.8 MB

GFRAM achieves competitive accuracy with significantly lower computational requirements

๐Ÿ“ฆ Installation

Basic Installation

pip install gfram

Full Installation (with all features)

pip install gfram[all]

From Source

git clone https://github.com/feruza-42h/gfram.git
cd gfram
pip install -e .

Requirements

  • Python 3.8+
  • PyTorch 1.9+
  • MediaPipe 0.9+
  • NumPy, SciPy, OpenCV

๐Ÿš€ Quick Start

Face Recognition in 5 Lines

from gfram import GFRAMRecognizer

# Initialize (model auto-downloads on first run)
recognizer = GFRAMRecognizer()

# Add people to database
recognizer.add_user("john_doe", ["john1.jpg", "john2.jpg", "john3.jpg"])
recognizer.add_user("jane_smith", ["jane1.jpg", "jane2.jpg"])

# Identify unknown face
result = recognizer.identify("unknown.jpg")
print(f"Identity: {result.user_id}, Confidence: {result.confidence:.2%}")
# Output: Identity: john_doe, Confidence: 98.45%

Face Verification (1:1 Matching)

from gfram import GFRAMRecognizer

recognizer = GFRAMRecognizer()

# Compare two faces
match = recognizer.verify("photo_a.jpg", "photo_b.jpg")
print(f"Same person: {match.match}, Score: {match.score:.3f}")
# Output: Same person: True, Score: 0.892

Extract Geometric Features Only

from gfram import GeometricExtractor

extractor = GeometricExtractor()

# Extract 150 geometric features
features = extractor.extract("face.jpg")
print(f"Feature vector shape: {features.shape}")  # (150,)
print(f"Interocular distance: {features[0]:.4f}")
print(f"Face width/height ratio: {features[45]:.4f}")

Using GeometricTransformer Directly

import torch
from gfram.models import GeometricTransformer

# Create model
model = GeometricTransformer(
    d_model=256,
    nhead=8,
    num_layers=4,
    dim_feedforward=1024
)

# Forward pass with landmarks
landmarks = torch.randn(1, 478, 3)  # (batch, points, xyz)
embedding = model(landmarks)  # (1, 512)
print(f"Embedding shape: {embedding.shape}")

Real-time Recognition from Webcam

from gfram import GFRAMRecognizer
import cv2

recognizer = GFRAMRecognizer()
recognizer.load("my_database.gfram")

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break
    
    # Recognize faces in frame
    results = recognizer.identify_frame(frame)
    
    for face in results:
        # Draw bounding box and name
        x, y, w, h = face.bbox
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.putText(frame, f"{face.user_id}: {face.confidence:.1%}", 
                    (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
    
    cv2.imshow("GFRAM Recognition", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

๐Ÿ—๏ธ Architecture

System Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         GFRAM Pipeline                          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Input Image                                                    โ”‚
โ”‚       โ†“                                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ RetinaFace  โ”‚ โ†’ โ”‚  MediaPipe  โ”‚ โ†’ โ”‚  478 3D Landmarks   โ”‚ โ”‚
โ”‚  โ”‚  Detection  โ”‚    โ”‚  FaceMesh   โ”‚    โ”‚    (x, y, z)        โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚       โ†“                                          โ†“              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”โ”‚
โ”‚  โ”‚  Geometric Features     โ”‚    โ”‚   GeometricTransformer      โ”‚โ”‚
โ”‚  โ”‚  G โˆˆ โ„ยนโตโฐ               โ”‚    โ”‚   E โˆˆ โ„โตยนยฒ                  โ”‚โ”‚
โ”‚  โ”‚  (~6ms CPU)             โ”‚    โ”‚   (~45ms CPU)               โ”‚โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚
โ”‚                โ†“                              โ†“                 โ”‚
โ”‚           โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚
โ”‚           โ”‚     Hybrid Similarity Matching          โ”‚          โ”‚
โ”‚           โ”‚     S = 0.3ยทsim_G + 0.7ยทsim_E           โ”‚          โ”‚
โ”‚           โ”‚     HNSW Index: O(log n) search         โ”‚          โ”‚
โ”‚           โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚
โ”‚                              โ†“                                  โ”‚
โ”‚                    Identity + Confidence                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Geometric Features (150 total)

Category Count Description Invariance
Euclidean Distances 45 Key landmark distances Rotation
Proportion Ratios 35 Normalized ratios, Golden ratio Scale
Angles 30 Angles between landmark triplets Scale, Translation
Areas 15 Triangle areas from landmarks Rotation
Delaunay Features 10 Triangulation topology Topological
Symmetry 8 Bilateral symmetry coefficients Scale
Hu Moments 7 Shape invariants Affine

GeometricTransformer Architecture

Input: X โˆˆ โ„^(478ร—3) โ€” 3D Landmarks
        โ†“
Linear Embedding: โ„ยณ โ†’ โ„ยฒโตโถ (768 params)
        โ†“
3D Positional Encoding (122K params)
        โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Transformer Encoder ร— 4          โ”‚
โ”‚   โ”œโ”€โ”€ Multi-Head Self-Attention    โ”‚
โ”‚   โ”‚   (8 heads, d_k=32)            โ”‚
โ”‚   โ”œโ”€โ”€ Add & LayerNorm              โ”‚
โ”‚   โ”œโ”€โ”€ Feed-Forward (256โ†’1024โ†’256)  โ”‚
โ”‚   โ””โ”€โ”€ Add & LayerNorm              โ”‚
โ”‚   (1.05M params)                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ†“
Global Average Pooling: โ„^(478ร—256) โ†’ โ„ยฒโตโถ
        โ†“
Projection Head: โ„ยฒโตโถ โ†’ โ„โตยนยฒ (131K params)
        โ†“
L2 Normalization
        โ†“
Output: e โˆˆ โ„โตยนยฒ โ€” Normalized Embedding

Total: ~1.3M parameters

๐Ÿ“š API Reference

GFRAMRecognizer

class GFRAMRecognizer:
    def __init__(
        self,
        model_version: str = "2.0.0",
        device: str = "cpu",  # or "cuda"
        alpha: float = 0.3,   # geometric weight
        beta: float = 0.7,    # semantic weight
        threshold: float = 0.68
    )
    
    def add_user(self, user_id: str, images: List[str]) -> None
    def remove_user(self, user_id: str) -> bool
    def identify(self, image: str, k: int = 5) -> IdentifyResult
    def verify(self, image1: str, image2: str) -> VerifyResult
    def identify_frame(self, frame: np.ndarray) -> List[FaceResult]
    def save(self, path: str) -> None
    def load(self, path: str) -> None
    def get_user_count(self) -> int

GeometricExtractor

class GeometricExtractor:
    def extract(self, image: Union[str, np.ndarray]) -> np.ndarray
    def extract_from_landmarks(self, landmarks: np.ndarray) -> np.ndarray
    def get_feature_names(self) -> List[str]

GeometricTransformer

class GeometricTransformer(nn.Module):
    def __init__(
        self,
        d_model: int = 256,
        nhead: int = 8,
        num_layers: int = 4,
        dim_feedforward: int = 1024,
        dropout: float = 0.1,
        output_dim: int = 512
    )
    
    def forward(self, landmarks: torch.Tensor) -> torch.Tensor

๐Ÿ”ง Configuration

Environment Variables

# Model cache directory (default: ~/.gfram/models/)
export GFRAM_CACHE_DIR=/path/to/cache

# Server URL for model downloads
export GFRAM_SERVER_URL=https://gfram.uz

# Disable auto-update
export GFRAM_AUTO_UPDATE=0

Config File (~/.gfram/config.yaml)

model:
  version: "2.0.0"
  device: "cpu"
  
matching:
  alpha: 0.3
  beta: 0.7
  threshold: 0.68
  
hnsw:
  M: 16
  ef_construction: 200
  ef_search: 50

๐Ÿ”ฌ Research & Citation

If you use GFRAM in your research, please cite:

@article{gfram2024,
  title={GFRAM: Geometric Face Recognition through Advanced Mathematical 
         Descriptors and Transformer Architecture},
  author={Ortiqova, Feruza S.},
  journal={arXiv preprint arXiv:2024.xxxxx},
  year={2024},
  institution={Tashkent University of Information Technologies}
}

Key Publications

  1. GeometricTransformer: Novel transformer architecture for landmark-based face recognition
  2. Hybrid Matching: Combining geometric and semantic embeddings for improved accuracy
  3. Privacy-Preserving FR: Geometric-only data transmission for GDPR compliance

๐Ÿ“ Project Structure

gfram/
โ”œโ”€โ”€ __init__.py              # Public API exports
โ”œโ”€โ”€ core.py                  # GFRAMRecognizer main class
โ”œโ”€โ”€ detectors/
โ”‚   โ”œโ”€โ”€ retinaface.py        # Face detection
โ”‚   โ””โ”€โ”€ mediapipe.py         # 478 landmark extraction
โ”œโ”€โ”€ geometry/
โ”‚   โ”œโ”€โ”€ features.py          # All 150 features
โ”‚   โ”œโ”€โ”€ distances.py         # Euclidean distances (45)
โ”‚   โ”œโ”€โ”€ ratios.py            # Proportions (35)
โ”‚   โ”œโ”€โ”€ angles.py            # Angles (30)
โ”‚   โ”œโ”€โ”€ areas.py             # Areas (15)
โ”‚   โ”œโ”€โ”€ delaunay.py          # Triangulation (10)
โ”‚   โ”œโ”€โ”€ symmetry.py          # Symmetry (8)
โ”‚   โ”œโ”€โ”€ moments.py           # Hu moments (7)
โ”‚   โ””โ”€โ”€ normalize.py         # 3-step normalization
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ transformer.py       # GeometricTransformer
โ”‚   โ”œโ”€โ”€ positional.py        # 3D Positional Encoding
โ”‚   โ””โ”€โ”€ losses.py            # ArcFace, Triplet, InfoNCE
โ”œโ”€โ”€ matching/
โ”‚   โ”œโ”€โ”€ hnsw.py              # HNSW index wrapper
โ”‚   โ”œโ”€โ”€ similarity.py        # Hybrid similarity
โ”‚   โ””โ”€โ”€ memory.py            # Prototype Memory Bank
โ””โ”€โ”€ utils/
    โ”œโ”€โ”€ visualization.py     # Plotting utilities
    โ””โ”€โ”€ io.py                # File operations

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup

git clone https://github.com/feruza-42h/gfram.git
cd gfram
pip install -e ".[dev]"
pytest  # Run tests

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ™ Acknowledgments

  • MediaPipe team for excellent landmark detection
  • PyTorch community for deep learning framework
  • FAISS and hnswlib for efficient similarity search
  • InsightFace for benchmark datasets and evaluation protocols

๐Ÿ“ž Contact & Support


Made with โค๏ธ in Tashkent for the research community
GFRAM โ€” Where Geometry Meets Intelligence

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

gfram-3.0.0.tar.gz (61.9 kB view details)

Uploaded Source

Built Distribution

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

gfram-3.0.0-py3-none-any.whl (57.8 kB view details)

Uploaded Python 3

File details

Details for the file gfram-3.0.0.tar.gz.

File metadata

  • Download URL: gfram-3.0.0.tar.gz
  • Upload date:
  • Size: 61.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for gfram-3.0.0.tar.gz
Algorithm Hash digest
SHA256 718dac7072b49fb99872db1b1bf9028a5a57deb53280d9f7c67a5d18d0a4eb71
MD5 2c3dbe00ba9ee63f52ff81b67ec5fb6d
BLAKE2b-256 5ad8931de06bed91c7d04d52b667177f483c2e63d93544fc0ca4981ea80bb3f2

See more details on using hashes here.

File details

Details for the file gfram-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: gfram-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 57.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for gfram-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f16a3902b8c88bee7ba232af2d0f8d1a679ba0832115badda150660f046eab16
MD5 ea1f042e2d21c33985b11aed37a27628
BLAKE2b-256 47b58f7019c9bddca31b4f32303972ff8e11aea63b65bbca9dadc42aafb7b054

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