Face Recognition Hub — (Full Deep Explanation)

What this page is

This is NOT just a navigation page anymore. This is a complete teaching document. By the time you finish reading, you will:

Quick Links

Open Capture Open Recognition JS Files and Links

SYSTEM FLOW (IMPORTANT)

Step 1: Capture faces → capture.html

Step 2: Export JSON + images

Step 3: Place files correctly

Step 4: Run recognition → recognize.html

Everything depends on correct training data. If training fails → recognition will ALWAYS show "Unknown".

JAVASCRIPT FILES / LIBRARIES USED, WHY THEY MATTER, AND LINKS

This project is mostly powered by JavaScript running directly in the browser. Some functionality comes from your own inline JavaScript inside capture.html and recognize.html, and some functionality comes from external JavaScript libraries loaded through script tags. This section lists the important JavaScript sources, explains why each one is important, and gives the direct links used in the project.

1. face-api.js library

This is the main AI-related JavaScript library used by both capture.html and recognize.html. It provides the actual face detection, landmark extraction, face descriptor generation, and matching support. Without this file, the project cannot detect faces or recognize anyone.

In capture.html, it is used mainly for detecting a face box so that the face crop can be saved properly.

In recognize.html, it is used for:

  • detecting faces in live video
  • finding facial landmarks
  • creating face descriptors
  • building labeled descriptors for known people
  • matching live faces against trained people

2. Inline JavaScript inside capture.html

This is not loaded as a separate external file, but it is still one of the most important JavaScript parts of the project. It contains your custom logic for the capture workflow.

Its importance is very high because it handles:

  • button event handlers such as Start, Stop, Save, Export, and Download
  • camera startup through getUserMedia()
  • face detection loop for capture mode
  • cropping the detected face from the live frame
  • saving captured faces in localStorage
  • building the exported faces.json file
  • rendering the capture gallery

Without this JavaScript, the library alone would not be enough. The page would have AI capability available, but no app behavior, no storage, and no export logic.

3. Inline JavaScript inside recognize.html

This is the custom JavaScript that turns the recognition page into a full live recognition tool.

Its importance is also extremely high because it controls:

  • loading the face models
  • loading data/faces.json
  • loading the training images listed inside the JSON file
  • building the FaceMatcher
  • starting and stopping the camera
  • switching between cameras
  • running the live detection loop
  • drawing rectangles and labels
  • writing logs to the debug panel
  • showing recognized names in the text area

This file is what connects the AI library with your actual recognition app behavior. Without it, there would be no training reload system, no live HUD, no logs, and no real recognition workflow.

4. Browser camera API support used by your JavaScript

This is not an external file, but it is one of the most important browser JavaScript capabilities used by both pages. The key API is: navigator.mediaDevices.getUserMedia()

It is important because it gives the web page access to the camera. Without this browser API, neither capture nor recognition can work.

Your Start Camera event handlers in both pages depend on this API first. If camera access fails, almost the whole workflow stops immediately.

5. Browser Canvas API support used by your JavaScript

Your project also relies heavily on HTML canvas through JavaScript. Both pages use canvas drawing contexts for important visual and image work.

In capture.html, canvas is important for:

  • drawing the red face rectangle overlay
  • copying the current video frame
  • cropping the face region
  • exporting cropped face images as JPEG data

In recognize.html, canvas is important for:

  • drawing face boxes
  • drawing labels above faces
  • handling mirrored drawing for the live preview

6. Local storage support used by capture.html

The capture page uses browser storage through JavaScript: localStorage

This is important because the page stores captured face images and metadata locally before export. That means the user can save several faces, refresh the gallery, and then export later.

Without localStorage in the current design, the user would lose the captured training set on refresh unless a backend API were added.

7. Fetch API used by recognize.html

The recognition page uses the browser Fetch API to load data/faces.json and also to load the referenced training images.

This is important because the recognition page does not hardcode the people and images directly into JavaScript. Instead, it reads them dynamically from files, which makes the system flexible and easier to maintain.

Without fetch, the page could not load your JSON config or external training images in the current architecture.

8. Optional note about AdSense script

Your pages also include the AdSense script tag. This script is not part of the face capture or face recognition logic, but it is still a JavaScript file included in the page.

Its role is monetization and ad loading, not face processing. So it is present, but it is not a functional dependency for the recognition workflow itself.

In short, the most important functional JavaScript source in this project is face-api.js, but your own inline JavaScript inside capture.html and recognize.html is what actually turns that library into a working application. The browser APIs such as getUserMedia, Canvas, fetch, and localStorage are also essential because they provide camera access, image processing, file loading, and local persistence.

RECOGNIZE.HTML — COMPLETE LINE BY LINE

We will start from the MOST IMPORTANT part:

1. Start Camera Button

btnStartCam.addEventListener("click", startCamera);

This line connects button → function. When user clicks button → startCamera() runs.

2. startCamera Function

async function startCamera() {

Async because we use await (camera + promises).

---
stream = await navigator.mediaDevices.getUserMedia({
  video: { facingMode: facing },
  audio: false,
});

CRITICAL LINE

This asks browser: "Give me camera stream"

If user denies → error If HTTP (not HTTPS) → fails

---
video.srcObject = stream;

Attach camera to video element

---
await video.play();

Start video playback

---
running = true;

System state → camera is active

---
loop();

Starts infinite detection loop

---

3. Detection Loop

async function loop() {
  if (!running) return;

  await detectFrame();
  draw();

  requestAnimationFrame(loop);
}

This runs continuously like a game loop.

Flow:

---

4. detectFrame()

const dets = await faceapi
  .detectAllFaces(video, new faceapi.TinyFaceDetectorOptions())
  .withFaceLandmarks()
  .withFaceDescriptors();

This is the CORE AI step.

It does:

---

5. draw()

ctx.strokeRect(b.x, b.y, b.width, b.height);

Draws red box on face

---
const best = matcher.findBestMatch(d.descriptor);

COMPARES FACE WITH TRAINING DATA

---
label = best.label;

Final output → person's name

---

6. Training System

matcher = new faceapi.FaceMatcher(labeled, 0.55);

0.55 = strictness Lower = stricter Higher = more lenient

---

7. faces.json Loading

const res = await fetch("data/faces.json");

Loads training config

---

8. Training Loop

for (const person of people)

For each person → load images → extract descriptors

---

IMPORTANT CONCEPT

Recognition works like:

CAPTURE.HTML — COMPLETE LOGIC

1. Start Camera

Same as recognize — uses:
getUserMedia()
---

2. Save Face

When user clicks "Save":

---

3. Data Structure

{
  name: "Champak",
  files: [
    "images/faces/champak-1.jpg"
  ]
}
---

4. Export JSON

Capture page groups all saved faces and creates: faces.json

DEBUGGING GUIDE (VERY IMPORTANT)

Problem: Unknown faces

Means → training failed

---

Problem: No rectangles

Means → detection failed

---

Problem: Camera not starting

Check:

---

Problem: faces.json error

Check:

FINAL UNDERSTANDING

Think of system like this:

Capture = Teaching Recognition = Testing

No teaching → no recognition.