Computer Vision

Vehicle Re-Identification without a Database

Building an intelligent, image-based vehicle matching platform using YOLOv5 and OSNet-AIN with zero database dependencies.

Shubham
Shubham Kulkarni AI Engineer
Updated
Vehicle AI

The Vehicle Re-Identification System is a complete, standalone application for matching vehicles across images using state-of-the-art deep learning models. But unlike traditional systems that rely on heavy PostgreSQL or MongoDB instances to store metadata, this implementation operates entirely in-memory.

<100ms Search Latency
0 Databases Used
512-dim Embeddings

1. The Database Bottleneck

In typical computer vision pipelines, once an image is processed, the feature vectors (embeddings) are written to a database. For high-speed applications like toll booths or forensic searching, I/O database latency becomes the primary bottleneck.

No database is required here—it uses pure in-memory, real-time processing to perform vehicle forensic matching. This makes it incredibly fast, easy to deploy, and perfectly suited for stateless, ephemeral environments like serverless functions or lightweight Docker containers.

2. Architecture Overview

The architecture is based on a session-based Microservice design with 5 focused services ensuring clear separation of concerns. The FastAPI backend provides an async REST API with automatic documentation.

Instead of persisting data, the system groups uploads by a temporary session ID. As long as the session is alive in memory, users can run ultra-fast cross-matching on the uploaded images.

graph TD UI[Frontend Client] -->|Upload Images| API[FastAPI Gateway] API -->|Images| YOLO[YOLOv5 Detector] YOLO -->|Bounding Boxes| OSNet[OSNet-AIN Extractor] OSNet -->|512-dim Vectors| FAISS[(FAISS In-Memory Index)] UI -->|Search Image| API API -->|Extract Query Vector| OSNet OSNet -->|Search| FAISS FAISS -->|Top Matches & Scores| API API -->|Results| UI

3. YOLO & OSNet Embeddings

The Deep Learning Pipeline

  • Automated Vehicle Detection: YOLOv5 acts as the first pass, finding where the vehicles are in the frame and generating tight bounding boxes.
  • Deep Learning Embeddings: OSNet-AIN takes those cropped bounding boxes and generates 512-dimensional vectors representing visual features (color, make, model nuances).
  • Confidence Scoring: The system computes intelligent match classification (confident/probable/weak) based on the L2 distance of vectors.

4. FAISS Vector Search

For the actual matching, looping through arrays in Python is too slow. We utilize FAISS (Facebook AI Similarity Search). By maintaining an ephemeral FAISS index in memory, vector distance calculations happen in C++ underneath the hood, bringing search latency to under 100 milliseconds.

faiss_matcher.py
import faiss
import numpy as np

class InMemoryMatcher:
    def __init__(self, dimension=512):
        # L2 Distance Index for Euclidean distance search
        self.index = faiss.IndexFlatL2(dimension)
        self.metadata = []

    def add_vehicle(self, vector, image_id):
        """Add OSNet-AIN vector to FAISS index."""
        # FAISS expects a 2D float32 numpy array
        np_vector = np.array([vector], dtype=np.float32)
        self.index.add(np_vector)
        self.metadata.append(image_id)

    def search(self, query_vector, k=5):
        """Search for top K matches in < 10ms."""
        np_query = np.array([query_vector], dtype=np.float32)
        distances, indices = self.index.search(np_query, k)
        
        results = []
        for dist, idx in zip(distances[0], indices[0]):
            if idx != -1:
                results.append({
                    "image_id": self.metadata[idx],
                    "confidence_score": max(0, 100 - (dist * 10)) # Arbitrary scaling
                })
        return results

5. Tech Stack Summary

Backend: Python, FastAPI, PyTorch, YOLOv5, FAISS
Frontend: HTML5, CSS3, Vanilla JavaScript with Drag & Drop
Deployment: Docker, Docker Compose