-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
43 lines (32 loc) · 1.16 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
import os
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from model.fruitnet import FruitNet
app = Flask(__name__)
CORS(app)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 # 2 MB
MODEL_PATH = os.path.join(
'config',
'fruit_classifier_v3_transfer_learning.h5'
)
model = FruitNet(MODEL_PATH)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET'])
def main():
return render_template('index.html')
@app.route('/', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'message': 'No image found.'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'message': 'Image wasn\'t selected.'}), 400
if file and allowed_file(file.filename):
label, percentage = model.predict(file.read())
return jsonify({ 'label': label, 'percentage': percentage })
return jsonify({'message': 'Only JPEG or PNG images are allowed.'}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0')