Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,93 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
from ultralytics import YOLO
|
|
|
|
| 6 |
|
| 7 |
-
def predict(
|
| 8 |
-
|
| 9 |
model = YOLO("best_res2.pt")
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
for r in results:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
}
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
fdi_teeth = [
|
| 24 |
-
# Cuadrante 1 (Superior derecho)
|
| 25 |
'11', '12', '13', '14', '15', '16', '17', '18',
|
| 26 |
-
# Cuadrante 2 (Superior izquierdo)
|
| 27 |
'21', '22', '23', '24', '25', '26', '27', '28',
|
| 28 |
-
# Cuadrante 3 (Inferior izquierdo)
|
| 29 |
'31', '32', '33', '34', '35', '36', '37', '38',
|
| 30 |
-
# Cuadrante 4 (Inferior derecho)
|
| 31 |
'41', '42', '43', '44', '45', '46', '47', '48'
|
| 32 |
]
|
| 33 |
-
fdi_teeth
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
with gr.Blocks(css=css) as demo:
|
| 37 |
# Encabezado
|
| 38 |
gr.Markdown("<h1 style='text-align: center;'>Welcome to DentAIxpert</h1>"
|
|
@@ -41,11 +96,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 41 |
# Contenedor para la carga de im谩genes y resultados
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column():
|
| 44 |
-
input_image = gr.Image(type="filepath", label="Input")
|
| 45 |
with gr.Column():
|
| 46 |
-
output_image = gr.Image(type="numpy", label="Output")
|
|
|
|
| 47 |
|
| 48 |
analyze_button = gr.Button("Submit")
|
| 49 |
-
analyze_button.click(fn=predict, inputs=input_image, outputs=output_image)
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
from collections import Counter
|
| 6 |
|
| 7 |
+
def predict(image_path: str):
|
| 8 |
+
# Cargar el modelo
|
| 9 |
model = YOLO("best_res2.pt")
|
| 10 |
+
|
| 11 |
+
# Leer la imagen desde la ruta
|
| 12 |
+
image = cv2.imread(image_path)
|
| 13 |
+
|
| 14 |
+
# Realizar la predicci贸n
|
| 15 |
+
results = model.predict(source=image_path)
|
| 16 |
+
|
| 17 |
+
# Lista para almacenar las etiquetas predichas
|
| 18 |
+
predicted_labels = []
|
| 19 |
|
| 20 |
for r in results:
|
| 21 |
+
# Obtener las etiquetas de las predicciones
|
| 22 |
+
labels = r.names # Nombres de las clases predichas
|
| 23 |
+
classes = r.boxes.cls # IDs de las clases predichas
|
| 24 |
+
|
| 25 |
+
# Se guardan las etiquetas de las predicciones
|
| 26 |
+
for cls_id in classes:
|
| 27 |
+
predicted_labels.append(labels[int(cls_id)])
|
| 28 |
|
| 29 |
+
# Contar las ocurrencias de cada diente en predicted_labels
|
| 30 |
+
predicted_counts = Counter(predicted_labels)
|
| 31 |
+
|
| 32 |
+
# Filtrar dientes identificados m谩s de una vez
|
| 33 |
+
identified_multiple = {tooth for tooth, count in predicted_counts.items() if count > 1}
|
| 34 |
+
|
| 35 |
+
# Convertir predicted_labels a un conjunto para comparaci贸n
|
| 36 |
+
predicted_set = set(predicted_labels)
|
| 37 |
+
|
| 38 |
+
# Encontrar dientes que no se identificaron
|
| 39 |
+
missing_teeth = [tooth for tooth in fdi_teeth if tooth not in predicted_set]
|
| 40 |
+
|
| 41 |
+
# Ordenar los dientes seg煤n el sistema FDI
|
| 42 |
+
def fdi_sort_key(tooth):
|
| 43 |
+
quadrant = int(tooth[0])
|
| 44 |
+
position = int(tooth[1])
|
| 45 |
+
return (quadrant, position)
|
| 46 |
|
| 47 |
+
# Generar el texto con los resultados
|
| 48 |
+
results_text = []
|
| 49 |
+
|
| 50 |
+
results_text.append("Todos los dientes identificados:")
|
| 51 |
+
for tooth in sorted(predicted_set, key=fdi_sort_key):
|
| 52 |
+
results_text.append(f"Diente {tooth}")
|
| 53 |
+
|
| 54 |
+
results_text.append("\nDientes identificados m谩s de una vez (requieren revisi贸n):")
|
| 55 |
+
for tooth in sorted(identified_multiple, key=fdi_sort_key):
|
| 56 |
+
results_text.append(f"Diente {tooth}")
|
| 57 |
+
|
| 58 |
+
results_text.append("\nDientes no identificados:")
|
| 59 |
+
for tooth in sorted(missing_teeth, key=fdi_sort_key):
|
| 60 |
+
results_text.append(f"Diente {tooth}")
|
| 61 |
+
|
| 62 |
+
# Convertir la lista de resultados a una sola cadena de texto
|
| 63 |
+
results_text_str = "\n".join(results_text)
|
| 64 |
+
|
| 65 |
+
# Devolver la imagen con las predicciones y el texto de resultados
|
| 66 |
+
# Convertir la imagen con las predicciones a formato numpy para Gradio
|
| 67 |
+
output_image = results[0].plot() # Usar la primera imagen de resultados
|
| 68 |
+
output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB) # Convertir BGR a RGB
|
| 69 |
+
|
| 70 |
+
return output_image, results_text_str
|
| 71 |
+
|
| 72 |
+
# Lista de dientes en el sistema FDI
|
| 73 |
fdi_teeth = [
|
|
|
|
| 74 |
'11', '12', '13', '14', '15', '16', '17', '18',
|
|
|
|
| 75 |
'21', '22', '23', '24', '25', '26', '27', '28',
|
|
|
|
| 76 |
'31', '32', '33', '34', '35', '36', '37', '38',
|
|
|
|
| 77 |
'41', '42', '43', '44', '45', '46', '47', '48'
|
| 78 |
]
|
|
|
|
| 79 |
|
| 80 |
+
# URL de la imagen para el fondo de la interfaz
|
| 81 |
+
image_url = "https://huggingface.co/spaces/Jass0117/test/blob/main/diente.jpg"
|
| 82 |
+
|
| 83 |
+
# CSS para el fondo de la interfaz
|
| 84 |
+
css = f"""
|
| 85 |
+
.gradio-container {{
|
| 86 |
+
background-color: #ADD8E6; /* Fondo azul claro */
|
| 87 |
+
}}
|
| 88 |
+
"""
|
| 89 |
|
| 90 |
+
# Crear la interfaz de Gradio
|
| 91 |
with gr.Blocks(css=css) as demo:
|
| 92 |
# Encabezado
|
| 93 |
gr.Markdown("<h1 style='text-align: center;'>Welcome to DentAIxpert</h1>"
|
|
|
|
| 96 |
# Contenedor para la carga de im谩genes y resultados
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
| 99 |
+
input_image = gr.Image(type="filepath", label="Input Image")
|
| 100 |
with gr.Column():
|
| 101 |
+
output_image = gr.Image(type="numpy", label="Output Image")
|
| 102 |
+
results_text = gr.Textbox(label="Analysis Results", lines=10, placeholder="Results will appear here...")
|
| 103 |
|
| 104 |
analyze_button = gr.Button("Submit")
|
| 105 |
+
analyze_button.click(fn=predict, inputs=input_image, outputs=[output_image, results_text])
|
| 106 |
|
| 107 |
demo.launch()
|