DriverTrac/docs/FIX_404_ERROR.md
2025-11-28 09:08:33 +05:30

2.8 KiB

Fix for 404 Error - OpenCV Model Download

Problem

The OpenCV fallback detector was trying to download face detection models from URLs that returned 404 errors:

urllib.error.HTTPError: HTTP Error 404: Not Found

Root Cause

The original implementation attempted to download OpenCV DNN models from GitHub URLs that are no longer available or have moved.

Solution

Replaced with OpenCV's Built-in Haar Cascade Classifier

  • No downloads required - Uses OpenCV's built-in face detection
  • Always available - Included with OpenCV installation
  • More reliable - No dependency on external URLs
  • Faster initialization - No network requests

What Changed

Before:

  • Tried to download models from GitHub (404 errors)
  • Required external dependencies
  • Failed if URLs were unavailable

After:

  • Uses cv2.data.haarcascades (built-in)
  • No network requests
  • Works offline
  • More reliable

Technical Details

The fix changes the face detector from:

# OLD: Download DNN models
urllib.request.urlretrieve(FACE_DETECTION_MODEL_URL, face_model)
self.face_net = cv2.dnn.readNetFromTensorflow(...)

To:

# NEW: Use built-in Haar Cascade
cascade_path = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
self.face_cascade = cv2.CascadeClassifier(cascade_path)

Verification

The fix ensures:

  1. No HTTP requests for model downloads
  2. Uses OpenCV's built-in resources
  3. Works offline
  4. Compatible with all OpenCV installations
  5. Maintains MediaPipe-compatible interface

Performance

  • Haar Cascade: Slightly faster than DNN for face detection
  • Accuracy: Good for face detection (suitable for POC)
  • Memory: Lower memory usage than DNN models

Testing

To verify the fix works:

# Activate virtual environment
source venv/bin/activate

# Test the import
python3 -c "from src.face_pose_detector import get_face_detector; print('OK')"

# Run the application
streamlit run src/poc_demo.py

Expected Behavior

When MediaPipe is not available:

  1. Code detects MediaPipe import failure
  2. Automatically uses OpenCV Haar Cascade
  3. Logs: "✓ OpenCV Face Detector loaded (Haar Cascade)"
  4. Application runs normally

Notes

  • Haar Cascade is less accurate than MediaPipe for detailed face landmarks
  • However, it's sufficient for:
    • Face presence detection
    • Basic face tracking
    • Driver absent detection
    • Simplified PERCLOS calculation

For production, MediaPipe is still recommended for better accuracy.

Summary

Fixed: 404 error eliminated Improved: No external dependencies Reliable: Works with standard OpenCV installation Compatible: Maintains same interface as MediaPipe

The application should now work without any download errors!