pneumonia / app.py
bhanusAI's picture
Update app.py
4d172bd verified
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 = 'cataract'
class_names = ['Normal', 'Cataract'] # Cataract class labels
channel='RGB'
selected_model = 'cataract'
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))
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 = 1 if (predictions[0]>0.5) else 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)