File size: 2,191 Bytes
159fb0f
 
 
 
6f7f6f4
6d4bbd5
0e3827e
 
52af948
8ee8c7a
e630870
 
159fb0f
0e3827e
eb16681
5b3415e
 
 
 
 
 
 
 
 
 
 
 
 
 
74a719c
5b3415e
2e4ea84
 
5b3415e
 
 
 
 
c2f9e23
 
 
 
 
 
 
ea1b8bd
 
 
433c169
ea1b8bd
aff8b35
433c169
a163b32
c2f9e23
bae418a
c2f9e23
2cd30e9
0e3827e
159fb0f
0e3827e
eb16681
0b4701f
74a719c
26a7280
159fb0f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import load_model
import tensorflow_addons as tfa
import os
import numpy as np


# labels= {'Burger King': 0, 'KFC': 1,'McDonalds': 2,'Other': 3,'Starbucks': 4,'Subway': 5}
HEIGHT,WIDTH=224,224
NUM_CLASSES=6

model=load_model('best_model.h5')

# def classify_image(inp):
#   np.random.seed(143)
#   inp = inp.reshape((-1, HEIGHT,WIDTH, 3))
#   inp = tf.keras.applications.nasnet.preprocess_input(inp) 
#   prediction = model.predict(inp)
#   ###label = dict((v,k) for k,v in labels.items())
#   predicted_class_indices=np.argmax(prediction,axis=1)
#   result = {}
#   for i in range(len(predicted_class_indices)):
#       if predicted_class_indices[i] < NUM_CLASSES:
#           result[labels[predicted_class_indices[i]]]= float(predicted_class_indices[i])
#   return result 


def classify_image(inp):
    np.random.seed(143)
    labels = {'Burger King': 0, 'KFC': 1, 'McDonalds': 2, 'Other': 3, 'Starbucks': 4, 'Subway': 5}
    NUM_CLASSES = 6
    inp = inp.reshape((-1, HEIGHT, WIDTH, 3))
    inp = tf.keras.applications.nasnet.preprocess_input(inp)
    prediction = model.predict(inp)
    predicted_class_indices = np.argmax(prediction, axis=1)
    result = {}
    # for i in range(len(predicted_class_indices)):
    #     if predicted_class_indices[i] < NUM_CLASSES:
    #         try:
    #             label = labels[predicted_class_indices[i]]
    #             result[label] = float(predicted_class_indices[i])
    #         except KeyError:
    #             print(f"KeyError: Label not found for index {predicted_class_indices[i]}")
    label_order = ["Burger King", "KFC", "McDonalds", "Other", "Starbucks", "Subway"]

    # Assuming prediction is a dictionary with label keys
    # result = [f"{label}: {prediction[label]:.2f}" for label in label_order]
    # return ", ".join(result)
    result = [f"{label}: {prediction[label]:.2f}" for label in labels]
    return ", ".join(result)



 



    
image = gr.Image(shape=(HEIGHT,WIDTH),label='Input')
label = gr.Textbox()

gr.Interface(fn=classify_image, inputs=image, outputs=label, title='Brand Logo Detection').launch(debug=False)