| import gradio as gr |
| import tensorflow as tf |
| import numpy as np |
| import cv2 |
| from huggingface_hub import hf_hub_download |
| from tensorflow.keras.preprocessing import image |
|
|
| |
| from preprocessing.zoom import apply_zoom |
| from preprocessing.hair_removal import quitar_pelos |
| from preprocessing.segmentation import segmentar_lesion |
| from preprocessing.metrics import ( |
| calcular_area, |
| calcular_perimetro, |
| calcular_circularidad, |
| calcular_simetria |
| ) |
|
|
| |
| ROWS, COLS = 224, 224 |
|
|
| |
| model_path = hf_hub_download(repo_id="Martinagg/simpleNet", filename="simpleNet.h5") |
| model = tf.keras.models.load_model(model_path) |
|
|
| |
| def preprocess_and_predict(img_input): |
| |
| img = np.array(img_input)[:, :, ::-1] |
|
|
| |
| zoomed = apply_zoom(img, zoom_factor=0.9) |
|
|
| |
| rgb_clean = cv2.cvtColor(zoomed, cv2.COLOR_BGR2RGB) |
| clean = quitar_pelos(rgb_clean) |
|
|
| |
| mask, lesion_rgb = segmentar_lesion(clean, size=(ROWS, COLS)) |
|
|
| |
| lesion_resized = cv2.resize(lesion_rgb, (ROWS, COLS)) |
| img_array = image.img_to_array(lesion_resized) / 255.0 |
| img_array = np.expand_dims(img_array, axis=0) |
|
|
| |
| probs = model.predict(img_array)[0] |
| classes = ["Benign", "Malignant"] |
| pred_idx = np.argmax(probs) |
| pred_label = classes[pred_idx] |
| result_text = f"Predicci贸n: {pred_label} ({probs[pred_idx]*100:.2f}%)" |
|
|
| |
| area = calcular_area(mask) |
| perim = calcular_perimetro(mask) |
| circ = calcular_circularidad(mask) |
| sim_v, sim_h = calcular_simetria(mask) |
|
|
| metrics_text = ( |
| f"脕rea: {area}\n" |
| f"Per铆metro: {perim}\n" |
| f"Circularidad: {circ}\n" |
| f"Simetr铆a Vertical: {sim_v}\n" |
| f"Simetr铆a Horizontal: {sim_h}" |
| ) |
|
|
| return mask, lesion_rgb, result_text, metrics_text |
|
|
| |
| demo = gr.Interface( |
| fn=preprocess_and_predict, |
| inputs=gr.Image(type="pil", label="Sube una imagen de lesi贸n"), |
| outputs=[ |
| gr.Image(type="numpy", label="M谩scara Binaria"), |
| gr.Image(type="numpy", label="Lesi贸n Segmentada"), |
| gr.Textbox(label="Resultado del Modelo"), |
| gr.Textbox(label="M茅tricas de la Lesi贸n") |
| ], |
| title="DermaScan - Clasificaci贸n de Lesiones", |
| description="Sube una imagen de piel. El sistema segmenta la lesi贸n, muestra la m谩scara, la predicci贸n y m茅tricas geom茅tricas." |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|