Spaces:
Sleeping
Sleeping
File size: 3,365 Bytes
adf2111 8c26c12 3639873 0596f27 5d99d2a 0596f27 fe7aa50 671c67e fe7aa50 8d05804 671c67e fe7aa50 671c67e fe7aa50 5d99d2a 8d05804 671c67e 5d99d2a f66d4da 0596f27 5d99d2a 0596f27 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import cv2
import datetime
from tensorflow.keras import backend as K
# Define the custom FixedDropout layer
class FixedDropout(tf.keras.layers.Layer):
def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
super(FixedDropout, self).__init__(**kwargs)
self.rate = rate
self.noise_shape = noise_shape # Include the noise_shape argument
self.seed = seed # Include the seed argument
def call(self, inputs, training=None):
if training is None:
training = K.learning_phase()
return K.in_train_phase(K.dropout(inputs, self.rate, noise_shape=self.noise_shape, seed=self.seed), inputs, training=training)
def get_config(self):
config = super(FixedDropout, self).get_config()
config['rate'] = self.rate # Serialize the rate argument
config['noise_shape'] = self.noise_shape # Serialize the noise_shape argument
config['seed'] = self.seed # Serialize the seed argument
return config
class ImageClassifierApp:
def __init__(self, model_path):
self.model_path = model_path
self.model = self.load_model()
self.class_labels = ["Normal", "Cataract"]
def load_model(self):
# Load the trained TensorFlow model
with tf.keras.utils.custom_object_scope({'FixedDropout': FixedDropout}):
model = tf.keras.models.load_model(self.model_path)
return model
def classify_image(self, input_image):
input_image = tf.image.resize(input_image, (192, 256))
input_image = (input_image / 255.0)
input_image = np.expand_dims(input_image, axis=0)
current_time = datetime.datetime.now()
prediction = self.model.predict(input_image)
class_index = np.argmax(prediction)
predicted_class = self.class_labels[class_index]
output_image = (input_image[0] * 255).astype('uint8')
output_image = cv2.copyMakeBorder(output_image, 0, 50, 0, 0, cv2.BORDER_CONSTANT, value=(255, 255, 255))
label_background = np.ones((50, output_image.shape[1], 3), dtype=np.uint8) * 255
output_image[-50:] = label_background
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.4
cv2.putText(output_image, f"Analysis Time: {current_time.strftime('%Y-%m-%d %H:%M:%S')}", (10, output_image.shape[0] - 30), font, font_scale, (0, 0, 0), 1)
cv2.putText(output_image, f"Predicted Class: {predicted_class}", (10, output_image.shape[0] - 10), font, font_scale, (0, 0, 0), 1)
image_height, image_width, _ = output_image.shape
box_size = 100
box_x = (image_width - box_size) // 2
box_y = (image_height - box_size) // 2
object_box_color = (255, 0, 0)
cv2.rectangle(output_image, (box_x, box_y), (box_x + box_size, box_y + box_size), object_box_color, 2)
return output_image
def run_interface(self):
input_interface = gr.Interface(
fn=self.classify_image,
inputs="image",
outputs="image",
live=True
)
input_interface.launch()
if __name__ == "__main__":
model_path = 'modelo_treinado.h5' # Replace with the path to your trained model
app = ImageClassifierApp(model_path)
app.run_interface()
|