-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (93 loc) · 2.75 KB
/
main.py
File metadata and controls
111 lines (93 loc) · 2.75 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Import the needed modules for prediction
from flask import Flask, request
import os
import tensorflow as tf
import numpy as np
import json
import sys
# Set the GPU setting Support
GPU_SETTING = False
# If False then not using GPU and use CPU instead
if GPU_SETTING != True:
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# If true then using GPU instead using CPU
else:
# Check for list for GPU Support in server or computer and set it to True
gpu_devices = tf.config.experimental.list_physical_devices("GPU")
for device in gpu_devices:
tf.config.experimental.set_memory_growth(device, True)
# Define Classes in Dictionary and Load Created Model Previously
classes = {
'A': 0,
'B': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7,
'I': 8,
'J': 9,
'K': 10,
'L': 11,
'M': 12,
'N': 13,
'O': 14,
'P': 15,
'Q': 16,
'R': 17,
'S': 18,
'T': 19,
'U': 20,
'V': 21,
'W': 22,
'X': 23,
'Y': 24,
'Z': 25}
# Load Model and show the Summary of the Model
model = tf.keras.models.load_model('model_SIBI.h5')
model.summary()
# Define the Flask App
app = Flask(__name__)
### Hello world route to make sure the server running
@app.route('/')
def hello():
return 'Hello World!'
### route request Prediction from JSON android
@app.route('/predict', methods=['POST'])
def predict():
#If there's an input from Android
request_json = request.json
print("data: {}".format(request_json), file=sys.stderr)
print("type: {}".format(type(request_json), file=sys.stderr))
# Convert into a Array for Prediction
IMG_array = np.array(request_json.get('data'))
print(IMG_array.shape, file=sys.stderr)
# Feed input Array Into the Prediction
predictions = model.predict(IMG_array)
# Using numpy.argmax to find which class between 26 classes
# have the highest probability between them
# and Find classes using keys that found before in numpy.argmax
for alphabets, values in classes.items():
if values == np.argmax(predictions):
out_val = alphabets
# Edit Received JSON file to response to the Android with prediction
response_json = {
"data" : request_json.get("data"),
"prediction" : str(out_val)
}
# Send Back the Prediction
return json.dumps(response_json)
### route request PING from JSON android
@app.route('/ping', methods=['POST'])
def ping():
request_json = request.json
print("data: {}".format(request_json))
print("type: {}".format(type(request_json)))
response_json = {
"data" : request_json.get("data"),
"answer" : str("Server is Online")
}
return json.dumps(response_json)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)