Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import gradio as grimport numpy as npimport cv2from tensorflow.keras.models import load_model# Custom object scope to handle InputLayer configurationfrom tensorflow.keras.layers import InputLayercustom_objects = {'InputLayer': InputLayer}# Load the modeldef load_ocular_model():return load_model('odir_cnn_model.h5', custom_objects=custom_objects)model = load_ocular_model()# Define the labelsLABELS = ['Normal (N)', 'Diabetes (D)', 'Glaucoma (G)', 'Cataract (C)','Age related Macular Degeneration (A)', 'Hypertension (H)','Pathological Myopia (M)', 'Other diseases/abnormalities (O)']# Preprocess the imagedef preprocess_image(image, img_size=128):img = cv2.resize(image, (img_size, img_size))img = img / 255.0 # Normalizeimg = np.expand_dims(img, axis=0) # Add batch dimensionreturn img# Prediction functiondef predict_diseases(left_image, right_image):left_img = preprocess_image(left_image)right_img = preprocess_image(right_image)left_predictions = model.predict(left_img)right_predictions = model.predict(right_img)combined_predictions = (left_predictions + right_predictions) / 2pred_labels = {label: float(pred) for label, pred in zip(LABELS, combined_predictions[0])}return pred_labels# Gradio interfaceiface = gr.Interface(fn=predict_diseases,inputs=[gr.inputs.Image(type="numpy"), gr.inputs.Image(type="numpy")],outputs="json",title="Ocular Disease Prediction",description="Upload left and right eye images to predict ocular diseases.")# Launch the interfaceiface.launch()
app.py+52