-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (95 loc) · 3.91 KB
/
app.py
File metadata and controls
116 lines (95 loc) · 3.91 KB
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
import time
import redis
import mysql.connector
import os
import sys
import csv
from flask import Flask, request, url_for, redirect, render_template
from GPSPhoto import gpsphoto
from google.cloud import automl_v1beta1
from google.cloud.automl_v1beta1.proto import service_pb2
app = Flask(__name__, template_folder='web')
cache = redis.Redis(host='redis', port=6379)
app.static_folder = 'web'
def insertVariablesIntoTable(path, latitude, longitude, prediction, pilon):
try:
connection = mysql.connector.connect(host='db',
database='pilons',
user='root',
password='root')
cursor = connection.cursor()
mysql_insert_query = """INSERT INTO photos (path, latitude, longitude, result, code)
VALUES (%s, %s, %s, %s, %s) """
recordTuple = (path, latitude, longitude, prediction, pilon)
cursor.execute(mysql_insert_query, recordTuple)
connection.commit()
print("Record inserted successfully into photos table")
except mysql.connector.Error as error:
print("Failed to insert into MySQL table {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
# 'content' is base-64-encoded image data.
def get_prediction(content, project_id, model_id):
prediction_client = automl_v1beta1.PredictionServiceClient()
name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
payload = {'image': {'image_bytes': content }}
params = {}
request = prediction_client.predict(name, payload, params)
return request # waits till request is returned
def get_pilon(latitude, longitude):
great_circle_radius = 6372.795
radius = 0.5
with open('web/VN_PB_KE_okolie.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
else:
print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
distance = acos(cos(deg2rad(latitude)) * cos(deg2rad(longitude)) * cos(deg2rad(row[0])) * cos(deg2rad(row[1]))
+ cos(deg2rad(latitude)) * sin(deg2rad(longitude)) * cos(deg2rad(row[0])) * sin(deg2rad(row[1]))
+ sin(deg2rad(latitude)) * sin(deg2rad(row[0]))
) * great_circle_radius
if (distance < radius) {
return row[0]
}
line_count += 1
@app.route('/')
def index():
return render_template("homepage.html")
@app.route('/map')
def map():
return render_template("map.html")
@app.route('/reports')
def reports():
return render_template("reports.html")
@app.route('/statistics')
def statistics():
return render_template("statistics.html")
@app.route('/photos')
def photos():
return render_template("AllPhotos.html")
@app.route('/unidentified-photos')
def not_defined_photos():
return render_template("UnidentifiedPhotoLayout.html")
@app.route('/api/v1/pilon', methods=['GET'])
def pilons():
count = get_hit_count()
return 'Helo World! I have been seen {} times.\n'.format(count)
@app.route('/api/v1/add', methods=['POST'])
def add():
path = '/web/photos'
if request.files:
image = request.file['image']
name = os.path.join(path, image.filename)
image.save(name)
data = gpsphoto.getGPSData(name)
prediction = get_prediction(content, '67264033942', 'IOD6491855299970859008')
pilonCode = get_pilon()
insertVariablesIntoTable(name, data['Latitude'], data['Longitude'], prediction, pilonCode)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)