================================================================================
MAJOR PROJECT: CUSTOM IDENTIFICATION MODULES - TECHNICAL REFERENCE
================================================================================
This document provides a comprehensive technical breakdown of the two custom Python
modules developed for the Multi-Object Tracking (MOT) project. It is designed to
explain the "Inner Workings" of the system for your project report and technical
defense.
--------------------------------------------------------------------------------
MODULE 1: generate_gallery.py (The "Memory System")
--------------------------------------------------------------------------------
FILE PATH: /Major/generate_gallery.py
[1] PURPOSE:
This script is responsible for "Learning" the identities. It converts raw images of
people into mathematical feature vectors (embeddings) that the AI can understand.
It acts as the "Enrollment" phase of the biometric system.
[2] INPUT & OUTPUT:
- INPUT: A directory path containing folders of person images.
Structure: dataset/
├── Chetan/ [[Link], [Link]...]
├── Ramu/ [[Link], [Link]...]
- OUTPUT: A serialized Pickle file (`[Link]`) containing a dictionary.
Format: { "Chetan": [Vector_1, Vector_2...], "Ramu": [Vector_1...] }
[3] TECHNICAL LOGIC (STEP-BY-STEP):
1. **Device Setup**: Automatically selects GPU (CUDA) if available for fast
processing, or CPU otherwise.
2. **Model Initialization**: Loads the `ReIDDetectMultiBackend` model (OSNet
architecture) which is pre-trained to extract 512-dimensional feature vectors
describing human appearance.
3. **Image Processing Loop**:
- Iterates through every sub-folder (Person Name).
- Reads every image file using OpenCV (`cv2`).
- **Preprocessing**: The model expects images in a specific tensor format.
The script handles resizing and batching.
- **Feature Extraction**: `model(img)` runs the image through the Neural
Network layers. The output is a raw 512-float vector.
4. **The "Multi-Look" Strategy (CRITICAL INNOVATION)**:
- Standard Approach: Average all vectors into one. (Bad, blurs details).
- **Our Approach**: We append EVERY vector to a list.
- *Benefit*: Preserves distinct "Looks" (Front View, Back View, Red Shirt
Look, etc.).
5. **L2 Normalization**:
- Formula: Vector / ||Vector||
- Why? Raw vectors have arbitrary magnitudes. Normalization forces them onto
a "Unit Hypersphere". This ensures that the math we use later (Cosine Distance) is
mathematically valid.
6. **Serialization**: Dumps the final dictionary to disk using `pickle`.
[4] KEY CODE SNIPPET (EXPLAINED):
# Iterate through all images of 'Chetan'
for img_path in images:
# Run AI Model
feat = model([img])
# Store List of Vectors (Multi-Look)
normalized_features = []
for feat in features_list:
# Normalize each vector individually
norm_feat = feat / [Link](feat)
normalized_features.append(norm_feat)
gallery[person_name] = normalized_features
--------------------------------------------------------------------------------
MODULE 2: [Link] (The "Real-Time Brain")
--------------------------------------------------------------------------------
FILE PATH: /Major/[Link]
[1] PURPOSE:
This is the main execution script. It orchestrates three massive components:
1. Object Detection (YOLOv9)
2. Object Tracking (StrongSORT)
3. **Custom Person Identification (Your Logic)**
[2] THE PIPELINE FLOW:
Frame Input -> YOLOv9 (Finds Box) -> StrongSORT (Assigns Stick-Figure ID) -> ReID
Extract (Get Vector) -> **YOUR CODE** -> Final Display
[3] THE IDENTIFICATION ALGORITHM (DEEP DIVE):
We injected a custom matching block inside the main tracking loop. This logic runs
for *every tracked person* in *every frame*.
ALGORITHM STEPS:
1. **Retrieve Live Feature**: Get the feature vector of the person currently being
tracked in the video (`[Link][-1]`).
2. **Gallery Search**:
- We loop through the `[Link]` we loaded at startup.
- We compare the "Live Person" against "Chetan (Front)", "Chetan (Back)",
"Ramu (Front)", etc.
3. **Distance Calculation (Cosine Distance)**:
- Formula: `Distance = 1.0 - Dot_Product(Live_Vector, Gallery_Vector)`
- Since vectors are normalized, Dot Product = Cosine Similarity (-1 to 1).
- Distance range: 0 (Identical) to 2 (Opposite).
- Typical "Same Person" distance: 0.0 to 0.15.
- Typical "Different Person" distance: 0.25 to 1.0.
4. **Minimum Distance Logic**:
- We find the *closest single match* across all looks of all people.
5. **The Threshold Gate (0.1)**:
- If `Min_Distance < 0.1`: We are >95% confident it is them. -> **ASSIGN
NAME**.
- If `Min_Distance >= 0.1`: We are unsure. -> **REJECT MATCH**.
[4] KEY CODE SNIPPET (EXPLAINED):
# Loop through every person in the Gallery
for name, g_feats in gallery_dict.items():
# Check against ALL looks (Front, Back, Side...)
for g_feat in g_feats:
# Calculate distance
current_dist = 1.0 - [Link](feat, g_feat)
# Keep track of the absolute best match found so far
if current_dist < person_min_dist:
person_min_dist = current_dist
# The Decision Gate
if best_name and min_dist < 0.1:
# SUCCESS: Overwrite the generic ID with the Name
id_map[t.track_id] = best_name
--------------------------------------------------------------------------------
PROJECT DEFENSE: "WHY IS THIS GOOD?"
--------------------------------------------------------------------------------
Q: Why use ReID instead of Face Recognition?
A:
1. **Viewpoint Invariant**: Face Recognition fails from the back or side. Our
system works 360 degrees because it creates a "Full Body Signature" (Multi-Look).
2. **Long Range**: Face ID needs high-res closeups. ReID works on low-res CCTV
footage.
3. **Non-Intrusive**: Subjects don't need to stop and look at the camera.
Q: How do you handle computation speed with many images?
A:
The heavy lifting is done by the Neural Networks (YOLO/OSNet). The matching logic
(dot products) takes microseconds. Even if the gallery has 1000 images, the
matching takes less than 1ms. The system is essentially O(N) where N is gallery
size, but with extremely small constants.