File size: 1,795 Bytes
34c6e77
 
 
 
505dd84
4ee7a4c
34c6e77
505dd84
 
 
a91136c
963c9f9
34c6e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5862b7f
 
 
4529bef
f4d278c
 
6939a5a
4ee7a4c
b3e1e17
34c6e77
 
 
 
 
bd50a55
34c6e77
5862b7f
34c6e77
 
4ee7a4c
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
import json
import tensorflow_hub as hub
from PIL import Image, ImageDraw, ImageFont

# Register the custom layer
tf.keras.utils.get_custom_objects().update({'KerasLayer': hub.KerasLayer})

# Load the model
model = tf.keras.models.load_model("dog_breed_model.h5")

def load_breeds(file_path='unique_breeds.json'):
    with open(file_path, 'r') as file:
        data = json.load(file)
    return data["breeds"]

unique_breeds = load_breeds('unique_breeds.json')  # Load breed names into your array

def process_image(image, img_size=224):
    """
    Takes a PIL image and turns it into a preprocessed Tensor.
    """
    image = image.resize((img_size, img_size))  # Resize
    img_array = tf.keras.preprocessing.image.img_to_array(image)  # Convert to array
    img_array = img_array / 255.0  # Normalize
    return img_array

def predict_breed(image):
    img_array = process_image(image)
    img_array = tf.expand_dims(img_array, axis=0)  # Add batch dimension
    predictions = model.predict(img_array)
    predicted_class_index = np.argmax(predictions[0])
    predicted_class = unique_breeds[predicted_class_index]
    confidence = predictions[0][predicted_class_index] * 100  # Convert to percentage
    
    # Replace underscores with spaces in the class name and capitalize
    predicted_class = predicted_class.replace('_', ' ').upper()
   

    return f"{predicted_class} ({confidence:.2f}%)"

# Create Gradio interface
interface = gr.Interface(
    fn=predict_breed,
    inputs=gr.Image(type="pil"),
    outputs=gr.Text(),
    title="Dog Breed Identifier 🐶",
    description="Upload a dog image and the model will predict the breed along with confidence!"
)

# Launch app with public link
interface.launch(share=True)