Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,4 +3,48 @@ import numpy as np
|
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
def load_h5_model(model_path):
|
| 7 |
+
loaded_model = load_model(model_path)
|
| 8 |
+
return(loaded_model)
|
| 9 |
+
|
| 10 |
+
def prepro_img(img):
|
| 11 |
+
# resize the image to 130x130
|
| 12 |
+
img = img.resize((130,130))
|
| 13 |
+
# converto it to array with shape (1,130,130,3)
|
| 14 |
+
img_array = np.array(img)
|
| 15 |
+
img_array = np.array([img_array])
|
| 16 |
+
# return result
|
| 17 |
+
return(img_array)
|
| 18 |
+
|
| 19 |
+
def make_prediction(img):
|
| 20 |
+
# preprocess image
|
| 21 |
+
img = prepro_img(img)
|
| 22 |
+
# make prediction
|
| 23 |
+
prediction = model.predict(img)
|
| 24 |
+
prediction = int(prediction[0][0])
|
| 25 |
+
# return prediction label
|
| 26 |
+
if prediction == 1:
|
| 27 |
+
return('Uninfected cell')
|
| 28 |
+
else:
|
| 29 |
+
return('Parasitized cell')
|
| 30 |
+
|
| 31 |
+
model = load_h5_model('cell_classifier_model.h5')
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
with gr.Row():
|
| 35 |
+
with gr.Column():
|
| 36 |
+
cell_img = gr.Image(label="Cell Image",
|
| 37 |
+
type='pil')
|
| 38 |
+
examples = gr.Examples(['para_1.png', 'para_2.png', 'para_3.png'],
|
| 39 |
+
inputs=cell_img,
|
| 40 |
+
label='Parasitized Cells')
|
| 41 |
+
examples = gr.Examples(['uninf_1.png', 'uninf_2.png', 'uninf_3.png'],
|
| 42 |
+
inputs=cell_img,
|
| 43 |
+
label='Uninfected Cells')
|
| 44 |
+
with gr.Column():
|
| 45 |
+
cell_class = gr.Label(value='...')
|
| 46 |
+
|
| 47 |
+
predict_btn = gr.Button("Predict")
|
| 48 |
+
predict_btn.click(fn=make_prediction, inputs=cell_img, outputs=cell_class)
|
| 49 |
+
|
| 50 |
+
demo.launch(debug=True)
|