Spaces:
Runtime error
Runtime error
Isabel Gwara
commited on
Commit
·
5507b4f
1
Parent(s):
46b3682
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import keras
|
| 2 |
+
from keras.models import load_model
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
from skimage import transform
|
| 6 |
+
import h5py
|
| 7 |
+
|
| 8 |
+
model = load_model('model.h5') # single file model
|
| 9 |
+
labels = ['cheetah', 'hyena', 'jaguar', 'tiger'] # will need to be loaded from file in the order of your directories
|
| 10 |
+
|
| 11 |
+
def preprocess(image):
|
| 12 |
+
image = np.array(image) / 255
|
| 13 |
+
image = transform.resize(image, (300, 300, 3))
|
| 14 |
+
image = np.expand_dims(image, axis=0)
|
| 15 |
+
return image
|
| 16 |
+
|
| 17 |
+
def match_prediction_with_label(test_img):
|
| 18 |
+
image = preprocess(test_img)
|
| 19 |
+
pred = model.predict(image)
|
| 20 |
+
results_dict = {}
|
| 21 |
+
for row in pred:
|
| 22 |
+
for idx, item in enumerate(row):
|
| 23 |
+
results_dict[labels[idx]] = float(item)
|
| 24 |
+
return results_dict
|
| 25 |
+
|
| 26 |
+
image = gr.inputs.Image(shape=(300, 300), label="Upload Your Image Here")
|
| 27 |
+
label = gr.outputs.Label(num_top_classes=4)
|
| 28 |
+
|
| 29 |
+
gr.Interface(fn=match_prediction_with_label, inputs=image, outputs=label, capture_session=True).launch(share=True, debug=True)
|