-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
76 lines (62 loc) · 1.93 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
import os
# from shutil import copy2
from flask import Flask, render_template, redirect, request, url_for, session
from flask_pymongo import PyMongo
import numpy as np
from colordescriptor import ColorDescriptor
import gridfs as gfs
from searcher_mdb import Searcher
import cv2
app = Flask(__name__)
app.config['MONGO_URI'] = 'mongodb://localhost:27017/FlickrDB'
mongo = PyMongo(app)
fs = gfs.GridFS(mongo.db)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_image():
# check if a valid image was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
src=os.path.abspath(file.filename)
#copy2(src,'static')
return render_template(
'upload.html', results = search(file.filename), filename=file.filename
)
return render_template('upload.html')
def search(filename):
#print(query)
cd = ColorDescriptor((8, 12, 3))
query = cv2.imread(filename)
query = cv2.resize(query,(416,416))
filepath='static/'+filename
cv2.imwrite(filepath, query)
features = cd.describe(query)
searcher = Searcher()
results = searcher.search(features)
#cv2.imshow("Query", query)
# loop over the results
for (score, resultID) in results:
# load the result image and display it
#print(resultID)
img = mongo.db.myImages.find({'filename': resultID},{"_id": 0 , "name": 0, "filename": 0})
l=[]
for x in img:
#print(x)
x = x['images'][0]
#print('hey')
#print(x)
fout = fs.get(x['imageID'])
im = np.frombuffer(fout.read(), dtype=np.uint8)
im = np.reshape(im, x['shape'])
# cv2.imshow("Result", im)
# cv2.waitKey(0)
return results
if __name__ == '__main__':
app.run(debug = True)