-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
156 lines (119 loc) · 5.37 KB
/
server.py
File metadata and controls
156 lines (119 loc) · 5.37 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
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
154
155
156
import os
from flask import Flask, render_template, flash, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
from src.soil_color import find_closest_soil_match_by_color
from src.monthly_rainfall import weather
from src.dominant_image_color import dominant_color
import sys
import cv2
import numpy
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
sys.path.insert(0, './src/')
import map_to_image
import dominant_image_color
sys.path.insert(0, './src/vision/')
from hslpipline import *
app = Flask(__name__, template_folder='web')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = 'assets'
ALLOWED_EXTENSIONS = set(['png', 'jpg'])
avg_rain = 0.0
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def process_image(filename="assets/fields/generated_fields/field0.png",
h=24, s=17, lum=41):
image = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
pipeline = HSLPipline(h, s, lum, filename)
pipeline.process(image)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
google = request.form['google']
if google != '':
lat = google[google.find('@')+1:]
lat = lat.split(',')
lon = lat[1]
lat = lat[0]
global avg_rain
filename = 'assets/fields/google_amps/google-maps1.png'
filepath = app.config['UPLOAD_FOLDER'] + '/' + filename
filename = secure_filename(filename)
map_to_image.save_image_for_google_maps_url(filepath, google)
process_image(filepath)
if 'favicon.ico' not in filename:
most_dominant_color = dominant_color(filepath)
soil_match = find_closest_soil_match_by_color(
most_dominant_color, 'data/soil.json')
return redirect(url_for('uploaded_file', filename=filename))
return render_template('home.html')
file = request.files['file']
coordinates = request.form['coordinates']
# if user does not select file, browser also
# submit an empty part without filename
if file and allowed_file(file.filename) and coordinates != '':
lat = coordinates
lon = lat[lat.find(',')+1:]
lat = lat[:lat.find(',')]
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
process_image(filepath)
if 'favicon.ico' not in filename:
most_dominant_color = dominant_color(filepath)
soil_match = find_closest_soil_match_by_color(most_dominant_color, 'data/soil.json')
return redirect(url_for('uploaded_file',
filename=filename))
return render_template('home.html')
@app.route('/<filename>', methods=['GET', 'POST', 'OPTIONS'])
def uploaded_file(filename):
filepath = app.config['UPLOAD_FOLDER'] + '/' + \
request.url.split('/')[len(request.url.split('/')) - 1]
if request.method == 'POST':
rgb = request.get_json()
rgb = numpy.uint8([[[rgb['B'], rgb['G'], rgb['R']]]])
hls = cv2.cvtColor(rgb, cv2.COLOR_BGR2HLS)
process_image(filepath, hls[0][0][0], hls[0][0][2], hls[0][0][1])
filename = app.config['UPLOAD_FOLDER'] + '/' + filename
if 'favicon.ico' not in filename:
most_dominant_color = dominant_image_color.dominant_color(filename)
soil_match = find_closest_soil_match_by_color(most_dominant_color,
'data/soil.json')
return render_template('info.html',
filename=filename,
soil_color_r=most_dominant_color[0],
soil_color_g=most_dominant_color[1],
soil_color_b=most_dominant_color[2],
soil_munsell_fine=soil_match['munsell_fine'],
soil_munsell_color=soil_match['munsell_color'],
soil_water_content=soil_match['water_content'],
soil_indicates=soil_match['indicates'][0],
precipitation=str(avg_rain))
@app.route('/assets/<filename>')
def send_assets(filename):
return send_from_directory('assets', filename)
@app.route('/css/<filename>')
def send_css(filename):
return send_from_directory('web/css', filename)
@app.route('/fonts/<filename>')
def send_fonts(filename):
return send_from_directory('web/fonts', filename)
@app.route('/img/<filename>')
def send_img(filename):
return send_from_directory('web/img', filename)
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('web/js', path)
@app.after_request
def add_header(response):
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)