Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
from tensorflow.keras.models import load_model
|
| 4 |
-
import tensorflow_addons as tfa
|
| 5 |
-
import os
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
NUM_CLASSES=6
|
| 12 |
-
|
| 13 |
-
model=load_model('best_model2.h5')
|
| 14 |
-
|
| 15 |
-
# def classify_image(inp):
|
| 16 |
-
# np.random.seed(143)
|
| 17 |
-
# inp = inp.reshape((-1, HEIGHT,WIDTH, 3))
|
| 18 |
-
# inp = tf.keras.applications.nasnet.preprocess_input(inp)
|
| 19 |
-
# prediction = model.predict(inp)
|
| 20 |
-
# ###label = dict((v,k) for k,v in labels.items())
|
| 21 |
-
# predicted_class_indices=np.argmax(prediction,axis=1)
|
| 22 |
-
# result = {}
|
| 23 |
-
# for i in range(len(predicted_class_indices)):
|
| 24 |
-
# if predicted_class_indices[i] < NUM_CLASSES:
|
| 25 |
-
# result[labels[predicted_class_indices[i]]]= float(predicted_class_indices[i])
|
| 26 |
-
# return result
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
def classify_image(inp):
|
| 32 |
np.random.seed(143)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
inp = inp.reshape((-1, HEIGHT, WIDTH, 3))
|
| 36 |
inp = tf.keras.applications.nasnet.preprocess_input(inp)
|
| 37 |
-
prediction = model.predict(inp)
|
| 38 |
-
predicted_class_indices = np.argmax(prediction, axis=1)
|
| 39 |
-
|
| 40 |
-
label_order = ["Burger King", "KFC", "McDonalds", "Other", "Starbucks", "Subway"]
|
| 41 |
-
|
| 42 |
-
result = {label: float(f"{prediction[0][labels[label]]:.6f}") for label in label_order}
|
| 43 |
-
|
| 44 |
-
return result
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
image = gr.Image(shape=(HEIGHT,WIDTH),label='Input')
|
| 58 |
-
label = gr.Label(num_top_classes=4)
|
| 59 |
-
|
| 60 |
-
gr.Interface(fn=classify_image, inputs=image, outputs=label, title='Brand Logo Detection').launch(debug=False)
|
| 61 |
-
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Configuration
|
| 7 |
+
HEIGHT, WIDTH = 224, 224
|
| 8 |
+
NUM_CLASSES = 6
|
| 9 |
+
LABELS = ["Burger King", "KFC", "McDonalds", "Other", "Starbucks", "Subway"]
|
| 10 |
|
| 11 |
+
# Loading trained model
|
| 12 |
+
model = tf.keras.models.load_model('best_model2.h5')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def classify_image(inp):
|
| 15 |
np.random.seed(143)
|
| 16 |
+
|
| 17 |
+
# Preprocess input
|
| 18 |
inp = inp.reshape((-1, HEIGHT, WIDTH, 3))
|
| 19 |
inp = tf.keras.applications.nasnet.preprocess_input(inp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Prediction
|
| 22 |
+
prediction = model.predict(inp)
|
| 23 |
+
# Build a dict of label:confidence
|
| 24 |
+
return {LABELS[i]: float(f"{prediction[0][i]:.6f}") for i in range(NUM_CLASSES)}
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=classify_image,
|
| 29 |
+
inputs=gr.Image(
|
| 30 |
+
label="Input Image",
|
| 31 |
+
source="upload", # or "sketchpad", "webcam"
|
| 32 |
+
type="numpy", # pass as numpy array to your function
|
| 33 |
+
height=HEIGHT, # set display height :contentReference[oaicite:0]{index=0}
|
| 34 |
+
width=WIDTH # set display width :contentReference[oaicite:1]{index=1}
|
| 35 |
+
),
|
| 36 |
+
outputs=gr.Label(num_top_classes=4),
|
| 37 |
+
title="Brand Logo Detection"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch(debug=False)
|