| import gradio as gr | |
| import numpy as np | |
| import cv2 | |
| from keras.models import load_model | |
| # Load the trained models | |
| model1 = load_model('./isatron_v3.h5') | |
| # model2 = load_model('./Isatron_v2.h5') | |
| # Print the loaded models | |
| print(model1) | |
| # print(model2) | |
| # Define image size and labels | |
| img_size1 = 150 | |
| # img_size2 = 224 | |
| labels = ['PNEUMONIA', 'NORMAL'] | |
| def load_and_preprocess_image1(img): | |
| # Convert Gradio image (PIL Image) to numpy array | |
| img = np.array(img) | |
| # Convert RGB to grayscale | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| # Resize image to the required input size | |
| img = cv2.resize(img, (img_size1, img_size1)) | |
| # Reshape image for model input | |
| img = img.reshape(-1, img_size1, img_size1, 1) | |
| # Normalize image | |
| img = img / 255.0 | |
| return img | |
| # def load_and_preprocess_image2(img): | |
| # # Convert Gradio image (PIL Image) to numpy array | |
| # img = np.array(img) | |
| # # Convert RGB to grayscale | |
| # img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| # # Resize image to the required input size | |
| # img = cv2.resize(img, (img_size2, img_size2)) | |
| # # Reshape image for model input | |
| # img = img.reshape(-1, img_size2, img_size2, 1) | |
| # # Normalize image | |
| # img = img / 255.0 | |
| # return img | |
| def image_classifier1(img): | |
| # Preprocess the image | |
| img = load_and_preprocess_image1(img) | |
| # Perform prediction using model1 | |
| prediction = model1.predict(img)[0][0] # Assuming your model outputs a single probability | |
| # Return the prediction | |
| return {'PNEUMONIA': prediction, 'NORMAL': 1-prediction} # Invert the prediction for Gradio | |
| # def image_classifier2(img): | |
| # # Preprocess the image | |
| # img = load_and_preprocess_image2(img) | |
| # # Perform prediction using model2 | |
| # prediction = model2.predict(img)[0][0] # Assuming your model outputs a single probability | |
| # # Return the prediction | |
| # return {'PNEUMONIA': 1-prediction, 'NORMAL': prediction} # Invert the prediction for Gradio | |
| # Create Gradio interfaces for each model | |
| demo_model1 = gr.Interface(fn=image_classifier1, inputs="image", outputs="label", title="Pneumonia Detection - Model 1") | |
| # demo_model2 = gr.Interface(fn=image_classifier2, inputs="image", outputs="label", title="Pneumonia Detection - Model 2") | |
| # gr.Parallel(demo_model1, demo_model2).launch(share=True) | |
| # Launch the interfaces simultaneously | |
| if __name__ == "__main__": | |
| # demo_model1.launch(share=True) | |
| demo_model1.launch(share=True) | |