-
Notifications
You must be signed in to change notification settings - Fork 52
/
app.py
104 lines (82 loc) · 3.15 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
import os, sys, shutil, time
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
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():
rfc = joblib.load('model/rf_model')
print('model loaded')
if request.method == 'POST':
address = request.form['Location']
geolocator = Nominatim()
location = geolocator.geocode(address,timeout=None)
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 = rfc.predict(X)
if my_prediction[0][0] == 1:
my_prediction='Predicted crime : Act 379-Robbery'
elif my_prediction[0][1] == 1:
my_prediction='Predicted crime : Act 13-Gambling'
elif my_prediction[0][2] == 1:
my_prediction='Predicted crime : Act 279-Accident'
elif my_prediction[0][3] == 1:
my_prediction='Predicted crime : Act 323-Violence'
elif my_prediction[0][4] == 1:
my_prediction='Predicted crime : Act 302-Murder'
elif my_prediction[0][5] == 1:
my_prediction='Predicted crime : Act 363-kidnapping'
else:
my_prediction='Place is safe no crime expected at that timestamp.'
return render_template('result.html', prediction = my_prediction)
if __name__ == '__main__':
app.run(debug = True)