Spaces:
Sleeping
Sleeping
File size: 1,734 Bytes
28c7dea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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
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)
# Load and preprocess the image
try:
image = Image.open(img).convert(channel).resize((256, 256))
except:
print("ERROR")
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())
cataract_app.launch(share=True) |