-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (36 loc) · 1.54 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
from flask import Flask, request
from werkzeug.utils import secure_filename
from session_package.session import Session
session = Session()
app = Flask(__name__)
@app.route('/')
def index():
return {'message': 'feature classification service'}
@app.route('/api/info', methods=['GET'])
def info():
return {'message': session.describe_dataset()}
@app.route('/api/add/<label>', methods=['POST'])
def add(label):
if session.authorization and not session.check_token(request.headers['Authorization']):
return {'error': 'unauthorized'}
session.clear_images()
if 'image' not in request.files:
return {'error': 'no image part'}
image = request.files['image']
if image.filename == '':
return {'error': 'image filename is empty'}
if image and session.check_image_extension(image.filename):
filename = secure_filename(image.filename)
image_path = f'{session.images_directory}/{filename}'
image.save(image_path)
session.add(image_path, label)
return {'message': 'image features added to dataset'}
else:
return {'error': 'invalid image extension'}
@app.route('/api/classify', methods=['POST'])
def classify():
if session.authorization and not session.check_token(request.headers['Authorization']):
return {'error': 'unauthorized'}
features_json = request.get_json(force=True)
label, success, err = session.classify(features_json)
return {'message': label} if success else {'error': err}