Optimised; PyLint score 9.44/10
This commit is contained in:
parent
fca01b7e7b
commit
656e8b59c4
128
main.py
128
main.py
|
@ -1,24 +1,21 @@
|
|||
"""AutoSafe
|
||||
Modular Road-Safety Toolkit
|
||||
|
||||
import overpy
|
||||
import sys
|
||||
import simplejson as sjson
|
||||
import json
|
||||
"""
|
||||
#pylint: disable=C0103,C0111
|
||||
import argparse
|
||||
import time
|
||||
from threading import Thread
|
||||
import overpy
|
||||
import simplejson as sjson
|
||||
from scipy.spatial import distance as dist
|
||||
from imutils.video import VideoStream
|
||||
from imutils import face_utils
|
||||
from threading import Thread
|
||||
import numpy as np
|
||||
import playsound
|
||||
import argparse
|
||||
#import playsound
|
||||
import imutils
|
||||
import dlib
|
||||
import cv2
|
||||
from uber_rides.session import Session
|
||||
from uber_rides.client import UberRidesClient
|
||||
import signal
|
||||
import argparse
|
||||
#from uber_rides.session import Session
|
||||
#from uber_rides.client import UberRidesClient
|
||||
import tweepy
|
||||
import requests
|
||||
|
||||
|
@ -29,7 +26,7 @@ import requests
|
|||
# Defining Variables #
|
||||
######################
|
||||
|
||||
radius = str(100) # Radius for maxspeed function. taken as a string because gets concatenated in the function
|
||||
radius = str(100) # Radius for maxspeed function. taken as a string because gets concatenated
|
||||
|
||||
################
|
||||
# Dummy Values #
|
||||
|
@ -81,37 +78,37 @@ def get_api(cfg):
|
|||
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
|
||||
return tweepy.API(auth)
|
||||
|
||||
def tweet():
|
||||
def tweetMe():
|
||||
cfg = {
|
||||
"consumer_key" : "knQFpTnjuSvr6OxYwebt3wyrd",
|
||||
"consumer_secret" : "Mhex3oRkmaF7lD3hoMvHpAD6ctW0ugKYCopTlhc0JzOLOMIZ0w",
|
||||
"access_token" : "2846631344-wEozinvHfEIFxFVy51I6te8SrN5OTFtU00wxsiz",
|
||||
"access_token_secret" : "Nfx1U8a2TjAQXFLBrJIyy2p36sjBGAWFIthLc1cIoI56U"
|
||||
}
|
||||
"consumer_key":"knQFpTnjuSvr6OxYwebt3wyrd",
|
||||
"consumer_secret":"Mhex3oRkmaF7lD3hoMvHpAD6ctW0ugKYCopTlhc0JzOLOMIZ0w",
|
||||
"access_token":"2846631344-wEozinvHfEIFxFVy51I6te8SrN5OTFtU00wxsiz",
|
||||
"access_token_secret":"Nfx1U8a2TjAQXFLBrJIyy2p36sjBGAWFIthLc1cIoI56U"
|
||||
}
|
||||
|
||||
api = get_api(cfg)
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
'user-key': 'a530c1424d9abe5442fa22f77ce03d25',
|
||||
'user-key': 'a530c1424d9abe5442fa22f77ce03d25',
|
||||
}
|
||||
|
||||
params = (
|
||||
('lat', '28.546519'),
|
||||
('lon', '77.179248'),
|
||||
)
|
||||
|
||||
response = requests.get('https://developers.zomato.com/api/v2.1/geocode', headers=headers, params=params)
|
||||
url = 'https://developers.zomato.com/api/v2.1/geocode'
|
||||
response = requests.get(url, headers=headers, params=params)
|
||||
loc = response.json()['location']['title']
|
||||
tweet = ("Stay Alert! Sudden braking at: ", loc )
|
||||
tweet = ("Stay Alert! Sudden braking at: ", loc)
|
||||
status = api.update_status(status=tweet)
|
||||
print(status)
|
||||
|
||||
################################
|
||||
# Fetching Details from Zomato #
|
||||
################################
|
||||
|
||||
res = []
|
||||
|
||||
def getRes():
|
||||
res = []
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
'user-key': 'a530c1424d9abe5442fa22f77ce03d25',
|
||||
|
@ -121,8 +118,8 @@ def getRes():
|
|||
('lat', '28.546519'),
|
||||
('lon', '77.179248'),
|
||||
)
|
||||
|
||||
response = requests.get('https://developers.zomato.com/api/v2.1/geocode', headers=headers, params=params)
|
||||
url = 'https://developers.zomato.com/api/v2.1/geocode'
|
||||
response = requests.get(url, headers=headers, params=params)
|
||||
res = response.json()['popularity']['nearby_res']
|
||||
return res
|
||||
|
||||
|
@ -133,12 +130,12 @@ def getDetails(res):
|
|||
}
|
||||
url = "https://developers.zomato.com/api/v2.1/restaurant?res_id=" + str(res[0])
|
||||
newResponse = requests.get(url, headers=headers)
|
||||
newRes = []
|
||||
#newRes = []
|
||||
resName = newResponse.json()['name']
|
||||
resAddress = newResponse.json()['location']['address']
|
||||
print("You are feeling sleepy, why don't you take a break?\n")
|
||||
print("Your nearest eatery is " + resName,"\n")
|
||||
print(resName + " is at " + resAddress,"\n")
|
||||
print("Your nearest eatery is " + resName, "\n")
|
||||
print(resName + " is at " + resAddress, "\n")
|
||||
|
||||
def zomato():
|
||||
res = getRes()
|
||||
|
@ -149,17 +146,17 @@ def zomato():
|
|||
############################
|
||||
|
||||
def drunk():
|
||||
print("Your Location Appears to be around places which sells \n alcohol, please take the breathalyser test ")
|
||||
print("Your appear to be around places which sell \n alcohol, take the breathalyser test")
|
||||
|
||||
bac_raw = str(open("./files/bac.txt","r").read())
|
||||
bac_raw = str(open("./files/bac.txt", "r").read())
|
||||
bac = float(float(bac_raw)/100)
|
||||
print(bac)
|
||||
if(bac >= 0.08):
|
||||
if bac >= 0.08:
|
||||
print("Please Do Not Drive!\n")
|
||||
print("I can call a cab if you want\n")
|
||||
print("")
|
||||
answer = input("Say No, to disagree, else I'll book the cab: \n")
|
||||
if(answer=="no"):
|
||||
if answer == "no":
|
||||
print("You are not fit to drive")
|
||||
print("Text message to emergency contact sent")
|
||||
print("Your cab has been booked, thank you for not driving")
|
||||
|
@ -186,8 +183,7 @@ shape_predictor = "./files/shape_predictor_68_face_landmarks.dat"
|
|||
EYE_AR_THRESH = 0.2 # If the EAR goes < this for 48 frames, it is counted as drowsiness
|
||||
EYE_AR_CONSEC_FRAMES = 24
|
||||
|
||||
COUNTER = 0
|
||||
ALERT = False
|
||||
|
||||
|
||||
|
||||
########################
|
||||
|
@ -195,6 +191,10 @@ ALERT = False
|
|||
########################
|
||||
|
||||
def sleepiness():
|
||||
|
||||
COUNTER = 0
|
||||
#ALERT = False
|
||||
|
||||
print("Initialising Facial Landmark Predictor...")
|
||||
detector = dlib.get_frontal_face_detector()
|
||||
predictor = dlib.shape_predictor(shape_predictor)
|
||||
|
@ -239,11 +239,9 @@ def sleepiness():
|
|||
|
||||
# draw an alarm on the frame
|
||||
cv2.putText(frame, "Sleepiness Detected!", (10, 30),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
||||
print("Sleepiness Detected!")
|
||||
print("\a");print("\a");print("\a");print("\a");print("\a")
|
||||
print("\a");print("\a");print("\a");print("\a");print("\a")
|
||||
print("\a");print("\a");print("\a");print("\a");print("\a")
|
||||
print("\n \n \n")
|
||||
zomato()
|
||||
time.sleep(5)
|
||||
exit()
|
||||
|
@ -288,36 +286,37 @@ def maxspeed(coordinates, radius):
|
|||
|
||||
results_list = []
|
||||
for way in result.ways:
|
||||
road = {}
|
||||
road["name"] = way.tags.get("name", "n/a")
|
||||
road["speed_limit"] = way.tags.get("maxspeed", "n/a")
|
||||
nodes = []
|
||||
for node in way.nodes:
|
||||
nodes.append((node.lat, node.lon))
|
||||
road["nodes"] = nodes
|
||||
results_list.append(road)
|
||||
return results_list
|
||||
road = {}
|
||||
road["name"] = way.tags.get("name", "n/a")
|
||||
road["speed_limit"] = way.tags.get("maxspeed", "n/a")
|
||||
nodes = []
|
||||
for node in way.nodes:
|
||||
nodes.append((node.lat, node.lon))
|
||||
road["nodes"] = nodes
|
||||
results_list.append(road)
|
||||
return results_list
|
||||
|
||||
|
||||
|
||||
|
||||
############################################################################################################################
|
||||
# Gives data to the function and gets json in return. This json is then parsed. Then the double quotes are stripped off it #
|
||||
############################################################################################################################
|
||||
#########################################################################
|
||||
# Gives data to the function and gets json in return. #
|
||||
#This json is then parsed. Then the double quotes are stripped off it #
|
||||
#########################################################################
|
||||
def speedlim():
|
||||
speedLimit = sjson.dumps(maxspeed((str(glat), str(glng)), radius)[0]['speed_limit']).strip('\"')
|
||||
while(True):
|
||||
carSpeedDummy = open("./files/carSpeed.txt","r")
|
||||
while True:
|
||||
carSpeedDummy = open("./files/carSpeed.txt", "r")
|
||||
carSpeed = carSpeedDummy.read()
|
||||
#print(carSpeed)
|
||||
#carSpeed = 29
|
||||
while(int(carSpeed) > int(speedLimit)):
|
||||
carSpeedDummy = open("./files/carSpeed.txt","r")
|
||||
while int(carSpeed) > int(speedLimit):
|
||||
carSpeedDummy = open("./files/carSpeed.txt", "r")
|
||||
carSpeed = carSpeedDummy.read()
|
||||
print("Over The Speed Limit")
|
||||
time.sleep(5)
|
||||
while(int(carSpeed) <= int(speedLimit)):
|
||||
carSpeedDummy = open("./files/carSpeed.txt","r")
|
||||
while int(carSpeed) <= int(speedLimit):
|
||||
carSpeedDummy = open("./files/carSpeed.txt", "r")
|
||||
carSpeed = carSpeedDummy.read()
|
||||
print("Under the Speed Limit")
|
||||
time.sleep(5)
|
||||
|
@ -327,16 +326,17 @@ def speedlim():
|
|||
##################
|
||||
|
||||
def brakes():
|
||||
while(True):
|
||||
tweet()
|
||||
while True:
|
||||
tweetMe()
|
||||
|
||||
#####################################################
|
||||
# Using Argument Parse to run one command at a time #
|
||||
#####################################################
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
FUNCTION_MAP = {'overspeed' : speedlim, 'sleep-detector' : sleepiness, 'sudden-braking' : brakes, 'drunk' : drunk }
|
||||
FUNCTION_MAP = {'overspeed' : speedlim, 'sleep-detector' : sleepiness,
|
||||
'sudden-braking' : brakes, 'drunk' : drunk}
|
||||
parser.add_argument('function', choices=list(FUNCTION_MAP))
|
||||
args = parser.parse_args()
|
||||
func = FUNCTION_MAP[args.function]
|
||||
func()
|
||||
FUNC = FUNCTION_MAP[args.function]
|
||||
FUNC()
|
||||
|
|
Loading…
Reference in New Issue