This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathserver.py
161 lines (132 loc) · 5.76 KB
/
server.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import glob
import os
from PIL import Image
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
from flask import Flask, render_template, request, Response
from werkzeug.utils import secure_filename
import json
from capgen import CaptionGenerator
os.environ['CUDA_VISIBLE_DEVICES'] = ''
es = Elasticsearch()
gencap = CaptionGenerator()
def description_search(query):
global es
results = es.search(
index="desearch",
body={
"size": 20,
"query": {
"match": {"description": query}
}
})
hitCount = results['hits']['total']['value']
print(results)
if hitCount > 0:
if hitCount is 1:
print(str(hitCount), ' result')
else:
print(str(hitCount), 'results')
answers = []
max_score = results['hits']['max_score']
if max_score >= 0.35:
for hit in results['hits']['hits']:
if hit['_score'] > 0.5 * max_score:
desc = hit['_source']['description']
imgurl = hit['_source']['imgurl']
answers.append([imgurl, desc])
else:
answers = []
return answers
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join('static', 'database')
app.config['TEMP_UPLOAD_FOLDER'] = os.path.join('static', 'uploads')
app.config['ALLOWED_EXTENSIONS'] = set(['jpg', 'jpeg', 'png'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
return render_template('home.html')
@app.route('/search', methods=['GET', 'POST'])
def search():
global gencap
if request.method == 'POST':
if 'query_img' not in request.files or request.files['query_img'].filename == '' or not allowed_file(
request.files['query_img'].filename):
return render_template('search.html')
file = request.files['query_img']
img = Image.open(file.stream) # PIL image
uploaded_img_path = os.path.join(app.config['TEMP_UPLOAD_FOLDER'], file.filename)
img.save(uploaded_img_path)
query = gencap.get_caption(uploaded_img_path)
answers = description_search(query)
return render_template('search.html',
query_path=uploaded_img_path,
answers=answers)
else:
return render_template('search.html')
@app.route('/api/search', methods=['POST'])
def api_search():
global gencap
if 'query_img' not in request.files or request.files['query_img'].filename == '' or not allowed_file(
request.files['query_img'].filename):
return Response(response=json.dumps({'success': False, 'message': 'Uploaded image is invalid or not allowed'}),
status=400, mimetype="application/json")
file = request.files['query_img']
img = Image.open(file.stream) # PIL image
uploaded_img_path = os.path.join(app.config['TEMP_UPLOAD_FOLDER'], file.filename)
img.save(uploaded_img_path)
query = gencap.get_caption(uploaded_img_path)
answers = description_search(query)
return Response(response=json.dumps({'success': True, 'answers': answers}),
status=200, mimetype="application/json")
@app.route('/database')
def database():
images = glob.glob(os.path.join(app.config['UPLOAD_FOLDER'], '*'))
return render_template('database.html', database_images=images)
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
if 'photos' not in request.files:
return render_template('database.html')
actions = []
for file in request.files.getlist('photos'):
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
cap = gencap.get_caption(file_path)
doc = {'imgurl': file_path, 'description': cap}
actions.append(doc)
bulk(es, actions, index="desearch", doc_type="json")
return render_template('database.html')
@app.route('/caption', methods=['GET', 'POST'])
def caption():
if request.method == 'POST':
if 'query_img' not in request.files or request.files['query_img'].filename == '' or not allowed_file(
request.files['query_img'].filename):
return render_template('caption.html')
file = request.files['query_img']
img = Image.open(file.stream) # PIL image
uploaded_img_path = os.path.join(app.config['TEMP_UPLOAD_FOLDER'], file.filename)
img.save(uploaded_img_path)
cap = gencap.get_caption(uploaded_img_path)
return render_template('caption.html', caption=cap, query_path=uploaded_img_path)
else:
return render_template('caption.html')
@app.route('/api/caption', methods=['POST'])
def caption_api():
if 'query_img' not in request.files or request.files['query_img'].filename == '' or not allowed_file(
request.files['query_img'].filename):
return Response(response=json.dumps({'success': False, 'message': 'Uploaded image is invalid or not allowed'}),
status=400, mimetype="application/json")
file = request.files['query_img']
img = Image.open(file.stream) # PIL image
uploaded_img_path = os.path.join(app.config['TEMP_UPLOAD_FOLDER'], file.filename)
img.save(uploaded_img_path)
cap = gencap.get_caption(uploaded_img_path)
return Response(response=json.dumps({'success': True, 'caption': cap}),
status=200, mimetype="application/json")
if __name__ == "__main__":
app.run("127.0.0.1", debug=True)