-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (34 loc) · 1.37 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
import os
from flask import Flask, request
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
import tempfile
from entities import Image
from core import locate
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@cross_origin()
@app.route('/images', methods = ['POST'])
def upload_file():
image = request.files['image']
query_image = request.files['queryImage']
# create a temporary directory
with tempfile.TemporaryDirectory() as directory:
filename = secure_filename(image.filename)
image.save(os.path.join(directory, filename))
query_filename = secure_filename(query_image.filename)
query_image.save(os.path.join(directory, query_filename))
image = Image(os.path.join(directory, filename))
template = Image(os.path.join(directory, query_filename), cluster=image.cluster)
max_contribution, max_offset = locate(image, template)
return dict(location=max_offset, score=max_contribution)
# use this in order to skip the save part
# # read image file string data
# filestr = request.files['file'].read()
# # convert string data to numpy array
# npimg = numpy.fromstring(filestr, numpy.uint8)
# # convert numpy array to image
# img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
if __name__ == '__main__':
app.run(debug = True)