-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
48 lines (43 loc) · 1.31 KB
/
main.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
#Imports
import os,io
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from PIL import Image
from tensorflow import keras
from flask import Flask, request, jsonify
#Load Model Architecture with weights
model = tf.keras.models.load_model("final_model.h5", custom_objects={'KerasLayer': hub.KerasLayer})
#Process the image and return Tensor
def process(image):
data=np.asarray(image)
data=data/255.0
data=tf.image.resize(data,[224,224])
return data
#Return Prediction Result
def predict(tensor):
predictions=model(np.array([tensor]))
res=np.argmax(predictions)
return res
#Instantiate the App
app = Flask(__name__)
#Define Routes
@app.route("/api/image",methods=["GET","POST"])
#index
def index():
if request.method=="POST":
image_file = request.files['image']
image_file.save('received_image.jpg')
try:
file=open("received_image.jpg", 'rb')
image_bytes=file.read()
image=Image.open(io.BytesIO(image_bytes))
tensor = process(image)
prediction=predict(tensor)
data={"prediction":int(prediction)}
return jsonify(data)
except Exception as e:
return jsonify({"error":str(e)})
return "Server up and Running !"
if __name__ == "__main__":
app.run(debug=True)