Skip to content

fjkrch/Mylidaar

Repository files navigation

Mylidaar

Real-time monocular depth estimation and face distance measurement from a single webcam. The system runs Depth Anything V2 per frame to produce a dense relative depth map, detects faces via OpenCV Haar Cascade, and computes an absolute distance estimate in centimeters using a pinhole camera model. The two signals are independent: the pinhole model drives the on-screen distance readout, and the neural network drives the dense depth visualization and point cloud export. When saving a point cloud, the pinhole distance can optionally anchor the relative depth to approximate metric scale.


Public Demo Notice

This repository is a sanitized, public subset of a larger internal prototype. It contains the core inference and visualization pipeline and is sufficient to run the demo end-to-end.

Included:

  • Webcam depth estimation with live face distance overlay (run_camera.py)
  • Point cloud export and visualization (run_pointcloud.py)
  • Automatic model weight download from Hugging Face (download_model.py)
  • Depth Anything V2 model code (DINOv2 backbone + DPT head)

Not included:

  • Internal evaluation datasets and ground-truth annotations
  • Benchmark and accuracy measurement scripts
  • Deployment configurations and hardware-specific tuning profiles
  • Extended calibration procedures and validation logs

Features

  • Per-frame dense relative depth inference via Depth Anything V2 (ViT-S, ViT-B, or ViT-L encoder)
  • Face detection using OpenCV Haar Cascade
  • Pinhole camera model distance estimate (cm), displayed as a live overlay
  • Side-by-side display of camera feed and colormapped depth map
  • Point cloud export to ASCII PLY (raw relative depth, or heuristically scaled to approximate cm when a face is detected)
  • Automatic model weight download from Hugging Face on first run
  • Device selection: CUDA, MPS, or CPU (automatic fallback)

How It Works

Pinhole Distance Estimate

For each detected face:

Z = (f * L) / l_px
Symbol Description
Z Estimated distance from camera (cm)
f Focal length in pixels (user-provided, default 800)
L Assumed real face width in cm (user-provided, default 13)
l_px Detected face bounding box width in pixels

This assumes a roughly frontal face and a known average face width. No lens distortion correction is applied.

Neural Depth Map

Depth Anything V2 produces a dense relative depth map from a single RGB frame. The output values are unitless and relative within each frame -- the model does not produce metric depth. The architecture is a DINOv2 Vision Transformer backbone with a DPT head.

Point Cloud Scaling

Pressing s always saves a raw point cloud (depth_points.ply). If a face is detected at that moment, a second file (depth_points_scaled.ply) is produced by using the pinhole distance to convert relative depth into approximate centimeters. The conversion uses IQR-based outlier filtering and is heuristic -- it is not validated against ground truth in this release.


Repository Structure

Mylidaar/
├── run_camera.py            Webcam depth estimation and face distance overlay
├── run_pointcloud.py        PLY point cloud visualization (matplotlib + Open3D)
├── download_model.py        Model weight downloader (Hugging Face)
├── test.py                  Prints torch.cuda.is_available()
├── requirements.txt         Python dependencies
├── depth_anything_v2/       Depth Anything V2 model implementation
│   ├── dpt.py               DPT head and DepthAnythingV2 module
│   ├── dinov2.py            DINOv2 ViT backbone
│   ├── dinov2_layers/       Attention, MLP, patch embedding, etc.
│   └── util/                Resize, normalization, feature fusion blocks
├── checkpoints/             Model weights (auto-downloaded, gitignored)
├── assets/                  Demo screenshots
└── picture/

Requirements

Python 3.10+. A CUDA-capable GPU is recommended for real-time inference; the code falls back to MPS or CPU automatically.

Key dependencies: torch, torchvision, opencv-python, matplotlib, open3d. requirements.txt currently includes packages not used by the public scripts in this release.

Webcam capture on Windows uses DirectShow (cv2.CAP_DSHOW). Other platforms may require backend changes.


Installation

python -m venv .venv

# Windows PowerShell
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activate

pip install -r requirements.txt

Model weights are downloaded automatically on first run. To download manually:

python download_model.py                  # ViT-S (default, ~95 MB)
python download_model.py --encoder vitb   # ViT-B (~390 MB)
python download_model.py --encoder vitl   # ViT-L (~1.3 GB)
python download_model.py --all            # All encoders

Weights are saved to ./checkpoints/ from Hugging Face.


Usage

Webcam depth and face distance

python run_camera.py

Opens the webcam, runs depth inference each frame, and displays a side-by-side window (camera feed with face annotations | colormapped depth map).

Keyboard controls:

  • q -- quit
  • s -- save depth_points.ply (always) and depth_points_scaled.ply (if a face is visible)

Point cloud visualization

python run_pointcloud.py

Reads depth_points.ply from the working directory. Opens a matplotlib 3D scatter plot, then an Open3D interactive viewer. The filename is hardcoded; no arguments are accepted.

CUDA check

python test.py

Command-Line Arguments

All arguments below apply to run_camera.py. download_model.py accepts --encoder and --all.

Argument Type Default Description
--camera int 1 Camera device index. 0 is typically the built-in webcam.
--encoder str vits Depth model encoder: vits (small/fast), vitb (base), vitl (large).
--focal-length float 800.0 Camera focal length in pixels. Affects pinhole distance calculation.
--face-width float 13.0 Assumed real face width in cm.
--input-size int 518 Input resolution for depth inference (multiples of 14 enforced internally).
--grayscale flag off Grayscale depth map instead of color.
--pred-only flag off Parsed but not implemented. Side-by-side view is always shown.

Focal length calibration

Place a face at a measured distance D (cm), read the detected width w (px), then compute: f = (w * D) / face_width_cm.


Output

Live window: Camera feed (left) with face bounding box and distance label, alongside a colormapped depth map (right).

Point cloud files: ASCII PLY with float X, Y, Z. The raw file stores pixel coordinates and unnormalized AI depth. The scaled file (when produced) maps Z to approximate centimeters and records the scaling parameters as a PLY comment.

Screenshots

Webcam depth and face distance

3D point cloud visualization


Limitations

  • This is a demonstration release, not a production system or research benchmark package.
  • Depth Anything V2 produces relative depth only. All metric scaling is approximate, conditional on face detection, and dependent on user-supplied camera parameters.
  • The pinhole model assumes a frontal face and a fixed average face width. Accuracy degrades with non-frontal poses, partial occlusions, and uncalibrated focal lengths.
  • Face detection uses a Haar Cascade, which is prone to false negatives at distance or under poor lighting.
  • Point cloud scaling is heuristic and has not been validated against ground truth in this release.
  • The --pred-only flag is parsed but has no effect.
  • requirements.txt currently includes packages not used by the public scripts in this release.
  • No temporal smoothing is applied. Frame-to-frame jitter is expected.
  • All depth is inferred from a single monocular RGB stream. No stereo, LiDAR, or multi-camera reference is used.

Internal Validation Note

Some quantitative results referenced in external materials were obtained using internal datasets and evaluation tooling that are not included in this public release. Those results are not reproducible from this repository alone.


Future Improvements

  • Replace Haar Cascade with a more robust face detector (e.g., MediaPipe, RetinaFace)
  • Integrate a metric depth model to reduce dependence on pinhole anchoring
  • Add temporal smoothing for distance estimates across frames
  • Implement the --pred-only display mode
  • Clean up unused dependencies in requirements.txt
  • Support non-Windows capture backends explicitly
  • Add colored point cloud export (RGB from camera frame)

License

No LICENSE file is currently included. This repository is provided as-is for viewing and educational reference. Reuse, redistribution, or derivative works are not explicitly licensed. If you require a formal license for use beyond inspection, please open an issue.

The DINOv2 backbone code under depth_anything_v2/ is derived from Meta Platforms, Inc. and is licensed under the Apache License 2.0.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages