-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
54 lines (46 loc) · 1.97 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
import tensorflow as tf
import numpy as np
import logging
import mlib
from PIL import Image
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input
from flask import Flask, request, jsonify, render_template
from flask.logging import create_logger
app = Flask(__name__)
LOG = create_logger(app)
LOG.setLevel(logging.INFO)
# This line creates an instance of the MobileNetV2 neural network model from Keras
# with pre-trained weights on the ImageNet dataset.
model = MobileNetV2(weights="imagenet")
@app.route("/")
def home():
return render_template("view.html")
# Preprocessing an image before feeding it into a neural network, specifically a MobileNetV2 model. It
# resizes the image to a square shape with dimensions of 224x224 pixels. MobileNetV2, expect input
# images of a specific size.
# The deep learning framework Keras preprocesses the image array to be compatible with the MobileNetV2
# model. It may involve normalization and other transformations.
def prepare_image(image):
img = Image.open(image)
img = img.resize((224, 224))
img_array = np.array(img)
LOG.info(f"Format, resize and process image...")
img_array = preprocess_input(img_array)
img_array = np.expand_dims(img_array, axis=0)
return img_array
# The model return a list of the top 3 posible results with highest probability.
@app.route("/predict", methods=["POST"])
def predict():
image = request.files["image"]
LOG.info(f"Processing image...")
img_array = prepare_image(image)
LOG.info(f"Calling to model...")
prediction = model.predict(img_array)
results = tf.keras.applications.mobilenet_v2.decode_predictions(prediction, top=3)[0]
response = []
for result in results:
response.append({"label": result[1], "probability": float(result[2])})
return render_template("result.html", response=response)
# Expose the Flask application to port 8080.
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)