Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,50 @@
|
|
1 |
-
import tensorflow as tf
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
image =
|
16 |
-
|
17 |
-
|
18 |
-
predictions =
|
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 |
demo.launch()
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow import keras
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
import os
|
7 |
+
|
8 |
+
classes = ["Abyssinian", "Bengal", "Birman", "Bombay", "British Shorthair", "Egyptian Mau", "Maine Coon", "Persian", "Ragdoll", "Russian Blue", "Siamese", "Sphynx"]
|
9 |
+
example_images = ["examples/" + f for f in os.listdir("examples")]
|
10 |
+
|
11 |
+
img_size = 400
|
12 |
+
model = tf.keras.models.load_model("CatClassifier.keras")
|
13 |
+
|
14 |
+
def model_predict(image):
|
15 |
+
image = cv2.resize(image, (img_size, img_size))
|
16 |
+
image = np.expand_dims(image, axis=0)
|
17 |
+
|
18 |
+
predictions = model.predict(image)
|
19 |
+
predictions = predictions[0]
|
20 |
+
|
21 |
+
predicted_class_index = np.argmax(predictions)
|
22 |
+
predicted_class = classes[predicted_class_index]
|
23 |
+
pred_dict = {}
|
24 |
+
|
25 |
+
for i in range(len(classes)):
|
26 |
+
pred_dict[classes[i]] = predictions[i]
|
27 |
+
|
28 |
+
return predicted_class, pred_dict
|
29 |
+
|
30 |
+
|
31 |
+
def predict_breed(image):
|
32 |
+
if image is None:
|
33 |
+
return "Please attach an image first!", None
|
34 |
+
|
35 |
+
return model_predict(image)
|
36 |
+
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column():
|
40 |
+
image_input = gr.Image(label="Cat Image")
|
41 |
+
run_button = gr.Button(variant="primary")
|
42 |
+
examples = gr.Examples(example_images,inputs=image_input)
|
43 |
+
with gr.Column():
|
44 |
+
breed_output = gr.Text(label="Predicted Breed", interactive=False)
|
45 |
+
predict_labels = gr.Label(label="Class Probabilties")
|
46 |
+
|
47 |
+
run_button.click(fn=predict_breed, inputs=image_input, outputs=[breed_output, predict_labels])
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
demo.launch()
|