Spaces:
Sleeping
Sleeping
Alex Vega
commited on
Commit
·
cd97e1e
1
Parent(s):
e9aefdb
up
Browse files
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# main.py
|
2 |
|
3 |
import io
|
4 |
import numpy as np
|
@@ -15,10 +15,10 @@ class TranslationResponse(BaseModel):
|
|
15 |
try:
|
16 |
model = tf.keras.models.load_model('best_model.keras')
|
17 |
except Exception as e:
|
18 |
-
raise IOError(f"Error al cargar el modelo 'best_model.keras'.
|
19 |
|
20 |
app = FastAPI(
|
21 |
-
title="API
|
22 |
description="Sube una imagen del alfabeto de señas (ASL) para obtener una predicción del modelo.",
|
23 |
version="1.0.0"
|
24 |
)
|
@@ -29,20 +29,28 @@ CLASS_NAMES = [
|
|
29 |
'del', 'nothing', 'space'
|
30 |
]
|
31 |
|
|
|
32 |
def preprocess_image(image: Image.Image) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
image = image.resize((96, 96))
|
34 |
image_array = np.array(image)
|
35 |
|
36 |
-
|
37 |
-
if image_array.shape[2] == 4:
|
38 |
image_array = image_array[..., :3]
|
39 |
|
40 |
-
|
41 |
-
normalized_array = image_array.astype('float32') / 255.0
|
42 |
-
return np.expand_dims(normalized_array, axis=0)
|
43 |
|
44 |
@app.post("/predict/", response_model=TranslationResponse)
|
45 |
async def predict(file: UploadFile = File(...)):
|
|
|
|
|
|
|
46 |
contents = await file.read()
|
47 |
|
48 |
try:
|
@@ -55,13 +63,11 @@ async def predict(file: UploadFile = File(...)):
|
|
55 |
predictions = model.predict(processed_image)
|
56 |
|
57 |
predicted_index = np.argmax(predictions, axis=1)[0]
|
58 |
-
|
59 |
confidence = float(predictions[0][predicted_index])
|
60 |
-
|
61 |
prediction_label = CLASS_NAMES[predicted_index]
|
62 |
|
63 |
return TranslationResponse(prediction=prediction_label, confidence=confidence)
|
64 |
|
65 |
@app.get("/")
|
66 |
def read_root():
|
67 |
-
return {"message": "
|
|
|
1 |
+
# main.py (CORREGIDO)
|
2 |
|
3 |
import io
|
4 |
import numpy as np
|
|
|
15 |
try:
|
16 |
model = tf.keras.models.load_model('best_model.keras')
|
17 |
except Exception as e:
|
18 |
+
raise IOError(f"Error al cargar el modelo 'best_model.keras'. Error: {e}")
|
19 |
|
20 |
app = FastAPI(
|
21 |
+
title="API keras asl",
|
22 |
description="Sube una imagen del alfabeto de señas (ASL) para obtener una predicción del modelo.",
|
23 |
version="1.0.0"
|
24 |
)
|
|
|
29 |
'del', 'nothing', 'space'
|
30 |
]
|
31 |
|
32 |
+
# --- Funciones de preprocesamiento (CORREGIDAS) ---
|
33 |
def preprocess_image(image: Image.Image) -> np.ndarray:
|
34 |
+
"""
|
35 |
+
Preprocesa la imagen para que sea compatible con el modelo.
|
36 |
+
- Cambia el tamaño a 96x96 píxeles.
|
37 |
+
- La convierte a un array de NumPy.
|
38 |
+
- Añade una dimensión de batch.
|
39 |
+
- NO normaliza, ya que el modelo tiene una capa de Rescaling.
|
40 |
+
"""
|
41 |
image = image.resize((96, 96))
|
42 |
image_array = np.array(image)
|
43 |
|
44 |
+
if image_array.shape[2] == 4: # Maneja imágenes RGBA
|
|
|
45 |
image_array = image_array[..., :3]
|
46 |
|
47 |
+
return np.expand_dims(image_array, axis=0)
|
|
|
|
|
48 |
|
49 |
@app.post("/predict/", response_model=TranslationResponse)
|
50 |
async def predict(file: UploadFile = File(...)):
|
51 |
+
"""
|
52 |
+
Endpoint para predecir la letra del lenguaje de señas a partir de una imagen.
|
53 |
+
"""
|
54 |
contents = await file.read()
|
55 |
|
56 |
try:
|
|
|
63 |
predictions = model.predict(processed_image)
|
64 |
|
65 |
predicted_index = np.argmax(predictions, axis=1)[0]
|
|
|
66 |
confidence = float(predictions[0][predicted_index])
|
|
|
67 |
prediction_label = CLASS_NAMES[predicted_index]
|
68 |
|
69 |
return TranslationResponse(prediction=prediction_label, confidence=confidence)
|
70 |
|
71 |
@app.get("/")
|
72 |
def read_root():
|
73 |
+
return {"message": "Bienvenido a la API de Lenguaje de Señas. Usa el endpoint /predict/ para hacer una predicción."}
|