Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
|
|
|
|
3 |
from tensorflow.keras.layers import DepthwiseConv2D
|
|
|
|
|
|
|
4 |
|
5 |
# Función personalizada para DepthwiseConv2D
|
6 |
def custom_depthwise_conv2d(**kwargs):
|
@@ -35,49 +40,45 @@ labels = [
|
|
35 |
'Urticaria Ronchas', 'Tumores Vasculares', 'Vasculitis', 'Verrugas Molusco'
|
36 |
]
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def classify_image(image):
|
39 |
# Redimensionar la imagen a (224, 224) antes de la predicción
|
40 |
-
|
41 |
-
|
42 |
-
prediction = model.predict(
|
43 |
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
title = "AI-DERM DETECTION"
|
47 |
|
48 |
article = (
|
49 |
"Se propone un sistema automatizado para el diagnóstico de las 23 enfermedades comunes de la piel:\n\n"
|
50 |
-
|
51 |
-
"2. Queratosis Actínica / Carcinoma Basocelular\n"
|
52 |
-
"3. Dermatitis Atópica\n"
|
53 |
-
"4. Enfermedades Bullosas\n"
|
54 |
-
"5. Celulitis / Impétigo (Infecciones Bacterianas)\n"
|
55 |
-
"6. Eccema\n"
|
56 |
-
"7. Exantemas (Erupciones Cutáneas por Medicamentos)\n"
|
57 |
-
"8. Pérdida de Cabello (Alopecia)\n"
|
58 |
-
"9. Herpes / VPH\n"
|
59 |
-
"10. Trastornos de la Pigmentación\n"
|
60 |
-
"11. Lupus\n"
|
61 |
-
"12. Melanoma (Cáncer de Piel)\n"
|
62 |
-
"13. Hongos en las Uñas\n"
|
63 |
-
"14. Hiedra Venenosa\n"
|
64 |
-
"15. Psoriasis (liquen plano)\n"
|
65 |
-
"16. Sarna / Enfermedad de Lyme\n"
|
66 |
-
"17. Queratosis Seborreica\n"
|
67 |
-
"18. Enfermedad Sistémica\n"
|
68 |
-
"19. Tiña / Tiña (Infecciones Fúngicas)\n"
|
69 |
-
"20. Urticaria / Ronchas\n"
|
70 |
-
"21. Tumores Vasculares\n"
|
71 |
-
"22. Vasculitis\n"
|
72 |
-
"23. Verrugas / Molusco\n\n"
|
73 |
-
"Este sistema automatizado se basa en un modelo preentrenado EfficientNetB7, capaz de diagnosticar 23 enfermedades cutáneas comunes. La interfaz te permite cargar una imagen y obtener las probabilidades de cada enfermedad detectada."
|
74 |
-
"<p style='text-align: center'>"
|
75 |
-
"<span style='font-size: 15pt;'>AI-DERM . Jeysshon Bustos . 2023.</span>"
|
76 |
-
"</p>"
|
77 |
)
|
78 |
|
79 |
description = (
|
80 |
-
"Utilizamos la interfaz de usuario generada por Gradio para ingresar imágenes a nuestra red neuronal
|
81 |
)
|
82 |
|
83 |
examples = [
|
@@ -99,7 +100,6 @@ gr.Interface(
|
|
99 |
article=article,
|
100 |
description=description,
|
101 |
inputs=gr.Image(),
|
102 |
-
outputs=gr.Label(num_top_classes=4),
|
103 |
examples=examples
|
104 |
).launch()
|
105 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
from tensorflow.keras.layers import DepthwiseConv2D
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import io
|
8 |
+
from PIL import Image
|
9 |
|
10 |
# Función personalizada para DepthwiseConv2D
|
11 |
def custom_depthwise_conv2d(**kwargs):
|
|
|
40 |
'Urticaria Ronchas', 'Tumores Vasculares', 'Vasculitis', 'Verrugas Molusco'
|
41 |
]
|
42 |
|
43 |
+
def generate_heatmap(image, prediction):
|
44 |
+
# Crear un mapa de calor basado en la predicción
|
45 |
+
heatmap = np.mean(prediction, axis=-1) # Promediar los canales de predicción
|
46 |
+
heatmap = np.maximum(heatmap, 0) # Aplicar ReLU
|
47 |
+
heatmap /= np.max(heatmap) # Normalizar
|
48 |
+
|
49 |
+
# Redimensionar el mapa de calor al tamaño de la imagen
|
50 |
+
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
51 |
+
heatmap = np.uint8(255 * heatmap) # Convertir a rango 0-255 para superponer
|
52 |
+
|
53 |
+
# Convertir a colores
|
54 |
+
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
55 |
+
superimposed_image = cv2.addWeighted(image, 0.6, heatmap, 0.4, 0)
|
56 |
+
|
57 |
+
# Convertir a formato PIL para mostrar en Gradio
|
58 |
+
img_pil = Image.fromarray(superimposed_image)
|
59 |
+
return img_pil
|
60 |
+
|
61 |
def classify_image(image):
|
62 |
# Redimensionar la imagen a (224, 224) antes de la predicción
|
63 |
+
image_resized = tf.image.resize(image, (224, 224))
|
64 |
+
image_resized = tf.expand_dims(image_resized, axis=0) # Añadir una dimensión para el batch
|
65 |
+
prediction = model.predict(image_resized).flatten()
|
66 |
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
67 |
+
|
68 |
+
# Generar mapa de calor
|
69 |
+
heatmap_image = generate_heatmap(np.array(image), prediction)
|
70 |
+
|
71 |
+
return confidences, heatmap_image
|
72 |
|
73 |
title = "AI-DERM DETECTION"
|
74 |
|
75 |
article = (
|
76 |
"Se propone un sistema automatizado para el diagnóstico de las 23 enfermedades comunes de la piel:\n\n"
|
77 |
+
# (Artículos de la lista omitidos por brevedad)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
)
|
79 |
|
80 |
description = (
|
81 |
+
"Utilizamos la interfaz de usuario generada por Gradio para ingresar imágenes a nuestra red neuronal..."
|
82 |
)
|
83 |
|
84 |
examples = [
|
|
|
100 |
article=article,
|
101 |
description=description,
|
102 |
inputs=gr.Image(),
|
103 |
+
outputs=[gr.Label(num_top_classes=4), gr.Image(label="Mapa de Calor")],
|
104 |
examples=examples
|
105 |
).launch()
|
|