-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (39 loc) · 1.42 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
import os
from flask import Flask, render_template, request, jsonify
from pyimagesearch.colordescriptor import ColorDescriptor
from pyimagesearch.searcher import Searcher
# create flask instance
app = Flask(__name__)
INDEX = os.path.join(os.path.dirname(__file__), 'index.csv')
# main route
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search', methods=['POST'])
def search():
if request.method == "POST":
RESULTS_ARRAY = []
# get url
image_url = request.form.get('img')
try:
# initialize the image descriptor
cd = ColorDescriptor((8, 12, 3))
# load the query image and describe it
from skimage import io
query = io.imread(image_url)
features = cd.describe(query)
# perform the search
searcher = Searcher(INDEX)
results = searcher.search(features)
# loop over the results, displaying the score and image name
for (score, resultID) in results:
RESULTS_ARRAY.append(
{"image": str(resultID), "score": str(score)})
# return success
return jsonify(results=(RESULTS_ARRAY[::-1][:3]))
except:
# return error
jsonify({"sorry": "Sorry, no results! Please try again."}), 500
# run!
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)