All projects

Technical Report · ML & Computer Vision

3D Object Detection and Tracking Illusion

Real-time computer-vision and 3D-graphics experiment that tracks a face/webcam target and updates an OpenGL scene to create a viewpoint-dependent depth illusion.

AUTHOR  Gaurav Verma CATEGORY  ML & Computer Vision SOURCE  https://github.com/GAURAV-321/3D-Object-Detection-and-Tracking-Illusion DATE  Mar 2021 STATUS  published
View Source Python OpenCV OpenGL Pygame Tracking

Abstract

A compact Python application that fuses OpenCV face tracking with an OpenGL 3D scene, mapping 2D webcam coordinates to a 3D camera transform so the rendered geometry appears to shift with the viewer's head position. The interesting part is the real-time coupling of two independent loops—vision and graphics—where latency and coordinate normalization directly determine whether the illusion holds.

1. What This Is

Shipped in March 2021 as a portfolio experiment, this project captures a live webcam feed, detects and tracks a face (or configured visual target), and feeds the normalized 2D position into an OpenGL scene rendered through Pygame. The 3D content—a chair/shelf-style arrangement—parallaxes in response to the tracked target, producing a viewpoint-dependent depth illusion.

The repository provides multiple run modes for experimenting with different detection and tracking behaviors, making it a small but complete integration study rather than a single-script demo.

2. How It Works

Each frame follows a fixed five-stage pipeline. The webcam is opened once; after that the loop is capture, detect, normalize, transform, render, repeated at the display refresh rate.

# Stage Input Tool Output
01 Capture Webcam device OpenCV VideoCapture BGR frame
02 Detect / Track Current frame OpenCV face detector Bounding-box coordinates
03 Normalize Pixel coordinates Python arithmetic Unit-range (x, y) offset
04 Transform Normalized offset OpenGL matrix math Updated camera or scene matrix
05 Render Scene + camera matrix Pygame + OpenGL Displayed 3D frame

3. Implementation Notes

3.1 Coordinate mapping

The core trick is converting a 2D pixel position (face center in the webcam frame) into a 3D camera or scene-graph offset. The mapping is a simple normalization—divide by frame width and height, center at zero—then scale to a rotation or translation range that looks natural. No projective geometry or depth estimation is involved; the illusion is purely perceptual.

3.2 Dual-loop integration

OpenCV capture, face detection, and OpenGL rendering all run in the same Python main loop, with Pygame providing the window and event handling. There is no separate thread or process for vision; the detection pass and the render pass are sequential within one iteration. This keeps the code simple but couples the two latencies directly.

3.3 Run modes

The repository exposes multiple run modes so the detection and tracking behavior can be swapped without editing the render loop. This makes it easy to compare, for example, a static face-cascade pass against a tracking pass, and to isolate whether the visual result comes from detection accuracy or from the coordinate mapping itself.

4. Constraints

  • Single-threaded loop

    Detection and rendering share one thread. A slow detection pass (low light, partial occlusion) stalls the next OpenGL frame, producing visible hitches in the illusion.

  • Cascade-level detection

    The face detector is OpenCV's built-in cascade, not a learned model. Accuracy degrades noticeably with unusual lighting, extreme angles, or partial occlusion, which breaks tracking and freezes the 3D view.

  • Static scene assets

    The 3D content is a fixed set of meshes (chair/shelf geometry). There is no scene-graph abstraction, asset streaming, or dynamic content loading—swapping the scene means editing the render code directly.

  • No error recovery

    If the camera disconnects or the target is lost, the loop has no graceful fallback. The application either crashes or renders a stale frame until the next valid detection.

5. Next

  1. a. Move face detection into a background thread with a shared latest-frame buffer, decoupling vision latency from the render frame time.
  2. b. Replace the cascade detector with a lightweight DNN-based face detector for better robustness under varied lighting and angles.
  3. c. Introduce a minimal scene-graph layer so 3D content can be swapped or extended without modifying the tracking or coordinate-mapping code.

— end of report —