Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
import tensorflow_addons as tfa
|
6 |
+
import os
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
|
10 |
+
# labels= {'Burger King': 0, 'KFC': 1,'McDonalds': 2,'Other': 3,'Starbucks': 4,'Subway': 5}
|
11 |
+
HEIGHT,WIDTH=224,224
|
12 |
+
NUM_CLASSES=6
|
13 |
+
|
14 |
+
model=load_model('Models/best_model1.h5')
|
15 |
+
|
16 |
+
# def classify_image(inp):
|
17 |
+
# np.random.seed(143)
|
18 |
+
# inp = inp.reshape((-1, HEIGHT,WIDTH, 3))
|
19 |
+
# inp = tf.keras.applications.nasnet.preprocess_input(inp)
|
20 |
+
# prediction = model.predict(inp)
|
21 |
+
# ###label = dict((v,k) for k,v in labels.items())
|
22 |
+
# predicted_class_indices=np.argmax(prediction,axis=1)
|
23 |
+
# result = {}
|
24 |
+
# for i in range(len(predicted_class_indices)):
|
25 |
+
# if predicted_class_indices[i] < NUM_CLASSES:
|
26 |
+
# result[labels[predicted_class_indices[i]]]= float(predicted_class_indices[i])
|
27 |
+
# return result
|
28 |
+
|
29 |
+
def classify_image(inp):
|
30 |
+
np.random.seed(143)
|
31 |
+
labels = {'Cat': 0, 'Dog': 1}
|
32 |
+
NUM_CLASSES = 2
|
33 |
+
inp = inp.reshape((-1, HEIGHT, WIDTH, 3))
|
34 |
+
inp = tf.keras.applications.nasnet.preprocess_input(inp)
|
35 |
+
prediction = model.predict(inp)
|
36 |
+
predicted_class_indices = np.argmax(prediction, axis=1)
|
37 |
+
|
38 |
+
label_order = ["Cat","Dog"]
|
39 |
+
|
40 |
+
result = {label: float(f"{prediction[0][labels[label]]:.6f}") for label in label_order}
|
41 |
+
|
42 |
+
return result
|
43 |
+
|
44 |
+
|
45 |
+
image = gr.Image(shape=(HEIGHT,WIDTH),label='Input')
|
46 |
+
label = gr.Label(num_top_classes=2)
|
47 |
+
|
48 |
+
gr.Interface(fn=classify_image, inputs=image, outputs=label, title='Smart Pet Classifier').launch(debug=False)
|
49 |
+
|
50 |
+
|