-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (31 loc) · 1.08 KB
/
main.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
from flask import Flask, jsonify, request
from utils import transform_image, get_prediction
app = Flask(__name__)
ALLOWED_EXTN = ('png', 'jpg', 'jpeg')
def allowed_file(filename):
## example: abc.png
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTN
@app.route('/predict', methods=['POST'])
def predict():
# 1. Loading the Image
# 2. Image -> Tensor
# 3. prediction
# 4. return json
if request.method == 'POST':
file = request.files.get('file')
if file is None or file.filename == '':
return jsonify({'error': "No file."})
if not allowed_file(file.filename):
return jsonify({'error': 'Format not supported.'})
try:
image_byte = file.read()
tensor = transform_image(image_byte)
prediction = get_prediction(tensor)
data = {
'prediction': prediction.item(),
'class_name': str(prediction.item())
}
return jsonify(data)
except:
return jsonify({'error': 'error during prediction.'})
return jsonify({'message': 'Prediction API is working. Use POST method to get predictions.'})