-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
84 lines (62 loc) · 2.55 KB
/
api.py
File metadata and controls
84 lines (62 loc) · 2.55 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Author: Akawi Ifeanyi Courage
E-mail address: Ifeanyi.akawi85@gmail.com
Purpose of program: Predict Ethnicity model
"""
# Import necessary libraries
from flask import Flask, render_template, request
import numpy as np
from keras.preprocessing import image
import tensorflow as tf
# load model
interpreter_file = tf.lite.Interpreter(model_path="./model_dir/model.tflite")
interpreter_file.allocate_tensors()
print('@@ Model loaded')
# -----------------------------------------------------------------------------------
def predict_image(interpreter, imagefile):
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
test_image = image.load_img(imagefile, target_size = (224, 224))
test_image = image.img_to_array(test_image).astype(np.uint8)
test_image = np.expand_dims(test_image, axis=0)
interpreter.set_tensor(input_details[0]['index'], test_image)
# Run inference.
interpreter.invoke()
# Post-processing: remove batch dimension and find the digit with highest
# probability.
output = interpreter.tensor(output_details[0]['index'])
digit = np.argmax(output()[0])
print('--->>>', digit)
pred_class = {0: 'Asian American',
1: 'Black American'}
return 'The Profile Picture Is {}!'.format(pred_class.get(digit))
# ------------>>Toyota model prediction<<--end
# Create flask instance
app = Flask(__name__)
#----------------------------------------------------------------
#render index.html page
@app.route("/", methods=['GET', 'POST'])
def home():
""" Display the home page"""
return render_template('./index.html')
# ---------------------------------------------
@app.route("/predict", methods=['GET', 'POST'])
def predict():
"""
Save the uploaded image, predict the uploaded image class
And render the ./predict.html page
"""
if request.method == 'POST':
file = request.files['image'] # fetch input
filename = file.filename
print("@@ Input posted = ", filename)
file_path = './static/user_uploaded/'+str(filename)
file.save(file_path)
print("@@ Predicting class......")
pred_file = predict_image(interpreter=interpreter_file, imagefile=file_path)
return render_template('./predict.html',
pred_output=pred_file, user_image=file_path)
# For local system
# write the main function
if __name__ == '__main__':
app.run(debug=True)