Skip to content

UniFace: A Unified Face Analysis Library for Python | Detection, alignment, landmarks, face-mesh, recognition, parsing, gaze, attributes and anti-spoofing under one API.

10.4 KB
Raw Blame
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Face Search: One-to-Many Face Matching\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/yakhyo/uniface/blob/main/examples/04_face_search.ipynb) [![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://www.kaggle.com/code/yakhyokhuja/face-search-one-to-many-face-matching)\n",
    "\n",
    "Search a group photo for one known person by comparing embeddings.\n",
    "\n",
    "---\n",
    "\n",
    "**UniFace** is a lightweight, production-ready Python library for face detection, recognition, tracking, landmark analysis, face parsing, gaze estimation, and face attributes.\n",
    "\n",
    "GitHub: [github.com/yakhyo/uniface](https://github.com/yakhyo/uniface) | Docs: [yakhyo.github.io/uniface](https://yakhyo.github.io/uniface)\n",
    "\n",
    "## Setup\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -q \"uniface[cpu]\"\n",
    "\n",
    "# Clone repo for assets (Colab / Kaggle only)\n",
    "import os\n",
    "if any(k in os.environ for k in ('COLAB_GPU', 'COLAB_RELEASE_TAG', 'KAGGLE_KERNEL_RUN_TYPE')):\n",
    "    if not os.path.exists('uniface'):\n",
    "        !git clone --depth 1 https://github.com/yakhyo/uniface.git\n",
    "    os.chdir('uniface/examples')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import uniface\n",
    "from uniface.analyzer import FaceAnalyzer\n",
    "from uniface.detection import RetinaFace\n",
    "from uniface.recognition import ArcFace\n",
    "\n",
    "print(uniface.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "`FaceAnalyzer` bundles the detector and the recognizer, so one `analyze()` call\n",
    "returns faces that already carry embeddings.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "analyzer = FaceAnalyzer(\n",
    "    detector=RetinaFace(confidence_threshold=0.5),\n",
    "    recognizer=ArcFace()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Embed the Face We Are Looking For\n",
    "\n",
    "The query only needs embedding once. Everything after this is a comparison against it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Einstein's photo\n",
    "einstein_path = '../assets/source/verify_einstein_1947.jpg'\n",
    "einstein_image = cv2.imread(einstein_path)\n",
    "\n",
    "# Get Einstein's face features\n",
    "einstein_faces = analyzer.analyze(einstein_image)\n",
    "\n",
    "if einstein_faces:\n",
    "    einstein_face = einstein_faces[0]\n",
    "    print(f'Detected {len(einstein_faces)} face with {einstein_face.embedding.shape[0]}D features')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Find Every Face in the Group Photo\n",
    "\n",
    "The same call on the group photo gives one embedding per person."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the group photo\n",
    "group_photo_path = '../assets/source/detect_crowd.jpg'\n",
    "group_photo = cv2.imread(group_photo_path)\n",
    "\n",
    "# Find all faces in the group photo\n",
    "group_faces = analyzer.analyze(group_photo)\n",
    "print(f'Detected {len(group_faces)} people in the group photo')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Look at Both Inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(1, 2, figsize=(15, 6))\n",
    "\n",
    "# Display Einstein's photo\n",
    "axes[0].imshow(cv2.cvtColor(einstein_image, cv2.COLOR_BGR2RGB))\n",
    "axes[0].set_title(\"Who we're looking for: Einstein\", fontsize=14, fontweight='bold')\n",
    "axes[0].axis('off')\n",
    "\n",
    "# Display the group photo\n",
    "axes[1].imshow(cv2.cvtColor(group_photo, cv2.COLOR_BGR2RGB))\n",
    "axes[1].set_title(f'Where we search: Group of {len(group_faces)} scientists', fontsize=14, fontweight='bold')\n",
    "axes[1].axis('off')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Compare and Rank\n",
    "\n",
    "`compute_similarity()` is a dot product between two normalised embeddings, so it is the\n",
    "cosine similarity. Sorting descending puts the likeliest person first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not einstein_faces or not group_faces:\n",
    "    print('Error: Could not detect faces')\n",
    "else:\n",
    "    # Compare Einstein with each person in the group\n",
    "    matches = []\n",
    "    for i, person in enumerate(group_faces):\n",
    "        similarity = einstein_face.compute_similarity(person)\n",
    "        matches.append((i, similarity))\n",
    "\n",
    "    # Sort by similarity (best matches first)\n",
    "    matches.sort(key=lambda x: x[1], reverse=True)\n",
    "\n",
    "    # Show top 5 matches\n",
    "    print('Top 5 most similar people:')\n",
    "    for rank, (person_idx, similarity) in enumerate(matches[:5], 1):\n",
    "        print(f'{rank}. Person #{person_idx + 1}: similarity = {similarity:.4f}')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Mark the Winner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if einstein_faces and group_faces:\n",
    "    # Get the best match\n",
    "    best_match_idx, best_similarity = matches[0]\n",
    "\n",
    "    # Draw bounding boxes\n",
    "    result_image = group_photo.copy()\n",
    "\n",
    "    for i, person in enumerate(group_faces):\n",
    "        bbox = person.bbox.astype(int)\n",
    "\n",
    "        if i == best_match_idx:\n",
    "            color = (0, 255, 0)\n",
    "            thickness = 3\n",
    "        else:\n",
    "            color = (128, 128, 128)\n",
    "            thickness = 1\n",
    "\n",
    "        cv2.rectangle(result_image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, thickness)\n",
    "\n",
    "        if i == best_match_idx:\n",
    "            label = f'Match: {best_similarity:.3f}'\n",
    "            cv2.putText(result_image, label, (bbox[0], bbox[1] - 10),\n",
    "                       cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)\n",
    "\n",
    "    plt.figure(figsize=(15, 10))\n",
    "    plt.imshow(cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB))\n",
    "    plt.title(f'Best match: Person #{best_match_idx + 1}', fontsize=14)\n",
    "    plt.axis('off')\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Inspect the Top Three\n",
    "\n",
    "Seeing the runners-up is how you tell a confident match from a lucky one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if einstein_faces and group_faces:\n",
    "    # Show top 3 matches\n",
    "    top_k = min(3, len(matches))\n",
    "\n",
    "    fig, axes = plt.subplots(1, top_k + 1, figsize=(16, 4))\n",
    "\n",
    "    # Show Einstein's face\n",
    "    einstein_rgb = cv2.cvtColor(einstein_image, cv2.COLOR_BGR2RGB)\n",
    "    axes[0].imshow(einstein_rgb)\n",
    "    axes[0].set_title(\"Query\", fontsize=12)\n",
    "    axes[0].axis('off')\n",
    "\n",
    "    # Show top 3 matches from the group\n",
    "    for i, (person_idx, similarity) in enumerate(matches[:top_k]):\n",
    "        person = group_faces[person_idx]\n",
    "        bbox = person.bbox.astype(int)\n",
    "\n",
    "        # Crop this person's face\n",
    "        face_crop = group_photo[bbox[1]:bbox[3], bbox[0]:bbox[2]]\n",
    "\n",
    "        if face_crop.size > 0:\n",
    "            face_rgb = cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB)\n",
    "            axes[i + 1].imshow(face_rgb)\n",
    "            axes[i + 1].set_title(f'Match {i + 1}: {similarity:.3f}', fontsize=12)\n",
    "            axes[i + 1].axis('off')\n",
    "\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Decide With a Threshold\n",
    "\n",
    "Ranking alone always returns a best match, even when the person is absent. A threshold is\n",
    "what turns the ranking into a yes or no answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0.4 is the library default, the value FAISS.search uses\n",
    "THRESHOLD = 0.4\n",
    "\n",
    "if einstein_faces and group_faces:\n",
    "    best_match_idx, best_similarity = matches[0]\n",
    "\n",
    "    print(f'Best match: Person #{best_match_idx + 1}')\n",
    "    print(f'Similarity: {best_similarity:.4f}')\n",
    "    print(f'Threshold: {THRESHOLD}')\n",
    "\n",
    "    if best_similarity > THRESHOLD:\n",
    "        print(f'Result: Match found (Einstein is person #{best_match_idx + 1})')\n",
    "    else:\n",
    "        print(f'Result: No match (similarity below threshold)')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notes\n",
    "\n",
    "- Ranking always produces a best match, including when the person is absent. The threshold is\n",
    "  what turns a ranking into a yes or no.\n",
    "- Past a few hundred faces, replace this loop with an index. Notebook 10 shows that."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}