Cepstromaniax
Created July 10, 2018 © Apache-2.0

RD3 - Robust Driver Drowsiness Detector

Detecting driver drowsiness, preventing a traffic accident.

IntermediateFull instructions provided49
RD3 - Robust Driver Drowsiness Detector

Things used in this project

Story

Read more

Code

MainActivity.java

Java
Main part of android code for receiving data from raspberry database and determining the drowsiness of driver.
package io.hackster.cepstromaniax.drowsysafe;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.Timer;
import java.util.TimerTask;

import android.media.MediaPlayer;
import android.os.Vibrator;

public class MainActivity extends AppCompatActivity {

    TextView counterTextView,statusTextView;
    //ImageView mImageView;
    int treshCounter;
    Timer myTimer;

    MediaPlayer mp;
    Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //mImageView = (ImageView) findViewById(R.id.backwalaImageView);
        //mImageView.setImageResource(R.drawable.backwala);

        treshCounter=10;

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
            }

        }, 0, 500);
    }

    private void TimerMethod()
    {
        //This method is called directly by the timer
        //and runs in the same thread as the timer.

        //We call the method that will work with the UI
        //through the runOnUiThread method.
        this.runOnUiThread(Timer_Tick);
    }
    private Runnable Timer_Tick = new Runnable() {
        public void run() {
            try {
                getJSON("http://192.168.1.24/safedrowsy/getdata.php");
            }catch (Exception e) {
                //Toast toast = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
                //toast.show();
            }
        }
    };

    private void getJSON(final String urlWebService) {

        class GetJSON extends AsyncTask<Void, Void, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                //Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                try {
                    loadIntoListView(s);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URL url = new URL(urlWebService);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String json;
                    while ((json = bufferedReader.readLine()) != null) {
                        sb.append(json + "\n");
                    }
                    return sb.toString().trim();
                } catch (Exception e) {
                    return null;
                }
            }
        }
        GetJSON getJSON = new GetJSON();
        getJSON.execute();
    }

    private void loadIntoListView(String json) throws JSONException {
        TextView counterTextView = (TextView) findViewById(R.id.counterTextView);
        TextView statusTextView = (TextView) findViewById(R.id.statusTextView);

        int shortcounter=0;

        JSONArray jsonArray = new JSONArray(json);
        String[] data = new String[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            data[i] = obj.getString("bit");
            if (data[i].toString().equals('1')){
                longcounter+=1;
            }
        }
        counterTextView.setText("Long Breathe Counter: "+longcounter);

        if(shortcounter>treshCounter){ //drowsy detected
            statusTextView.setText("Status: Drowsy Detected");
            //play music
            int i=1;

            mp=MediaPlayer.create(this, R.raw.sound);
            mp.start();

            //vibrate phone
            // Get instance of Vibrator from current Context
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 400 milliseconds
            v.vibrate(1000);

            //call family
            try {
                String number = "7777777777";
                Uri call = Uri.parse("tel:" + number);
                Intent surf = new Intent(Intent.ACTION_DIAL, call);
                startActivity(surf);
            } catch (Exception e) {
                //Toast.makeText(getApplicationContext(), "Error in your phone call"+e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
        else{
            statusTextView.setText("Status: Normal Breath, No Drowsy");
        }

    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

safedrowsy.py

Python
This code for retrieving data from walabot and estimate the long/short breathe and send it to database
# Modified by cepstromaniax from Walabreathe code
# Safedrowsy system (prototype)

from __future__ import print_function
from sys import platform
from os import system
from imp import load_source
from os.path import join

#import matplotlib.pyplot as plt #pyplot library for plotting breath 'energy' in real-time
import numpy as np

import time
import array
import os

import sys
import MySQLdb
conn = MySQLdb.connect(host= "localhost",
                  user="admin",
                  passwd="admin",
                  db="safedrowsy",
                  port=3306)    
x = conn.cursor()


#plt.axis([0, 1000, 0, 100]) #pyplot function for mentioning minimum and maximum 'x' and 'y' axis values
#plt.ion() #enables interactive plotting

counter = 0
b_magnitude = 0
b_counter = 0
nb_counter = 0
gen_counter = 0
lb_counter = 0
sb_counter = 0
i = 0
bn_counter = 0    #Breath Stop Detection

if platform == 'win32':
	modulePath = join('C:/', 'Program Files', 'Walabot', 'WalabotSDK',
		'python', 'WalabotAPI.py')
elif platform.startswith('linux'):
    modulePath = join('/usr', 'share', 'walabot', 'python', 'WalabotAPI.py')

wlbt = load_source('WalabotAPI', modulePath)
wlbt.Init()

def detectBreath(energy):    #the main function in walabreathe where the breath detection,conversion to morse code and speech output takes place

    global counter
    global b_magnitude
    global b_counter
    global nb_counter
    global gen_counter
    global lb_counter
    global sb_counter
    global bn_counter

    b_magnitude = energy*1000

    counter+=1
    gen_counter+=1

    #plt.scatter(counter,energy*1000)    #pyplot function that allows us to scatter plot breathing 'energy' values
    #plt.pause(0.005)

    if b_magnitude>3:    #user is breathing
        b_counter+=1
    else:    #not breathing
        nb_counter+=1

    if(gen_counter==10):    #taking 15 'energy' values to classify into 'long' and 'short' breaths
        if(b_counter>0 and nb_counter>0):
            if(b_counter>nb_counter):     #if energy magnitude is 'high' most the time among the 15 readings, then it is a 'long breath'
                print('Long Breath')
                lb_counter+=1
                try:
                   x.execute("INSERT INTO data(status,bit) VALUES (%s,%s)",('Long Breath',1))
                   conn.commit()
                except:
                   conn.rollback()
            else:    #when energy magnitude is 'low' most the time among the 15 readings, then it is a 'short breath'
                print('Short Breath')
                sb_counter+=1
                try:
                   x.execute("INSERT INTO data(status,bit) VALUES (%s,%s)",('Short Breath',0))
                   conn.commit()
                except:
                   conn.rollback()
        else:
            print('No Breath')
            try:
               x.execute("INSERT INTO data(status,bit) VALUES (%s,%s)",('No Breath',0))
               conn.commit()
            except:
               conn.rollback()
             

        if(b_counter==0):
            bn_counter+=1

        gen_counter=0
        b_counter=0
        nb_counter=0

def BreathingApp():
    # Walabot_SetArenaR - input parameters
    minInCm, maxInCm, resInCm = 30, 150, 1
    # Walabot_SetArenaTheta - input parameters
    minIndegrees, maxIndegrees, resIndegrees = -4, 4, 2
    # Walabot_SetArenaPhi - input parameters
    minPhiInDegrees, maxPhiInDegrees, resPhiInDegrees = -4, 4, 2
    # Configure Walabot database install location (for windows)
    wlbt.SetSettingsFolder()
    # 1) Connect : Establish communication with walabot.
    wlbt.ConnectAny()
    # 2) Configure: Set scan profile and arena
    # Set Profile - to Sensor-Narrow.
    wlbt.SetProfile(wlbt.PROF_SENSOR_NARROW)
    # Setup arena - specify it by Cartesian coordinates.
    wlbt.SetArenaR(minInCm, maxInCm, resInCm)
    # Sets polar range and resolution of arena (parameters in degrees).
    wlbt.SetArenaTheta(minIndegrees, maxIndegrees, resIndegrees)
    # Sets azimuth range and resolution of arena.(parameters in degrees).
    wlbt.SetArenaPhi(minPhiInDegrees, maxPhiInDegrees, resPhiInDegrees)
    # Dynamic-imaging filter for the specific frequencies typical of breathing
    wlbt.SetDynamicImageFilter(wlbt.FILTER_TYPE_DERIVATIVE)
    # 3) Start: Start the system in preparation for scanning.
    wlbt.Start()
    # 4) Trigger: Scan (sense) according to profile and record signals to be
    # available for processing and retrieval.
    while True:
        appStatus, calibrationProcess = wlbt.GetStatus()
        # 5) Trigger: Scan(sense) according to profile and record signals
        # to be available for processing and retrieval.
        wlbt.Trigger()
        # 6) Get action: retrieve the last completed triggered recording
        energy = wlbt.GetImageEnergy()

        detectBreath(energy)
    # 7) Stop and Disconnect.
    wlbt.Stop()
    wlbt.Disconnect()
    print('Terminate successfully')

if __name__ == '__main__':
    BreathingApp()

getdata.php

PHP
Retrieving data from mysql database and send it to android phone in jason code
<?php
$servername = "localhost";
$username = "admin";
$password = "admin";
$database="safedrowsy";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//echo "Connected successfully";

//creating an array for storing the data 
$datax = array(); 

//this is our sql query 
$sql = "SELECT * FROM data ORDER BY data.index DESC LIMIT 20;";

//creating an statment with the query
$stmt = $conn->prepare($sql);

//executing that statement
$stmt->execute();

//binding results for that statment 
$stmt->bind_result($index,$status,$bit);

//looping through all the records
while($stmt->fetch()){
	
	//pushing fetched data in an array 
	$temp = [
		'index'=>$index,
		'status'=>$status,
		'bit'=>$bit,		
	];
	array_push($datax, $temp);
}

//displaying the data in json format 
echo json_encode($datax);
?>

Credits

Cepstromaniax

Cepstromaniax

2 projects • 1 follower
Cepstromaniax is the community-based team on the science and technology. We focus our study on signal processing, embedded systems and IoT.

Comments