Safety precaution for driving in Indian roads
India has 1% of the world's vehicles but accounts for 6% of the world's road traffic accidents. What is the root cause for it and what are the measures we can take to minimize these accidents?
What this project is about
Analyzing the road accident data from the Indian government website and suggesting a few important safety measures for the travelers.
Basic flow chart of the project
In this blog, we are going to know the root cause of the accident by using the following technology- Python
- Pandas
- Seaborn
- MySQL
- Flask
- Jinja web template
Prerequisite
- Python 3.X version
- Refer my blog where you will get all the reference link to install Python and other dependencies
- Anaconda for Jupyter notebook
- From this site, you will get Conda, Jupyter notebook, Spyder IDE and many more
- Pycharm IDE (Optional)
- I personally like this IDE for Python coding. Refer my previous blog
- MySQL server
- Download MySQL from the official site and select the option of MySQL server while installing
The workflow of this project
- Downloading the needed accident reports from the government website
- Analyzing the downloaded dataset by EDA (Exploratory Data Analysis)
- Load the result data into the MySQL database
- Creating a webpage to get data from the user
- Suggesting the safety precaution ideas to the user
Road accident dataset
Download the dataset from the Indian government website and I used only the below accidents category dataset. You can get those selected datasets from here.
- The accident report from the year 1970 to 2017
- Accidents due to weather condition
- Reports based on the time
- Persons killed without using helmets
- The accident reports based on the place
- Accidents happened due to vehicle defects
- State-wise reports in the year 2017
- Person killed per thousand population
Exploratory Data Analysis
I upload the exploratory data analyzing Jupyter's notebook file in the kaggle. Refer the EDA file to get to know the techniques of analyzing any kind of dataset. Here I added a few statistics reports from the EDA process.
Accidents report from the year 1970 to 2017
Total and fatal accidents across the states
Accidents happened due to driver's irresponsibility
Person killed without using the helmet
Accidents based on per thousand population across the states
In the EDA process, we analyzed the data based on certain rules and also set threshold values for each criterion. Now we need to load the data into the database.
Loading data into the database
I am using the MySQL database to load the data. Create the database name "indian_road_accident" by using the below command
Move the file that we create in the EDA process to this location "C:\ProgramData\MySQL\MySQL Server 8.0\Uploads". Also, edit the "my.ini" file by following the below image to load data from CSV file into the database
Create the table schema by running the below code
CREATE TABLE Accident(States_UTs VARCHAR(255), Fatal_Accidents VARCHAR(255), Without_using_helmet VARCHAR(255),Intake_of_Alcohal VARCHAR(255), Exceeding_lawful_speed VARCHAR(255), Jumping_Red_Light VARCHAR(255),Driving_on_wrong_side VARCHAR(255), Jumping_Changing_lanes VARCHAR(255), Overtaking VARCHAR(255),Using_Mobile_Phones_while_driving VARCHAR(255), Asleep_or_Fatigued_or_Sick VARCHAR(255),Near_School_College_any_other_educational_Institutes VARCHAR(255),Pedestrian_crossing VARCHAR(255), Market_Place VARCHAR(255), Near_a_Religious_Place VARCHAR(255),Near_Hospital VARCHAR(255), Narrow_bridge_or_culverts VARCHAR(255), Near_Petrol_Pump VARCHAR(255),Near_Bus_Stand VARCHAR(255), Near_or_on_Road_under_Construction VARCHAR(255), 06_900hrs VARCHAR(255),09_1200hrs VARCHAR(255), 12_1500hrs VARCHAR(255), 15_1800hrs VARCHAR(255), 18_2100hrs VARCHAR(255), 21_2400hrs VARCHAR(255),00_300hrs VARCHAR(255), 03_600hrs VARCHAR(255), Defective_brakes VARCHAR(255), Defective_Steering_Axil VARCHAR(255),Punctured_burst_Typres VARCHAR(255), Worn_out_Tyres VARCHAR(255), Mist_fog VARCHAR(255), Cloudy VARCHAR(255),Heavy_rain VARCHAR(255), Flooding_of_slipways_rivulers VARCHAR(255), Hail_sleet VARCHAR(255), snow VARCHAR(255),Strong_wind VARCHAR(255), Dust_storm VARCHAR(255), Very_hot VARCHAR(255), Very_cold VARCHAR(255));
Loading CSV file into the databaseLOAD DATA INFILE "C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\Road_accident_dataset.csv" INTO TABLE accident FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;
Run the below command to check the DBCreating a webpage to get user data
I used Python flask and Jinja web template to create a web page. Below I listed the work of each file
- flask_file.py
- It takes care of all the web page related work like using CSS, Javascript, JQuery
- Creating a dynamic table using the Jinja web template
- main.py
- The main controller that connects with flask and MySQL
- accident_database.py
- Fetching data from the database
- HTML files
- upload.html
- Used to get the data from the user
- statistics.html
- Used to publish the result to the user
import os
from main import execution
from flask import Flask, render_template, request
image_folder = os.path.join('static', 'image')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = image_folder
app.debug = True
# route and function to handle the form page
@app.route('/', methods=['GET', 'POST'])
def upload_page():
if request.method == 'POST':
user_selected_values = request.form.getlist("accident") # Getting values given by user
suggestion, accident_threshold_value, overall_safety = execution(user_selected_values)
negative_value_count = sum(n < 0 for n in accident_threshold_value) # Checking total numer of negative difference values
year_chart = os.path.join(app.config['UPLOAD_FOLDER'], 'year.png')
fatal_accident = os.path.join(app.config['UPLOAD_FOLDER'], 'fatal_accident.png')
irresponsibility = os.path.join(app.config['UPLOAD_FOLDER'], 'irresponsibility.png')
print(overall_safety)
return render_template('statistics.html', suggestion= suggestion, accident_threshold_value=accident_threshold_value , overall_safety=overall_safety, negative_value_count = negative_value_count, year_chart=year_chart, fatal_accident=fatal_accident, irresponsibility=irresponsibility)
elif request.method == 'GET':
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)
Main.pyimport pandas as pd
import configuration # importing values from configuration file
from accident_database import mysql_data
feature = []
suggestion = []
def execution(feature_keys):
for data in feature_keys:
data = int(data)
if data in configuration.feature_dict.keys():
feature.append(configuration.feature_dict[data]) # Separating feature name to use in SQL query based on key
elif data in configuration.state_dict.keys():
state_name = configuration.state_dict[data] # Separating state name to use in SQL query based on key
fetch_data = mysql_data(state_name, feature) # calling MYSQL to execute the query
actual_value = pd.Series(fetch_data[0]) # Contains values for each feature
threshold_value = pd.Series(fetch_data[1]) # Contains threshold values for each feature
difference_value = threshold_value.astype('float') - actual_value.astype('float') # Variance from the threshold
# Formula based on the weightage
# 50% value for difference value lesser than -5
# 25% value for difference value between -3 and -5
# 10% value for difference value between 0 and -3
# 8% value for difference value between 0 and 3
# 5% value for difference value between 3 and 5
# 2% value for difference value greater than 5
accident_threshold_value = difference_value.apply(
lambda x: x * 5 / 100 if (3.0 < x < 5.0) else (x * 8 / 100 if (0 < x < 3.0) else (
x * 10 / 100 if (-3 < x < 0) else (
x * 25 / 100 if (-5 < x < -3) else (x * 50 / 100 if (x < -5) else x * 2 / 100)))))
for data in feature:
if data in configuration.user_suggestion.keys():
suggestion.append(configuration.user_suggestion[data]) # Appending suggestion data to publish in webpage
overall_safety = accident_threshold_value.sum() # Overall safety status of the travel
# accident_threshold_dict = dict(zip(suggestion, accident_threshold_value))
return suggestion, accident_threshold_value, overall_safety
These two are the important files and the remaining files you can download from the end of this blog. Now we'll move to the output part.Run the "flask_file.py" file and you will the result like below
Open the http://127.0.0.1:5000/ link in the browser
Used Bootstrap CSS to have rich look in the webpage and you can check other features in the CSS files
After submitting your travel details, you will get safety precaution suggestion and alerting the risk status based on the values you are feeding.
Safe to travel status
Warning alert for travel
Danger alert to take precaution immediately
Code in Github
Get this project complete code from hereFinally, Tamilnadu and Uttar Pradesh are having a high accident rate and we can control by driving in lawful speed and driving carefully during worse climate respectively.
P.s: Mental illness affects one in four people as per the WHO report. Don't compare yourself with anyone and everyone's track is different. Valuable comments and suggestions are always welcome.
Comments
Post a Comment