| import numpy as np |
| import tensorflow as tf |
|
|
| from PIL import Image |
| from tensorflow.keras.models import load_model |
| from tensorflow.keras.preprocessing.image import img_to_array |
| import gradio as gr |
|
|
| |
| model = load_model('model.keras') |
| classnames = np.array(['Normal', 'Pneumonia']) |
|
|
| |
| def transform(img): |
| img = img.resize((224, 224)) |
| img_array = img_to_array(img) |
| img_array = img_array / 255.0 |
| img_array = np.expand_dims(img_array, axis=0) |
| return img_array |
|
|
| |
| def classify(path): |
| img = Image.open(path) |
| processed_img = transform(img) |
| result = model.predict(processed_img)[0] |
| index = np.argmax(result) |
| predict = str(classnames[index]) |
| return predict |
|
|
| |
| with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.blue, neutral_hue=gr.themes.colors.zinc)) as demo: |
| img_path = gr.Image(label="Input Image", type="filepath", height=512, width=512) |
| output = gr.Textbox(label="Output") |
| clear_btn = gr.ClearButton([img_path, output], variant="stop") |
| img_path.upload(classify, inputs=img_path, outputs=output) |
| demo.launch() |