-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleeaf_edge_server.py
121 lines (96 loc) · 3.77 KB
/
leeaf_edge_server.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
import json
import os
from datetime import datetime
import tensorflow as tf
from flask import Flask, request
from werkzeug.exceptions import HTTPException
from image_utils import image_coordinates
from twinbase_utils import find_closest_tree, load_twinbase_tree_pois
from yolo_utils import run_predictions_on_image, load_model
pois_filename = 'trees.json'
segmentation_model_filename = '../bench_models/model-m.pt'
classification_model_filename = '../bench_models/vgg19'
tree_pois = None
if os.path.exists(pois_filename):
print(f'loading pois...')
f = open(pois_filename)
tree_pois = json.load(f)['trees']
print(f'loaded pois')
else:
tree_pois = load_twinbase_tree_pois()
# load segmentation model
print(f'loading segmentation model...')
segmentation_model_filename = "../bench_models/model-m.pt"
model = load_model(segmentation_model_filename)
print(f'loaded: {segmentation_model_filename}')
# load classification model
print(f'loading classification model...')
classification_model = tf.keras.models.load_model(classification_model_filename)
print(f'loaded: {classification_model_filename}')
# start web server
print(f'loading web server...')
leeafEdge = Flask(__name__)
@leeafEdge.route("/")
def get_home():
return {'service': 'up',
'segmentation_model': segmentation_model_filename,
'classification_model': classification_model_filename}
@leeafEdge.route("/pois")
def get_trees():
return {'pois': tree_pois}
@leeafEdge.route('/<group>/<thing>/upload', methods=['POST'])
def uploadWithArgs(group, thing):
return parse_uploaded_image(group=group, thing=thing)
@leeafEdge.route('/upload', methods=['POST'])
def upload():
return parse_uploaded_image()
def parse_uploaded_image(group=None, thing=None):
if len(request.files.getlist('file')) > 0:
try:
filename = download_file(request.files.getlist('file')[0])
img_coordinates = None
try:
img_coordinates = image_coordinates(filename)
except KeyError as k:
pass
(tree, distance) = find_closest_tree(tree_pois, img_coordinates)
print(tree)
if group is None:
group = tree['group']
if thing is None:
tree_name = tree['uuid']
else:
tree_name = thing
date_string = datetime.now().strftime("%Y%m%d")
print(f'uploaded image for group:{group}, tree:{tree_name}, date: {date_string}, filename: {filename}')
count, predict_time, detections = run_predictions_on_image(model, classification_model, image_path="./",
image_name=filename, group=group, tree=tree_name,
date_string=date_string)
print(detections)
return {'tree': tree['name'], 'distance': distance, 'count': count, 'predict_time': predict_time}
except Exception as err:
raise err
else:
raise NoFilesProvided()
@leeafEdge.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = e.get_response()
# replace the body with JSON
response.data = json.dumps({
"code": e.code,
"name": e.name,
"description": e.description,
})
response.content_type = "application/json"
return response
class NoFilesProvided(HTTPException):
code = 400
description = 'no files found in request.'
def download_file(file):
file.save(file.filename)
return file.filename
if __name__ == "__main__":
# leeafEdge.config()
leeafEdge.run(host="0.0.0.0", port=30000, debug=False)