-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
40 lines (31 loc) · 1.01 KB
/
api.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
from flask import Flask, request, url_for
from werkzeug import secure_filename
import os
from flask.ext.cors import CORS
from src import solver
UPLOAD_FOLDER = '/tmp/wordbase-images'
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'png'])
app = Flask(__name__)
cors = CORS(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def hello():
return "Hello World!"
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
if not os.path.isdir(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if file and allowed_file(file.filename):
color = request.form["color"]
filename = secure_filename(file.filename)
img_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(img_path)
res = solver.solve(color, img_path, 'src/Word-List.txt')
return " ".join([x[0] for x in res])
return "failure"
if __name__ == "__main__":
app.run(debug=True)