File size: 1,362 Bytes
8c3eaa3
 
 
 
 
bb7c351
8c3eaa3
 
 
 
f4b14bc
9839997
 
 
8c3eaa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cdf806
3059806
667232d
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
from PIL import Image as PIL_Image
# Load the model
model = tf.keras.models.load_model('dogcat_model_bak.h5')

def classify_image(input_image):
    pil_img = PIL_Image.fromarray(input_image, 'RGB')
    # Preprocess the image
    img1 = pil_img.resize((64, 64))
    img2 = pil_img
    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.Image()
interface = gr.Interface(fn = classify_image, inputs=gr.Image(),outputs=classify_image(inputs))
    # Load and pre, capture_session=True)

# Launch the Gradio app
interface.launch()