import os import pickle import numpy as np import tensorflow as tf from flask import Flask, render_template, request from PIL import Image import keras from keras.applications.vgg16 import preprocess_input from tensorflow.keras.applications.vgg16 import preprocess_input import gradio as gr import cv2 def model(img): model_dir = 'pneumonia' class_names = ['Normal eye', 'pneumonic lung'] # Pneumonia class labels channel='L' selected_model = 'pneumonia' architecture_path = os.path.join('models',model_dir, f'model_architecture_{selected_model}.pkl') weights_path = os.path.join('models',model_dir, f'model_weights_{selected_model}.pkl') with open(architecture_path, 'rb') as f: loaded_model_architecture = pickle.load(f) with open(weights_path, 'rb') as f: loaded_model_weights = pickle.load(f) # Create the model using the loaded architecture loaded_model = tf.keras.models.model_from_json(loaded_model_architecture) # Set the loaded weights to the model loaded_model.set_weights(loaded_model_weights) loaded_model.summary() # Load and preprocess the image try: # image = img.convert(channel).resize((256, 256)) image = cv2.resize(img, (256, 256)) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) except: print("ERROR") print(image) return "ERROR" x = np.array(image) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Make predictions predictions = loaded_model.predict(x) print(predictions) predicted_class_index = np.argmax(predictions[0]) print(predicted_class_index) predicted_class_label = class_names[predicted_class_index] return predicted_class_label cataract_app = gr.Interface(model,gr.Image(),["text"]) cataract_app.launch(share=True)