-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
36 lines (27 loc) · 1 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
# https://stackoverflow.com/a/60137874
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from flask import Flask, render_template, request, jsonify
from tensorflow.keras.models import load_model
import cv2
import numpy as np
app = Flask(__name__)
model = load_model("models/eatable_model_reg.keras") # Load the model (regression version)
@app.route("/")
def main():
return render_template("index.html")
@app.route("/predict", methods=["POST"])
def predict():
# Get the image from the form
image = request.files["image"]
# Load, resize, reshape the image for input to the model
image = cv2.imdecode(np.frombuffer(image.read(), np.uint8), cv2.IMREAD_COLOR)
image = cv2.resize(image, (256, 256))
image = image.reshape((1, 256, 256, 3))
# Make a prediction
prediction = model.predict(image)
# Extract the prediction
prediction = prediction[0][0]
prediction = str("%.2f" % round(prediction, 2))
# Return the prediction
return jsonify({"prediction": prediction})