-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (55 loc) · 1.86 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
57
58
59
60
61
62
63
import os
import string
import random
import json
import requests
import numpy as np
import tensorflow as tf
from flask import Flask, request, redirect, url_for, render_template
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
"""
Constants
"""
MODEL_URI = 'http://localhost:8502/v1/models/pets:predict'
OUTPUT_DIR = 'static'
CLASSES = ['Cat', 'Dog']
SIZE = 128
"""
Utility functions
"""
def generate_filename():
return ''.join(random.choices(string.ascii_lowercase, k=20)) + '.jpg'
def get_prediction(image_path):
image = tf.keras.preprocessing.image.load_img(image_path, target_size=(SIZE, SIZE))
image = tf.keras.preprocessing.image.img_to_array(image)
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
image = np.expand_dims(image, axis=0)
data = json.dumps({'instances': image.tolist() })
response = requests.post(MODEL_URI, data=data.encode())
result = json.loads(response.text)
prediction = result['predictions'][0]
class_name = CLASSES[int(prediction > 0.5)]
return class_name
"""
Routes
"""
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
uploaded_file = request.files['file']
if uploaded_file.filename != '':
if uploaded_file.filename[-3:] in ['jpg', 'png']:
image_path = os.path.join(OUTPUT_DIR, generate_filename())
uploaded_file.save(image_path)
class_name = get_prediction(image_path)
result = {
'class_name': class_name,
'path_to_image': image_path,
'size': SIZE
}
return render_template('show.html', result=result)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)