File size: 1,317 Bytes
8c3eaa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np

# Load the model
model = tf.keras.models.load_model('dogcat_model_bak.h5')

def classify_image(input_image):
    # Load and preprocess the image
    img1 = image.load_img(input_image.name, target_size=(64, 64))
    img2 = image.load_img(input_image.name)
    img = image.img_to_array(img1)
    img = img / 255.0
    img = np.expand_dims(img, axis=0)

    # Make prediction
    prediction = model.predict(img)

    # Display prediction result on the image
    if prediction[0][0] > 0.5:
        value = 'Dog: %1.2f' % prediction[0][0]
        plt.text(20, 62, value, color='red', fontsize=18, bbox=dict(facecolor='white', alpha=0.8))
    else:
        value = 'Cat: %1.2f' % (1.0 - prediction[0][0])
        plt.text(20, 62, value, color='red', fontsize=18, bbox=dict(facecolor='white', alpha=0.8))

    plt.imshow(img2)
    plt.axis('off')  # Hide axis for better visualization
    plt.show()

    return img2  # Return the image with prediction annotations

# Interface creation using Gradio
inputs = gr.inputs.Image()
outputs = gr.outputs.Image()
interface = gr.Interface(classify_image, inputs, outputs, capture_session=True)

# Launch the Gradio app
interface.launch()