Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,15 @@ import gradio as gr
|
|
8 |
IMG_HEIGHT = 224
|
9 |
IMG_WIDTH = 224
|
10 |
|
11 |
-
# Função para construir o modelo
|
12 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
inp = Input(shape=(img_height, img_width, n))
|
14 |
efnet = efn.EfficientNetB0(
|
15 |
input_shape=(img_height, img_width, n),
|
@@ -25,41 +32,44 @@ def build_model(img_height, img_width, n):
|
|
25 |
model.compile(optimizer=opt, loss=loss, metrics=['accuracy'])
|
26 |
return model
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
-
#
|
33 |
def preprocess_image(input_image):
|
34 |
-
# Redimensione a imagem para as dimensões esperadas pelo modelo
|
35 |
input_image = tf.image.resize(input_image, (IMG_HEIGHT, IMG_WIDTH))
|
36 |
-
|
37 |
-
# Normalização dos valores de pixel para o intervalo [0, 1]
|
38 |
input_image = input_image / 255.0
|
39 |
-
|
40 |
-
# Outras transformações, se necessárias (por exemplo, normalização adicional)
|
41 |
-
|
42 |
return input_image
|
43 |
|
44 |
-
#
|
45 |
def predict_image(input_image):
|
46 |
# Realize o pré-processamento na imagem de entrada
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
prediction = loaded_model.predict(input_image)
|
52 |
|
53 |
# A saída será uma matriz de previsões (no caso de classificação de duas classes, será algo como [[probabilidade_classe_0, probabilidade_classe_1]])
|
54 |
# Adicione lógica para interpretar o resultado e formatá-lo para exibição
|
|
|
55 |
class_names = ["Normal", "Cataract"]
|
56 |
-
predicted_class = class_names[np.argmax(
|
57 |
-
probability =
|
|
|
|
|
|
|
|
|
58 |
|
|
|
59 |
formatted_text = f"Predicted Class: {predicted_class}\nProbability: {probability:.2%}"
|
60 |
return formatted_text
|
61 |
|
62 |
-
|
63 |
# Crie uma interface Gradio para fazer previsões
|
64 |
iface = gr.Interface(
|
65 |
fn=predict_image,
|
@@ -69,4 +79,4 @@ iface = gr.Interface(
|
|
69 |
)
|
70 |
|
71 |
# Execute a interface Gradio
|
72 |
-
iface.launch()
|
|
|
8 |
IMG_HEIGHT = 224
|
9 |
IMG_WIDTH = 224
|
10 |
|
11 |
+
# Função para construir o modelo de detecção de objetos
|
12 |
+
def build_object_detection_model(img_height, img_width):
|
13 |
+
# Replace this with your object detection model architecture and weights
|
14 |
+
# For example, you can use a model from TensorFlow Hub or any other source
|
15 |
+
object_detection_model = None # Load your object detection model here
|
16 |
+
return object_detection_model
|
17 |
+
|
18 |
+
# Função para construir o modelo de classificação
|
19 |
+
def build_classification_model(img_height, img_width, n):
|
20 |
inp = Input(shape=(img_height, img_width, n))
|
21 |
efnet = efn.EfficientNetB0(
|
22 |
input_shape=(img_height, img_width, n),
|
|
|
32 |
model.compile(optimizer=opt, loss=loss, metrics=['accuracy'])
|
33 |
return model
|
34 |
|
35 |
+
# Load the object detection and classification models
|
36 |
+
object_detection_model = build_object_detection_model(IMG_HEIGHT, IMG_WIDTH)
|
37 |
+
classification_model = build_classification_model(IMG_HEIGHT, IMG_WIDTH, 3)
|
38 |
+
classification_model.load_weights('modelo_treinado.h5')
|
39 |
|
40 |
+
# Function to preprocess the image for classification
|
41 |
def preprocess_image(input_image):
|
|
|
42 |
input_image = tf.image.resize(input_image, (IMG_HEIGHT, IMG_WIDTH))
|
|
|
|
|
43 |
input_image = input_image / 255.0
|
|
|
|
|
|
|
44 |
return input_image
|
45 |
|
46 |
+
# Function to perform object detection and classification
|
47 |
def predict_image(input_image):
|
48 |
# Realize o pré-processamento na imagem de entrada
|
49 |
+
input_image_classification = preprocess_image(input_image)
|
50 |
+
|
51 |
+
# Faça uma previsão usando o modelo de classificação carregado
|
52 |
+
input_image_classification = tf.expand_dims(input_image_classification, axis=0)
|
53 |
+
classification_prediction = classification_model.predict(input_image_classification)
|
54 |
|
55 |
+
# Perform object detection here using the object_detection_model
|
56 |
+
# Replace this with your object detection logic to get bounding box coordinates
|
|
|
57 |
|
58 |
# A saída será uma matriz de previsões (no caso de classificação de duas classes, será algo como [[probabilidade_classe_0, probabilidade_classe_1]])
|
59 |
# Adicione lógica para interpretar o resultado e formatá-lo para exibição
|
60 |
+
|
61 |
class_names = ["Normal", "Cataract"]
|
62 |
+
predicted_class = class_names[np.argmax(classification_prediction)]
|
63 |
+
probability = classification_prediction[0][np.argmax(classification_prediction)]
|
64 |
+
|
65 |
+
# You can format the result with object detection bounding box and label here
|
66 |
+
# For example:
|
67 |
+
# formatted_text = f"Predicted Class: {predicted_class}\nProbability: {probability:.2%}\nObject Detection: {bounding_box_coordinates}"
|
68 |
|
69 |
+
# Return the formatted result
|
70 |
formatted_text = f"Predicted Class: {predicted_class}\nProbability: {probability:.2%}"
|
71 |
return formatted_text
|
72 |
|
|
|
73 |
# Crie uma interface Gradio para fazer previsões
|
74 |
iface = gr.Interface(
|
75 |
fn=predict_image,
|
|
|
79 |
)
|
80 |
|
81 |
# Execute a interface Gradio
|
82 |
+
iface.launch()
|