-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
153 lines (124 loc) · 4.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os, sys, shutil, time
import pickle
from flask import Flask, request, jsonify, render_template,send_from_directory
import pandas as pd
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import urllib.request
import json
from geopy.geocoders import Nominatim
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
app = Flask(__name__)
@app.route('/')
def root():
return render_template('index.html')
@app.route('/images/<Paasbaan>')
def download_file(Paasbaan):
return send_from_directory(app.config['images'], Paasbaan)
@app.route('/index.html')
def index():
return render_template('index.html')
@app.route('/work.html')
def work():
return render_template('work.html')
@app.route('/about.html')
def about():
return render_template('about.html')
@app.route('/contact.html')
def contact():
return render_template('contact.html')
@app.route('/result.html', methods = ['POST'])
def predict():
dataset=pd.read_csv('data.csv')
data=pd.read_csv('data.csv')
data['timestamp'] = pd.to_datetime(data['timestamp'], errors='coerce')
data['timestamp'] = pd.to_datetime(data['timestamp'], format = '%d/%m/%Y %H:%M:%S')
# DATE TIME STAMP FUNCTION
column_1 = data.ix[:,0]
db=pd.DataFrame({"year": column_1.dt.year,
"month": column_1.dt.month,
"day": column_1.dt.day,
"hour": column_1.dt.hour,
"dayofyear": column_1.dt.dayofyear,
"week": column_1.dt.week,
"weekofyear": column_1.dt.weekofyear,
"dayofweek": column_1.dt.dayofweek,
"weekday": column_1.dt.weekday,
"quarter": column_1.dt.quarter,
})
dataset1=dataset.drop('timestamp',axis=1)
data1=pd.concat([db,dataset1],axis=1)
data1.dropna(inplace=True)
#fitting of data
X=data1.iloc[:,[1,2,3,4,6,16,17]].values
y=data1.iloc[:,[10,11,12,13,14,15]].values
#splitting of data
#from sklearn.cross_validation import train_test_split
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=50)
knn = KNeighborsClassifier(n_neighbors=10)
knn.fit(X,y)
rfc = RandomForestClassifier(n_estimators=100)
knn.fit(X,y)
if request.method == 'POST':
address = request.form['Location']
geolocator = Nominatim(user_agent="E-BEATS")
location = geolocator.geocode(address)
print(location.address)
lat=[location.latitude]
log=[location.longitude]
latlong=pd.DataFrame({'latitude':lat,'longitude':log})
print(latlong)
DT= request.form['timestamp']
latlong['timestamp']=DT
data=latlong
cols = data.columns.tolist()
cols = cols[-1:] + cols[:-1]
data = data[cols]
data['timestamp'] = pd.to_datetime(data['timestamp'].astype(str), errors='coerce')
data['timestamp'] = pd.to_datetime(data['timestamp'], format = '%d/%m/%Y %H:%M:%S')
column_1 = data.ix[:,0]
DT=pd.DataFrame({"year": column_1.dt.year,
"month": column_1.dt.month,
"day": column_1.dt.day,
"hour": column_1.dt.hour,
"dayofyear": column_1.dt.dayofyear,
"week": column_1.dt.week,
"weekofyear": column_1.dt.weekofyear,
"dayofweek": column_1.dt.dayofweek,
"weekday": column_1.dt.weekday,
"quarter": column_1.dt.quarter,
})
data=data.drop('timestamp',axis=1)
final=pd.concat([DT,data],axis=1)
X=final.iloc[:,[1,2,3,4,6,10,11]].values
my_prediction = knn.predict(X)
if my_prediction[0][0] == 1:
my_prediction='Predicted crime : Act 379-Robbery'
#print("ROBBERY")
elif my_prediction[0][1] == 1:
my_prediction='Predicted crime : Act 13-Gambling'
#print("GAMBLAING")
elif my_prediction[0][2] == 1:
my_prediction='Predicted crime : Act 279-Accident'
#print("ACCIDENT")
elif my_prediction[0][3] == 1:
my_prediction='Predicted crime : Act 323-Violence'
#print("VOILENCE")
elif my_prediction[0][4] == 1:
my_prediction='Predicted crime : Act 302-Murder'
#print("MURDER")
elif my_prediction[0][5] == 1:
my_prediction='Predicted crime : Act 363-kidnapping'
#print("KIDNAPPING")
else:
my_prediction='Place is safe no crime expected at that timestamp.'
#print("SAFE")
proba = np.max(knn.predict_proba(X))*100
probr = 'Probability of occurance : '+str(proba)
return render_template('result.html', prediction = my_prediction, prob = probr)
if __name__ == '__main__':
app.run(debug = True)