This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·188 lines (161 loc) · 4.08 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from flask import Flask
from tools import base64_to_numpy
from flask import request
from ia.model_loader import Model
import services.stats as statsService
import services.db as dbService
import json
from config import config
app = Flask(__name__, instance_relative_config=True)
@app.route('/statistics', methods=['GET'])
def statistics():
return json.dumps({
'pictureCount': dbService.getPictureCount(),
'contributionCount': dbService.getContributionCount(),
'downloadCount': statsService.getDownloadCount(),
'speciesCount': dbService.getSpeciesCount(),
'plantsCount': dbService.getPlantsCount()
}),200
@app.route('/map', methods=['GET'])
def map():
lat = request.args.get('lat')
long = request.args.get('long')
dlat = request.args.get('dlat')
dlong = request.args.get('dlong')
species = request.args.get('species')
year = request.args.get('year')
# Check request validity
## Mandatory args
if any( x==None for x in [lat,long,dlat,dlong] ):
return {},400
## Check type
try:
lat = float(lat)
long = float(long)
dlat = float(dlat)
dlong = float(dlong)
if year != None:
year = int(year)
if species != None:
species = int(species)
except ValueError:
return {},400
if dlat*dlong > 6:
return {},416
if (year != None) and (year < 0):
return {},400
# Logic
## Ask database
inputs = dbService.getAllInputsInRange((lat,long),(dlat,dlong),species,year)
## Reshape output
response = []
for input in inputs:
response.append({
"latitude":input.latitude,
"longitude":input.longitude,
"iaGuessedSpeciesId":input.iaGuessedSpeciesId,
})
## Send response
return json.dumps({"inputs":response}),200
@app.route('/query', methods=['POST'])
def query():
# TODO : Set a limit rate on this query to avoid some kind of denial service attacks or saturation of the storage space of the database
lat = request.args.get('lat')
long = request.args.get('long')
# Check request validity
## Mandatory args
if any( x==None for x in [lat,long] ):
return {},400
## Check type
try:
lat = float(lat)
long = float(long)
except ValueError:
return {},400
# Handle request body
## Extract
data = request.get_json()
## Image must have been transfered
if not ("image64" in data):
return {},400
image_base64 = data["image64"]
## Reshape
try:
image = base64_to_numpy(image_base64)
except AttributeError:
# If unable to reshape : image have been given in wrong format or ratio
return {},400
except ValueError:
# If unable to reshape : image have been given in wrong format or ratio
return {},400
# logic
## Predict
predict,predictionList = model.predict_one(image)
predict = int(predict)
## Save entry into the database
dbService.addInput(image_base64,predict,lat,long,predictionList)
## Send response
return json.dumps({
"prediction": predict
}),201
@app.route('/species/wiki', methods=['GET'])
def species():
nominal_number = request.args.get('nominalNumber')
# Check request validity
## Mandatory args
if any( x==None for x in [nominal_number] ):
return {},400
## Check type
try:
nominal_number = int(nominal_number)
except ValueError:
return {},400
# logic
species = dbService.getOneSpecies(nominal_number)
if species is not None:
return json.dumps(
{
"name":species.name,
"scientificName":species.scientificName,
"refImage":species.refImage,
"stats": {
"water":species.stats.water,
"light":species.stats.light,
"toxicity":species.stats.toxicity,
}
}
),200
else :
return {}, 404
@app.route('/species/list', methods=['GET'])
def speciesList():
page = request.args.get('page')
# Check request validity
## Check type
try:
if page == None:
page = 0
else:
page = int(page)
except ValueError:
return {}, 400
if page < 0:
return {}, 400
# logic
species = [
{
'nominalNumber':s.nominalNumber,
'name':s.name,
'image':s.refImage,
}
for s in dbService.getSpeciesList(page) ]
if len(species) == 0:
return {}, 404
else:
return json.dumps({"species":species}),200
model = Model()
app.run(
debug=config.get('WEB.DEBUG'),
port=config.get('WEB.PORT'),
host='0.0.0.0'
)