Daniel00611 commited on
Commit
8f728a2
verified
1 Parent(s): 48b4546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -1,11 +1,12 @@
1
  from fastapi import FastAPI, UploadFile, File
 
2
  from huggingface_hub import hf_hub_download
3
  from keras.models import load_model
4
- from tensorflow.keras.preprocessing import image
5
  from tensorflow.keras.applications.inception_v3 import preprocess_input
6
  import numpy as np
7
  from PIL import Image
8
  import io
 
9
 
10
  app = FastAPI()
11
 
@@ -27,30 +28,58 @@ class_names = ['acanthoica', 'akashiwo', 'alexandrium', 'amoeba', 'amphidinium',
27
  model_path = hf_hub_download(repo_id="Daniel00611/InceptionV3_72", filename="InceptionV3_72.keras")
28
  model = load_model(model_path)
29
 
30
- def preprocess_image(image_file, target_size=(299, 299)):
31
- # Convertir el archivo a BytesIO
32
- img_bytes = image_file.read() # Leer el archivo como bytes
33
- img = Image.open(io.BytesIO(img_bytes)) # Abrir la imagen con PIL
34
- img = img.resize(target_size) # Redimensionar la imagen al tama帽o objetivo
35
- img_array = np.array(img) # Convertir la imagen a un array de NumPy
36
- img_array = np.expand_dims(img_array, axis=0) # A帽adir dimensi贸n extra para lote
37
  return img_array
38
-
 
 
 
 
 
39
  @app.post("/predict/")
40
  async def predict(file: UploadFile = File(...)):
41
  try:
42
- # Procesar la imagen
43
- img_array = preprocess_image(file.file)
 
44
 
45
- # Realizar la predicci贸n
46
  predictions = model.predict(img_array)[0]
47
 
48
  # Obtener el top 10 de predicciones
49
  top_10_indices = predictions.argsort()[-10:][::-1]
50
  top_10_classes = [class_names[i] for i in top_10_indices]
51
  top_10_probabilities = predictions[top_10_indices]
 
 
 
 
52
 
53
- # Formar la respuesta en formato JSON
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  result = [{"class": top_10_classes[i], "probability": float(top_10_probabilities[i])} for i in range(10)]
55
  return {"predictions": result}
56
 
 
1
  from fastapi import FastAPI, UploadFile, File
2
+ from pydantic import BaseModel
3
  from huggingface_hub import hf_hub_download
4
  from keras.models import load_model
 
5
  from tensorflow.keras.applications.inception_v3 import preprocess_input
6
  import numpy as np
7
  from PIL import Image
8
  import io
9
+ import base64
10
 
11
  app = FastAPI()
12
 
 
28
  model_path = hf_hub_download(repo_id="Daniel00611/InceptionV3_72", filename="InceptionV3_72.keras")
29
  model = load_model(model_path)
30
 
31
+ def preprocess_image(img, target_size=(299, 299)):
32
+ img = img.resize(target_size) # Redimensionar la imagen
33
+ img_array = np.array(img) # Convertir a array de NumPy
34
+ img_array = np.expand_dims(img_array, axis=0) # Expandir dimensi贸n para lote
35
+ img_array = preprocess_input(img_array) # Preprocesar la imagen
 
 
36
  return img_array
37
+
38
+ # Modelo para recibir la imagen en formato Base64
39
+ class ImageBase64(BaseModel):
40
+ image_base64: str
41
+
42
+ # Ruta para im谩genes subidas como archivo
43
  @app.post("/predict/")
44
  async def predict(file: UploadFile = File(...)):
45
  try:
46
+ # Leer la imagen subida
47
+ img = Image.open(io.BytesIO(await file.read()))
48
+ img_array = preprocess_image(img)
49
 
50
+ # Realizar predicci贸n
51
  predictions = model.predict(img_array)[0]
52
 
53
  # Obtener el top 10 de predicciones
54
  top_10_indices = predictions.argsort()[-10:][::-1]
55
  top_10_classes = [class_names[i] for i in top_10_indices]
56
  top_10_probabilities = predictions[top_10_indices]
57
+
58
+ # Formar respuesta en formato JSON
59
+ result = [{"class": top_10_classes[i], "probability": float(top_10_probabilities[i])} for i in range(10)]
60
+ return {"predictions": result}
61
 
62
+ except Exception as e:
63
+ return {"error": str(e)}
64
+
65
+ # Ruta para im谩genes en formato Base64
66
+ @app.post("/predict_base64/")
67
+ async def predict_base64(image_data: ImageBase64):
68
+ try:
69
+ # Decodificar la imagen Base64
70
+ image_bytes = base64.b64decode(image_data.image_base64)
71
+ img = Image.open(io.BytesIO(image_bytes))
72
+ img_array = preprocess_image(img)
73
+
74
+ # Realizar predicci贸n
75
+ predictions = model.predict(img_array)[0]
76
+
77
+ # Obtener el top 10 de predicciones
78
+ top_10_indices = predictions.argsort()[-10:][::-1]
79
+ top_10_classes = [class_names[i] for i in top_10_indices]
80
+ top_10_probabilities = predictions[top_10_indices]
81
+
82
+ # Formar respuesta en formato JSON
83
  result = [{"class": top_10_classes[i], "probability": float(top_10_probabilities[i])} for i in range(10)]
84
  return {"predictions": result}
85