-
Notifications
You must be signed in to change notification settings - Fork 0
/
runflask.py
110 lines (90 loc) · 3.17 KB
/
runflask.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
import sys
from image import *
from flask import Flask, flash, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
import requests
import cv2
import logging
import image as image_module
logging.basicConfig(filename='backend.log', level=logging.DEBUG)
logger = logging.getLogger('backend')
UPLOAD_FOLDER = '../tmp/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
cbir = None
@app.route("/")
def hello():
return "Hello World from Flask!"
import cv2
from flask import Response
import json as js
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
from flask import json
@app.route('/image', methods=['POST'])
def image_url():
try:
image = request.files['imgurl']
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
logger.info(image.filename)
logger.info(request.form['databasename'])
image_module.databasename = request.form['databasename']
image_module.modelo = request.form['modelo']
if image_module.databasename != 'corel1000':
if image_module.modelo == 'grayscale':
image_module.histograms_filename = 'histograms_ox.txt'
else:
image_module.histograms_filename = 'histograms_ox_rgb.txt'
else:
if image_module.modelo == 'grayscale':
image_module.histograms_filename = 'histograms2.txt'
else:
image_module.histograms_filename = 'histograms.txt'
logger.info(image_module.histograms_filename)
global cbir
cbir = CBIR()
hists = cbir.run_process(f'../tmp/{filename}')
#img = cv2.imread(f'/tmp/{filename}')
#aux = to_grayscale(img)
#logger.info('o')
#histogram = calc_histograma(aux)
#build_tsne(histogram, hists, f'/tmp/{filename}')
logger.info('finished')
from collections import OrderedDict
response = app.response_class(
response=js.dumps(hists),
status=200,
mimetype='application/json'
)
return response
except Exception as e:
logger.error(str(e))
return 'error'
@app.route('/refilter', methods=['POST'])
def refilter():
global cbir
try:
dados = request.get_json()
tecnica = dados.pop()['tecnica']
nomeclasse = dados.pop()['classname']
if tecnica == 'qpm':
hists = cbir.refilter_imgs(dados)
elif tecnica == 'multiquery':
hists = cbir.multiple_query_point_search(dados)
elif tecnica == 'rfra':
hists = cbir.rfra(dados)
response = app.response_class(
response=js.dumps(hists),
status=200,
mimetype='application/json'
)
return response
except Exception as e:
logger.error(str(e))
return 'error'
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5000)