Giter VIP home page Giter VIP logo

drowsiness_detection's Introduction

Medium LinkedIn OpenCV Python Computer Vision

License: MIT

Drowsiness Detection

Assista ao Vídeo

CAPA Medium (2)

This project uses Python computer vision to monitor a user's eyes and detect signs of drowsiness. This technology has the potential to prevent accidents related to drowsiness, such as those that occur while driving.

Package Import

The necessary packages are imported at the beginning of the code. The main packages include:

  • dlib: A library containing machine learning algorithms and image processing tools, including face detection.
  • cv2 (OpenCV): A computer vision library that contains functions for processing images and videos.
  • imutils: A library that simplifies some basic image processing operations with OpenCV.
  • numpy: A library used for scientific computing with Python.
  • playsound: A library for playing sounds.
  • argparse: A library for creating user-friendly command line interfaces.
  • scipy.spatial: A module for calculating distances and performing operations in n-dimensional spaces.
  • matplotlib.pyplot: Used for plotting data.

Project Structure

The structure of the project is as follows:

.
├── Readme.md
├── alarm.wav
├── environment.yaml
└── main.py
└── shape_predictor_68_face_landmarks.dat

Description of the files:

  • alarm.wav: Alarm sound that will be played when signs of fatigue are detected.
  • main.py: Main Python script that contains the logic of the fatigue detector.
  • requirements.txt: A file that lists the dependencies required to run the program.
  • shape_predictor_68_face_landmarks.dat: Data file used by the dlib facial landmarks detector.

Custom Functions

Two custom functions are defined at the beginning of the program. The play_alarm(path) function plays an alarm sound. The calculate_ear(eye) function computes the Eye Aspect Ratio (EAR). The EAR is a numeric value that indicates the degree of eye openness. When a person blinks, the EAR decreases.

Command Line Arguments

The program accepts two command line arguments: "-a" to turn the sound alarm on or off, and "-w" to select the camera that will be used to capture the video.

Initial Setup

The program defines some constants, such as the EAR threshold that indicates a blink (EAR_THRESHOLD) and the number of consecutive frames where the EAR must be below the threshold to trigger the alarm (NUM_CONSEC_FRAMES). It also initializes some variables and loads the dlib face detector and facial landmarks predictor.

Video Processing

The program enters a continuous loop, where each iteration of the loop processes a single frame of the video. For each frame, the program detects present faces, identifies the facial features for the face region, extracts the coordinates of the left and right eyes, and calculates the EAR for both eyes.

The program draws the contours of the eyes on the original image using the cv2.drawContours() method. The EAR ratio is displayed on a real-time graph with each iteration.

Drowsiness Detection

If the EAR is below the threshold for a set number of consecutive frames, the program considers the user to be becoming drowsy and triggers an alarm.

If the EAR returns to being above the threshold, the frame counter and the alarm are reset.

The code also displays the calculated EAR on the video frame to aid in debugging and adjusting the appropriate EAR thresholds.

Termination

The video processing loop continues until the 'q' key is pressed. When this happens, the program ends and finalizes the video stream.

Literary References

SOUKUPOVA, Tereza; CECH, Jan. Real-time eye blink detection using facial landmarks. In: 21st computer vision winter workshop, Rimske Toplice, Slovenia. 2016.

License

This project is licensed under the MIT License

drowsiness_detection's People

Contributors

jonysarcanjo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.