-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
89 lines (73 loc) · 3.36 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
import os
import time
import sys
import shutil
import warnings
import uuid
import logging
import json
# Supress deprecation warnings
warnings.filterwarnings("ignore", category=FutureWarning)
from flask import Flask, render_template, request
import modelo
debug_output=False
min_confidence = 0.05
max_results = 5
pic_number = 0
application = Flask(__name__)
model_file = application.root_path + "/model.tflite"
label_file = application.root_path + "/dict.txt"
model = modelo.Model(model_file, label_file)
speciesInfoJSON = application.root_path + "/species_info.json"
file = open("species_info.json", encoding="utf-8")
data = json.load(file)
family_dict = dict()
secundary_name = dict()
iNaturalist_link = dict()
wikipedia_link = dict()
for species in data:
family_dict[species["name"]] = species["family"]
secundary_name[species["name"]] = species["otherName"]
iNaturalist_link[species["name"]] = species["iNatLink"]
wikipedia_link[species["name"]] = species["WikipediaLink"]
application.config["IMAGE_STATIC"] = application.root_path + "/static/images"
application.config["LOG_FILE"] = application.root_path + "/lepilens.log"
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=application.config['LOG_FILE'])
@application.route("/")
def initHTML():
return render_template('Model_WebPage.html',confidence_slider_initially=10)
@application.route("/classify_image", methods=["GET", "POST"])
def classify_image():
confidence_slider = request.form["confidence_slider"]
min_confidence = float(confidence_slider) / 100
if debug_output:
print(min_confidence)
dictionary = {}
if request.method == "POST":
if request.files:
images = request.files.getlist("image_to_classify")
if len(images) > 1 or images[0].filename != '':
for image in images:
print("%s / %s" % (image.filename, image.mimetype), file=sys.stderr)
if image.mimetype != 'image/png' and image.mimetype != 'image/jpeg':
logging.error('\'%s\' - invalid MIME type \'%s\'' % (image.filename, image.mimetype))
continue
_, extension = os.path.splitext(image.filename)
unique_filename = str(uuid.uuid4()) + extension
path_to_image = os.path.join(application.config["IMAGE_STATIC"], unique_filename)
image.save(path_to_image)
logging.info('\'%s\' - saved onto \'%s\'' % (image.filename,unique_filename))
t = time.time()
res_list = model.classify(path_to_image, max_results, min_confidence)
t = time.time() - t
logging.info('\'%s\' - %d results in %d milliseconds for min confidence level %f' % (image.filename,len(res_list), int(t * 1e+03), min_confidence))
dictionary[unique_filename] = res_list
for elem in res_list:
logging.info('\'%s\' - %s' % (image.filename, elem))
return render_template('Model_WebPage.html', confidence_slider_initially=confidence_slider,species_list=dictionary, family_dict=family_dict,
secundary_name=secundary_name, iNaturalist_link=iNaturalist_link, wikipedia_link=wikipedia_link)
if __name__ == "__main__":
application.run()