-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
81 lines (54 loc) · 2.16 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
import os
import cv2
import numpy as np
from flask import Flask, render_template, request, jsonify, redirect, url_for
from werkzeug.utils import secure_filename
from features.colordescriptor import ColorDescriptor
from features.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', preview="static/init-preview.png")
# image database url list route
@app.route('/list', methods=['POST'])
def image_list():
if request.method == "POST":
try:
imgList = [img for img in list(os.listdir(os.path.join(os.path.dirname(__file__), 'static/images/'))) if img[-4:] in ('.png', '.jpg', '.gif')]
return jsonify(imgList=imgList)
except Exception as e:
return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
# search route
@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
import cv2
query = cv2.imread(os.path.join(os.path.dirname(__file__), 'static/images/'+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[:15]), preview="images/"+image_url)
except Exception as e:
print(str(e))
# return error
return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
# run!
if __name__ == '__main__':
app.run('127.0.0.1', debug=True)