Skip to main content

Featured Post

Personal Portfolio Website | HTML | CSS | JavaScript

General Portfolio Details:      A personal portfolio website is an online platform where individuals showcase their skills, experiences, projects, and accomplishments. It serves as a digital resume and a creative outlet, allowing you to present yourself professionally to potential clients, employers, or collaborators. Here's a breakdown of what a personal portfolio website typically includes: 1.  Introduction/About Me:  A brief yet engaging introduction that highlights your expertise, passion, and professional background. It sets the tone for visitors to understand who you are and what you do. 2.  Portfolio/Projects:  Showcase your best work prominently. This could include case studies, descriptions, images, or videos of projects you've completed. Highlighting diverse skills and accomplishments can demonstrate your versatility. 3.  Services/Skills:  A section detailing the services you offer or the skills you possess. Use this area to elaborate on your expertise and what sets y

Drowsiness Detection & Alerting System

 The "Drowsiness Detection and Alerting System" is an innovative project leveraging Python 3, OpenCV (Open Source Computer Vision Library), IBM Cloud, and Node-RED to enhance safety by detecting driver drowsiness in real-time and issuing alerts. 

sample image


Key Components:


1. Python 3 with OpenCV: Utilizes the OpenCV library's computer vision algorithms to analyze live video feeds or images from a camera to detect facial landmarks and monitor changes in the driver's facial expressions, specifically focusing on eye movements and blink patterns.


2. Drowsiness Detection Algorithm: Employs image processing techniques to detect indicators of drowsiness such as eye closure duration, head position, or yawning patterns. By continuously monitoring these features in real time, the system can accurately determine the driver's state of alertness.


3. Alerting Mechanism: Once signs of drowsiness are detected, the system triggers an alert mechanism. This could include audible warnings, visual cues on a dashboard or screen, or even haptic feedback to grab the driver's attention and prompt them to take a break or corrective action.


4. IBM Cloud: Integration with IBM Cloud services provides scalability, storage, and potentially the ability to run complex analytics or machine learning models for more advanced drowsiness prediction.


5. Node-RED: Using Node-RED for flow-based programming facilitates the integration of various components and simplifies the creation of event-driven workflows. It allows for seamless communication between the Python-based drowsiness detection module and other systems or alert mechanisms.


6. User Interface (UI): Optionally, a user-friendly interface could be developed to display real-time analytics, alerts, and potentially historical data on drowsiness instances, providing insights for analysis and improvement.



Benefits:


- Enhanced Safety: By continuously monitoring the driver's alertness, the system aims to reduce the risk of accidents caused by drowsy driving.

- Real-time Detection: Offers immediate alerts, allowing the driver to take corrective action promptly.

- Integration Possibilities: Potential integration with other safety systems in vehicles or fleet management solutions.

- Scalability and Analytics: Utilizes cloud services for scalability and advanced analytics, facilitating further improvements to the drowsiness detection algorithm over time.


This project combines cutting-edge technologies to create a proactive safety solution that can potentially save lives by addressing the dangers associated with driver drowsiness.


GitHub Source Code Link:

https://github.com/vsmidhun21/DrowsinessDetectionAndAlertingSystem

main.py



#Importing Packages
import cv2
import os
from keras.models import load_model
import numpy as np
from pygame import mixer
import time
import sys
import ibmiotf.application
import ibmiotf.device
import pyttsx3

#IBM Waston Device
organization = "25f3fm"
deviceType = "abcd"
deviceId = "1234"
authMethod = "token"
authToken = "12345678"

#IBM Connection & sending data
def ibmstart(x):

    def myCommandCallback(cmd):
        print("Command received: %s" % cmd.data['command'])
        print(cmd)
    try:
        deviceOptions = {"org": organization, "type": deviceType, "id": deviceId, "auth-method": authMethod, "auth-token": authToken}
        deviceCli = ibmiotf.device.Client(deviceOptions)
    except Exception as e:
        print("Caught exception connecting device: %s" % str(e))
        sys.exit()
    deviceCli.connect()
    data = { 'value' : x}

    def myOnPublishCallback():
        print("Published Status = %s" % x, "to IBM Waston")

    success = deviceCli.publishEvent("DD","json",data,qos=0,on_publish=myOnPublishCallback())
    if not success:
        print("Not Connected to IoTF")

    deviceCli.commandCallback = myCommandCallback
    deviceCli.disconnect()

#pyttsx3 audio msg
engine = pyttsx3.init()

#Detect face , left eye & right eye
face = cv2.CascadeClassifier('haar cascade files\haarcascade_frontalface_alt.xml')
leye = cv2.CascadeClassifier('haar cascade files\haarcascade_lefteye_2splits.xml')
reye = cv2.CascadeClassifier('haar cascade files\haarcascade_righteye_2splits.xml')

lbl = ['Close', 'Open']

model = load_model('models/cnncat2.h5')
path = os.getcwd()
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
count = 0
score = 0
thicc = 2
rpred = [99]
lpred = [99]

#Program starts run infinite time
while (True):
    ret, frame = cap.read()
    height, width = frame.shape[:2]

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = face.detectMultiScale(gray, minNeighbors=5, scaleFactor=1.1, minSize=(25, 25))
    left_eye = leye.detectMultiScale(gray)
    right_eye = reye.detectMultiScale(gray)

    cv2.rectangle(frame, (0, height - 50), (200, height), (0, 0, 0), thickness=cv2.FILLED)

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 1)

    for (x, y, w, h) in right_eye:
        r_eye = frame[y:y + h, x:x + w]
        count = count + 1
        r_eye = cv2.cvtColor(r_eye, cv2.COLOR_BGR2GRAY)
        r_eye = cv2.resize(r_eye, (24, 24))
        r_eye = r_eye / 255
        r_eye = r_eye.reshape(24, 24, -1)
        r_eye = np.expand_dims(r_eye, axis=0)
        rpred = np.argmax(model.predict(r_eye), axis=-1)
        if (rpred[0] == 1):
            lbl = 'Open'
        if (rpred[0] == 0):
            lbl = 'Closed'
        break

    for (x, y, w, h) in left_eye:
        l_eye = frame[y:y + h, x:x + w]
        count = count + 1
        l_eye = cv2.cvtColor(l_eye, cv2.COLOR_BGR2GRAY)
        l_eye = cv2.resize(l_eye, (24, 24))
        l_eye = l_eye / 255
        l_eye = l_eye.reshape(24, 24, -1)
        l_eye = np.expand_dims(l_eye, axis=0)
        lpred = np.argmax(model.predict(l_eye), axis=-1)
        if (lpred[0] == 1):
            lbl = 'Open'
        if (lpred[0] == 0):
            lbl = 'Closed'
        break

    if (rpred[0] == 0 and lpred[0] == 0):
        score = score + 1
        cv2.putText(frame, "Closed", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA)
    else:
        score = score - 1
        cv2.putText(frame, "Open", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA)

    if (score < 0):
        score = 0
    if (score < 2):
        flags=0
        ibmstart(0)
    cv2.putText(frame, 'Score:' + str(score), (100, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA)
    if (score >= 2): #Drowsiness detected
        ibmstart(1)
        flags=1
        try:
            engine.say("Alert!! Wake up!")
            engine.runAndWait()

        except:  # isplaying = False
            pass
        if (thicc < 16):
            thicc = thicc + 2
        else:
            thicc = thicc - 2
            if (thicc < 2):
                thicc = 2
        cv2.rectangle(frame, (0, 0), (width, height), (0, 0, 255), thicc)
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#Stop program
cap.release()
cv2.destroyAllWindows()

Demonstration Video:



Best Regards,

Coder21



Comments

Popular Post