Skip to main content

Drowsiness detection using facial landmarks or CNN.

Project description

Drowsiness Validator

A Python package for detecting drowsiness using facial landmarks or a Convolutional Neural Network (CNN).

Installation

Install the package using pip:

pip install drowsiness-validator

Usage

Here's a basic example of how to use the package:

from drowsiness_validator import detect_drowsiness

# Example with image path
test_img_path = 'path/to/your/image.jpg'
result = detect_drowsiness(image_path=test_img_path, method='cnn')
print(result)
# Example output: {'prediction': 1, 'status': 'Drowsy', 'method': 'cnn'}

# Example with base64 image
import base64
with open(test_img_path, 'rb') as f:
    img_b64 = base64.b64encode(f.read()).decode('utf-8')
result = detect_drowsiness(image_base64=img_b64, method='cnn')
print(result)
# Example output: {'prediction': 1, 'status': 'Drowsy', 'method': 'cnn'}

Flask Route Example

Here is an example of a Flask route to check drowsiness from a base64 image:

from flask import Flask, request, jsonify
from drowsiness_validator import detect_drowsiness

app = Flask(__name__)

@app.route('/check-drowsiness', methods=['POST'])
def check_drowsiness():
    data = request.json
    if 'image_base64' not in data:
        return jsonify({'error': 'image_base64 is required'}), 400

    image_base64 = data['image_base64']
    result = detect_drowsiness(image_base64=image_base64, method='cnn')
    return jsonify(result)

if __name__ == '__main__':
    app.run(debug=True)

Documentation

detect_drowsiness

Detects drowsiness from an image using either CNN or facial aspect ratios (via Random Forest).

Args:

  • image_path (str, optional): Path to the input image file. Defaults to None.
  • image_base64 (str, optional): Base64 encoded string of the input image. Defaults to None.
  • method (str, optional): Method for detection ('cnn' or 'aspect_ratio'). Defaults to 'cnn'.
  • force_train_cnn (bool, optional): Whether to force retraining of the CNN model. Defaults to False.
  • train_dir (str, optional): Path to the training data directory (needed if force_train_cnn=True).
  • test_dir (str, optional): Path to the test data directory (needed if force_train_cnn=True).

Returns:

  • dict: A dictionary containing the prediction result and method used.

Example Response:

{
  "prediction": 1,
  "status": "Drowsy",
  "method": "cnn"
}

calculate_ARs

Calculates aspect ratios from an image.

Args:

  • image_path (str, optional): Path to the input image file. Defaults to None.
  • image_data (numpy array, optional): Image data. Defaults to None.

Returns:

  • dict: A dictionary containing calculated aspect ratios or an error message.

    Example:

    {
      "EAR": 0.289,  # Eye Aspect Ratio
      "MAR": 0.625,  # Mouth Aspect Ratio
      "MOE": 2.162,  # Mouth over Eye Ratio
      "HPR": 1.342,  # Head Pose Ratio
      "BAR": 12.456  # Brow Aspect Ratio
    }
    

calculate_all_ARs

Calculates all aspect ratios from either an image path or image data (numpy array).

Args:

  • image_path (str, optional): Path to the input image file. Defaults to None.
  • image_data (numpy array, optional): Image data. Defaults to None.

Returns:

  • dict: A dictionary containing calculated aspect ratios.

predict_drowsiness_with_cnn

Predicts drowsiness using the trained CNN model from image data or path.

Args:

  • image_data (numpy array, optional): Image data as numpy array. Defaults to None.
  • image_path (str, optional): Path to the input image file. Defaults to None.
  • force_train (bool, optional): Whether to force retraining of the CNN model. Defaults to False.
  • train_dir (str, optional): Path to the training data directory (needed if force_train=True).
  • test_dir (str, optional): Path to the test data directory (needed if force_train=True).

Returns:

  • tuple: (prediction, confidence) where prediction is 0 (Active) or 1 (Drowsy), and confidence is a float between 0 and 1.

Author

Sharjeel Baig

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drowsiness_validator-0.1.4.tar.gz (74.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drowsiness_validator-0.1.4-py3-none-any.whl (74.8 MB view details)

Uploaded Python 3

File details

Details for the file drowsiness_validator-0.1.4.tar.gz.

File metadata

  • Download URL: drowsiness_validator-0.1.4.tar.gz
  • Upload date:
  • Size: 74.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for drowsiness_validator-0.1.4.tar.gz
Algorithm Hash digest
SHA256 05fd9cc5be0f8515bef95cec5b09f7f6adc6a5490437afd0f2715531541c2e9c
MD5 643e90a6b12449edcfb2360371671089
BLAKE2b-256 36fb7ff820e738babc2be04fdf7c9cefb8b444190c37f0849719d22b343c4235

See more details on using hashes here.

File details

Details for the file drowsiness_validator-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for drowsiness_validator-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 604c85f75e7d73765de66d25673f7a31614e98d2f5bc3f4718d7aa4d8c754515
MD5 f9b82f760de2bd218e8d22c53f1e2123
BLAKE2b-256 9b9d54e873a7f757b97d415232e62ac0f3d39af3080a3160f61e2f788ceffc4c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page