File size: 1,614 Bytes
aa5b7bd
 
 
 
 
 
 
4013e19
aa5b7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image

model = tf.keras.models.load_model('best_model_InceptionV3.h5')

# Function for prediction
def predict(img):
    try:
        img_resized = img.resize((224, 224))  # Resize image to the target size
        img_array = image.img_to_array(img_resized)  # Convert image to array
        img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
        img_array = preprocess_input(img_array)  # Preprocess image according to model requirements

        predictions = model.predict(img_array)
        class_idx = np.argmax(predictions, axis=1)[0]
        class_labels = ['Benign', 'Malignant']  # Update according to your class labels
        class_label = class_labels[class_idx]
        confidence = float(predictions[0][class_idx])

        return f"Class: {class_label}, Confidence: {confidence:.2f}"
    except Exception as e:
        return f"Error in prediction: {e}"

# Define the Gradio app
with gr.Blocks() as demo:
    gr.Markdown("Image Classification with InceptionV2")
    
    with gr.Row():
        with gr.Column():
            classify_input = gr.Image(type="pil", label="Upload an Image")
            classify_button = gr.Button("Classify!")
        with gr.Column():
            classify_output = gr.Textbox(label="Classification Result")

    classify_button.click(
        predict,
        inputs=[classify_input],
        outputs=[classify_output]
    )

demo.launch()