File size: 1,354 Bytes
372ddf5 1ba08d3 372ddf5 |
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 56 |
import tensorflow as tf
import numpy as np
import gradio as gr
from PIL import Image
# ----------------------------
# LOAD MODEL (LOCAL FILE)
# ----------------------------
model = tf.keras.models.load_model("CModel.h5")
print(model.input_shape)
IMG_SIZE = (224, 224)
CLASS_NAMES = [
"Normal",
"Monkeypox"
]
# ----------------------------
# PREDICTION FUNCTION
# ----------------------------
def predict_image(image):
image = image.convert("RGB")
image = image.resize(IMG_SIZE)
img_array = np.array(image) / 255.0
img_array = np.expand_dims(img_array, axis=0)
pred = model.predict(img_array)
if pred.shape[1] == 1:
confidence = float(pred[0][0])
label = CLASS_NAMES[1] if confidence > 0.5 else CLASS_NAMES[0]
return label, confidence
else:
class_index = int(np.argmax(pred))
confidence = float(pred[0][class_index])
return CLASS_NAMES[class_index], confidence
# ----------------------------
# GRADIO UI
# ----------------------------
interface = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil"),
outputs=[
gr.Label(label="Prediction"),
gr.Number(label="Confidence")
],
title="Monkeypox Classification using CNN",
description="Upload a skin image to classify Monkeypox using a CNN model."
)
interface.launch()
|