-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
96 lines (78 loc) · 2.18 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from flask import Flask, request, Response,render_template
from werkzeug.utils import secure_filename
import json
from db import db_init, db
from model import Img
import random
from algo import algo, algo
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///img.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db_init(app)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/game')
def gamePlay():
return render_template("start.html")
@app.route('/uploadFile')
def uploadFile():
return render_template("upload.html")
@app.route('/upload', methods=['POST'])
def upload():
# print(request.data['file'])
pic = request.files['pic']
class_type = (request.form.to_dict()['attrib'])
if not pic:
return {
'success': False,
'message': 'File not uploaded',
'resCode': 400
}
filename = secure_filename(pic.filename)
mimetype = pic.mimetype
if not filename or not mimetype:
return {
'success': False,
'message': 'Bad upload.',
'resCode': 400
}
img = Img(img=pic.read(), name=filename, mimetype=mimetype, class_type = class_type)
db.session.add(img)
db.session.commit()
return {
'success': True,
'message': 'sucessfully uploaded',
'resCode': 200
}
@app.route('/getImage/<int:id>')
def get_img(id):
img = Img.query.filter_by(id=id).first()
if not img:
return 'Img Not Found!', 404
print(img.img)
return Response(img.img, mimetype=img.mimetype)
attribute = ''
@app.route('/getRandomImage')
def getRandom():
queryLen = len(Img.query.all())
id_random = random.randint(1,queryLen)
img = Img.query.filter_by(id=id_random).first()
global attribute
attribute = img.class_type
return Response(img.img, mimetype=img.mimetype)
@app.route('/getRandomImage/attribute', methods=['GET'])
def getAttrib():
return {
'attrib': attribute
}
@app.route('/get_data', methods=["POST","GET"])
def getAPI():
data= json.loads(request.data)
res = algo(data)
return {
"success" : True,
"res": res
}
if __name__ == "__main__":
app.run(debug=True)