File size: 2,546 Bytes
503c7ba
 
 
57403af
503c7ba
3b1de78
57403af
503c7ba
e44d4e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b1de78
 
 
 
 
 
 
 
e44d4e7
 
 
 
 
 
 
 
afa7f02
503c7ba
 
57403af
e44d4e7
57403af
 
8dca2bc
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

model_path = "DogClassifier2.1.keras"
model = tf.keras.models.load_model(model_path)

# Define the core prediction function
def predict_bmwX(image):
    # Preprocess image
    image = Image.fromarray(image.astype('uint8'))  # Convert numpy array to PIL image
    image = image.convert("RGB")  # Ensure the image is in RGB format
    image = image.resize((150, 150))  # Resize the image to 150x150
    image = np.array(image)
    image = np.expand_dims(image, axis=0)  # Add batch dimension

    # Predict
    prediction = model.predict(image)
    
    # Apply softmax to get probabilities for each class
    prediction = tf.nn.softmax(prediction)
    
    # Define class names
    class_names = ['Afghan', 'African Wild Dog', 'Airedale', 'American Hairless', 'American Spaniel', 'Basenji', 'Basset', 'Beagle', 
                   'Bearded Collie', 'Bermaise', 'Bichon Frise', 'Blenheim', 'Bloodhound', 'Bluetick', 'Border Collie', 'Borzoi',
                   'Boston Terrier', 'Boxer', 'Bull Mastiff', 'Bull Terrier', 'Bulldog', 'Cairn', 'Chihuahua', 'Chinese Crested',
                   'Chow', 'Clumber','Cockapoo', 'Cocker', 'Collie', 'Corgi', 'Coyote', 'Dalmation', 'Dhole', 'Dingo', 'Doberman',
                   'Elk Hound', 'French Bulldog', 'German Sheperd', 'Golden Retriever', 'Great Dane', 'Great Perenees', 'Greyhound',
                   'Groenendael', 'Irish Spaniel', 'Irish Wolfhound', 'Japanese Spaniel', 'Komondor', 'Labradoodle', 'Labrador',
                   'Lhasa', 'Malinois', 'Maltese', 'Maltese', 'Mex Hairless', 'Newfoundland', 'Pekinese', 'Pit Bull', 'Pomeranian',
                   'Poodle', 'Pug', 'Rhodesian', 'Rottweiler', 'Saint Bernard', 'Schnauzer', 'Scotch Terrier', 'Shar_Pei', 
                   'Shiba Inu', 'Shih-Tzu', 'Siberian Husky', 'Vizsla', 'Yorkie']
    
    # Create a dictionary with the probabilities for each dog breed
    prediction_dict = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
    
    # Sort the dictionary by value in descending order and get the top 3 classes
    sorted_predictions = dict(sorted(prediction_dict.items(), key=lambda item: item[1], reverse=True))
        
    return sorted_predictions


input_image = gr.Image()
iface = gr.Interface(
    fn=predict_bmwX,
    inputs=input_image, 
    outputs=gr.Label(),
    description="A simple MLP classification model for image classification using the MNIST dataset.")
iface.launch(share=True)